Log in Code Help

HYKE

Active Member
Joined
Jan 31, 2010
Messages
373
Helle there,

I found this code in the net and copied it to my current project in ms access ( I am a beginner ) however there is one particular line that is not working. I want lo load navigation form when the user login is successful but nothing is happening no matter how many times I click the okay button. can somebody help figure out the error in the code?
Code:
Option Compare Database

Private intLogAttempt As Integer
Private Sub cboUser_AfterUpdate()
txtPassword.SetFocus
End Sub
Private Sub cboUser_GotFocus()
    cboUser.Dropdown
End Sub
Private Sub cmdExit_Click()

    Response = MsgBox("Do you want close this application?", vbYesNo + vbQuestion, "Quit Application")
    
    If Response = vbYes Then
        Application.Quit
    End If

End Sub
Private Sub cmdLogin_Click()
    Call Login
End Sub
Private Sub cmdLogin_Enter()
    Call Login
End Sub
Public Sub Form_Load()
    
'    txtFocus.SetFocus 'txtFocus is a textbox control with height, widht and positions set to zero
    
End Sub
Public Sub Login()
On Error GoTo ErrorHandler:

    If IsNull([cboUser]) = True Then 'Check UserName
        MsgBox "Username is required", vbExclamation, "Emerging Market Service Dept Database"
        
    ElseIf IsNull([txtPassword]) = True Then 'Check Password
        MsgBox "Password is required"
        
    Else
    
        'Compare value of txtPassword with the saved Password in tblUser
        If Me.txtPassword.Value = DLookup("Password", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") Then
            strUser = Me.cboUser.Value 'Set the value of strUser declared as Global Variable
            strRole = DLookup("Role", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") 'set the value of strRole declared as Global Variable
            DoCmd.Close acForm, "frmLogin", acSaveNo
            MsgBox "Welcome Back, " & strUser, vbOKOnly, "Welcome"
            DoCmd.OpenForm "frmNavigation", acNormal, "", "", , acNormal
            
        Else
            MsgBox "Invalid Password. Please try again.", vbOKOnly, "Invalid Password"
            intLogAttempt = intLogAttempt + 1
            txtPassword.SetFocus
    
        End If
    
    End If
    
    'Check if the user has 3 wrong log-in attempts and close the application
    If intLogAttempt = 3 Then
        MsgBox "You do not have access to this database.Please contact admin." & vbCrLf & vbCrLf & _
        "Application will exit.", vbCritical, "Restricted Access!"
        Application.Quit
    End If
    
ErrorHandler:

End Sub
Thanks!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
try changing this line:
Code:
            DoCmd.Close acForm, "frmLogin", acSaveNo
to
Code:
            Me.Visible = False

Once you close the form on which the code is running, you stop the procedure. By hiding the form you can continue to use the form and display somethning else in its place.

Note: When you hide a form instead of closing it, be sure to actually close it once the procedure has finished running. Otherwise you will get an error when you try to launch an alreday open form. So, make sure that you have the Close statement just before the End Sub or that you check for the presence of the form before you open it again.

Denis
 
Upvote 0
Hi,

did as what you advised, changed the code to
Code:
 Me.Visible = False
but still the code is not closing the form and not loading the other form as well
 
Upvote 0
Does the MsgBox display when you run the code? If not, you are getting an error and the routine is exiting.
Try putting some code around the error handler --
Chenge this
Code:
ErrorHandler:
 
End Sub
To this
Code:
    Exit Sub
ErrorHandler:
    MsgBox "Error " & Err.Number & "; " & Err.Description
 
End Sub

Denis
 
Upvote 0
Hi Denis,

The msgbox displays this error:

Error 2471 : The expression you entered as a query parameter produced this error: 'Role'

Thanks,
HYKE
 
Upvote 0
This line is looking for a Role in tblUsers.
Code:
            strRole = DLookup("Role", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") 'set the value of strRole declared as Global Variable

Do you have such a field? If not, comment out that line and see how you go.

Denis
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top