Solved Problem with login form (1 Viewer)

justin0312

New member
Local time
Today, 22:14
Joined
Jul 2, 2021
Messages
22
Hi, I made some minor changes to my login form and I am having a problem when user key in invalid username/password, it will prompt me an error message (run-time error '3201': No current record.)

I tried testing with the followings
correct username, password and account is active, is working
correct username incorrect password and account is active, is working
correct username, password and account is inactive, is working
incorrect username, password, prompt me the error message

My table have this fields
ID | username | password | account status

I cannot figure out where is the issues. The error keep point to this line
Code:
If rst!password = Encrypt(Me.password) And rst![account status].value = "Inactive" Then

Hope someone can help me.

Code:
Private Sub login_Click()

  Dim db As DAO.Database
  Dim rst As DAO.Recordset
  Dim sql As String
 
  If Trim(Me.username.value & vbNullString) = vbNullString Then
    MsgBox prompt:="Please enter username.", buttons:=vbInformation, title:="Information Required"
    Me.username.SetFocus
    Exit Sub
  End If
 
  If Trim(Me.password.value & vbNullString) = vbNullString Then
    MsgBox prompt:="Please enter password.", buttons:=vbInformation, title:="Information Required"
    Me.password.SetFocus
    Exit Sub
  End If
 
  sql = "Select password, [security level], [account status] FROM tbl_db_user WHERE Username = '" & Me.username.value & "'"
 
  Set db = CurrentDb
  Set rst = db.OpenRecordset(sql)
 
  If rst!password = Encrypt(Me.password) And rst![account status].value = "Inactive" Then

  MsgBox prompt:="Account already inactive.", buttons:=vbCritical, title:="Error"
 
  Else
 
  If rst!password = Encrypt(Me.password) Then
    
  MsgBox prompt:="Welcome", buttons:=vbOKOnly, title:="Logon"
  
  Else
  MsgBox prompt:="Your username/password is incorrect.", buttons:=vbCritical, title:="Error"
  End If
 
  End If

 
 Set db = Nothing
 Set rst = Nothing

End Sub
 

Minty

AWF VIP
Local time
Today, 14:14
Joined
Jul 26, 2013
Messages
10,355
If your user name is invalid you won't have a recordset to look at.
You need to check for that - something like
Code:
Set rst = db.OpenRecordset(sql)

If rst.EOF or rst.BOF Then
   msgbox "Invalid User", , "Login Error"
   exit sub
End if
 

justin0312

New member
Local time
Today, 22:14
Joined
Jul 2, 2021
Messages
22
If your user name is invalid you won't have a recordset to look at.
You need to check for that - something like
Code:
Set rst = db.OpenRecordset(sql)

If rst.EOF or rst.BOF Then
   msgbox "Invalid User", , "Login Error"
   exit sub
End if
Thanks, sorry for my late acknowledgement.
 

Users who are viewing this thread

Top Bottom