我正在尝试验证用户名和密码,我无法理解问题?我该如何解决?

时间:2019-10-04 10:57:46

标签: vb6

我正在尝试验证用户名和密码,当我通过调试器调用GetUsername()和GetPassword()函数时,它会打印适当的值,但是当我在程序中键入相同的值时,它将显示用户名或密码不正确。

Private Sub cmdLogin_Click()
  Dim ReturnVal As Integer
  Dim resp As Integer
  Dim username As String
  Dim password As String
  Debug.Print GetUsername()
  Debug.Print GetPassword()
  username = GetUsername()
  password = GetPassword()
  If txtUsername.Text = username And txtPassword.Text = password Then
    ReturnVal = sndPlaySound("D:\Docs Dump\351877__theatomicbrain__start- 
    computer.wav", SND_ASYNC)
    frmMenu.Show
    FormLogin.Hide
  Else
    MsgBox "Incorrect Password or Username", vbOKOnly + vbCritical, "Error"
    txtUsername.Text = ""
    txtPassword.Text = ""
  End If
End Sub

GetUsername()和GetPassword():

Public Function GetUsername() As String
  Dim username As String
  Set db = New Connection
  Set adoRsLogin_Username = New Recordset
  db.Open "PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = D:\Docs Dump\School 
  stuff\Hytekcrete_Project\HytekcreteDB\DB_old.mdb;"
  adoRsLogin_Username.Open " select User_ID from Login ", db, adOpenStatic, 
  adLockOptimistic
  username = adoRsLogin_Username.GetString
  adoRsLogin_Username.Close
  GetUsername = username
End Function
Public Function GetPassword() As String
  Dim password As String
  Set db = New Connection
  Set adoRsLogin_Password = New Recordset
  db.Open "PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = D:\Docs Dump\School 
  stuff\Hytekcrete_Project\HytekcreteDB\DB_old.mdb;"
  adoRsLogin_Password.Open " select Password from Login ", db, adOpenStatic, 
  adLockOptimistic
  password = adoRsLogin_Password.GetString
  adoRsLogin_Password.Close
  GetPassword = password
End Function

1 个答案:

答案 0 :(得分:0)

尝试一下

Private Sub cmdLogin_Click()
  Dim ReturnVal As Integer
  Dim resp As Integer
  Dim username As String
  Dim password As String
  Dim fUserNameOK As Boolean, fPassOK As Boolean

  username = GetUsername()
  password = GetPassword()
  Debug.Print username
  Debug.Print password

  fUserNameOK = (StrComp(username, txtUsername.Text, vbTextCompare) = 0)
  fPassOK = (StrComp(username, txtPassword.Text, vbBinaryCompare) = 0)
  If fUserNameOK And fPassOK Then
    ' play sound file
  Else
    ' login error
  End If
End Sub

在If行上设置一个断点。将鼠标悬停在fUserNameOK和fPassOK上。