-1

I'm trying to validate the username and password, when I call the GetUsername() and GetPassword() function through the debugger, it's printing the values which are appropriate but when I type the same values in the program, it shows incorrect username or password.

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() and 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

  • Apparently they are not the same values. Judging by the rest of the code, they have either different case or trailing spaces. – GSerg Oct 04 '19 at 11:00
  • I thought the same thing, but I carefully checked the values and apparently, they are exactly the same – Supriya Honawale Oct 04 '19 at 11:07
  • To ensure that there are no trailing spaces, I even used the Trim( String ) function and yet the same error – Supriya Honawale Oct 04 '19 at 11:13
  • 1
    Operator = (Equal to) is case sensitive. Username usually is not case sensitive so try something like this: If Trim$(LCase(txtUsername.Text)) = Trim$(LCase(username)) And txtPassword.Text = password Then ... – Smith Oct 04 '19 at 11:29
  • 1
    @Smith Converting either to lower or upper case for case-insensitive comparisons [is wrong](https://stackoverflow.com/a/234751/11683). Use the third parameter of [`StrComp`](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/strcomp-function) to perform case-insensitive comparisons. – GSerg Oct 04 '19 at 11:35
  • I explicitly set the username and password to lowercase to test the system. Doesn't work. – Supriya Honawale Oct 04 '19 at 11:36
  • The values in the database are: Username: "hey" Password: "9545" – Supriya Honawale Oct 04 '19 at 11:37
  • Set a breakpoint at the `If` line. Hover you mouse over the four involved variables. – GSerg Oct 04 '19 at 11:42
  • @GSerg I agree, StrComp is a better solution. Split that IF statement into 2 statements (1 for each condition) and determine which one (maybe both) is False. – Smith Oct 04 '19 at 11:55
  • Well, that is not a solution its an alternative, because when I input the values "hey" and "9545" directly into the IF statement, it works! so there is no need to split the IF statement. – Supriya Honawale Oct 04 '19 at 12:27
  • Moreover, I can't split the IF statement into two seperate conditions because I want both of them to be true at the same time (hence the 'AND' operator). – Supriya Honawale Oct 04 '19 at 12:31
  • 1
    Did you hover the mouse cursor over the four variables on that line though? – GSerg Oct 04 '19 at 12:57
  • 2
    Your code will only work, if you have exact ONE record in your login table OR the very first record is what you are searching for (hey/9545). are you sure, you should not include the username in your sql query? – nabuchodonossor Oct 07 '19 at 09:03

1 Answers1

0

Try this

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

Set a breakpoint at the If line. Hover you mouse over fUserNameOK and fPassOK.

Smith
  • 710
  • 1
  • 7
  • 11