Results 1 to 7 of 7
  1. #1
    need_help12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    58

    Log-in Form code

    Good afternoon.

    MY current db is setup with a log-in prompt that requires end-users to select a username from a drop-down list and type in the appropriate password. This currently works perfectly, however I'd like to make an alteration to the code, and my mind is mush at this point.

    I have two log-in choices, ADMIN and GUEST. Based on the choice the end-user selects, I'd like either the ADMIN Navigation form or GUEST Navigation form to open. The difference between the two navigation forms is that the GUEST form will not contain the data entry forms the ADMIN form does.

    Here's my current code for the log-in form. How would I alter this so that the form being opened is a condition of whichever username is selected? Any help is much appreciated.

    Private Sub cmdEnter_Click()

    'Check to see if data entered into the UserName combo box

    If IsNull(Me.cmbUserName) Or Me.cmbUserName = "" Then
    MsgBox "You must select a User Name.", vbOKOnly, "Required Data"
    Me.cmbUserName.SetFocus
    Exit Sub
    End If

    'Check to see if data is entered into the password text box

    If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
    MsgBox "You must enter a valid password.", vbOKOnly, "Required Data"
    Me.txtPassword.SetFocus
    Exit Sub
    End If

    'Check value of password in the tblLogin to see if this matches value chosen in combo box

    If Me.txtPassword.Value = DLookup("Password", "tblUser", _
    "[UserID]=" & Me.cmbUserName.Value) Then

    MyEmpID = Me.cmbUserName.Value

    'Close logon form and open navigation screen

    DoCmd.Close acForm, "frmLogin", acSaveNo
    DoCmd.OpenForm "frmNavigationADMIN"

    Else
    MsgBox "Password invalid. Please try again.", vbOKOnly, _
    "Invalid Entry"
    Me.txtPassword.SetFocus
    End If



    End Sub

  2. #2
    TG_W is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Manvel, TX
    Posts
    299
    Try writing the log in information to a table that gets overwritten with each log in and set which form to open based on that information OR set it to open the form based on the user information in the frmLogin if it is correct.

  3. #3
    need_help12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    58
    TG_W, I agree with your solution, just not sure how to write the code for it.

  4. #4
    TG_W is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Manvel, TX
    Posts
    299
    Try switching around how you are validating - check the password first, if invalid, then MsgBox will appear. If it's good, then it will move on the If...Then where the password is correct. Here, you will nest another If...Then where the cmbusername value is = to either your GUEST or ADMIN ID code. For the purposes of the coding below, I used the GUEST code of "456123". This worked well for me. Coding is below.

    Code:
    If Me.txtPassword.Value <> DLookup("Password", "tblUser", "[ID]=" & Me.cmbusername.Value) Then
            MsgBox "Invalid password. Please try again.", vbOKOnly, "Required Data"
            Me.txtPassword.SetFocus
    End If
            
    If Me.txtPassword.Value = DLookup("Password", "tblUser", "[ID]=" & Me.cmbusername.Value) Then
            If Forms!frmLogin!cmbusername.Value Like "456123" Then
                DoCmd.OpenForm "frmNavigationGuest"
            Else
                DoCmd.OpenForm "frmNavigationAdmin"
            End If
        DoCmd.Close acForm, "frmLogin"
    End If
    Hope this helps.

  5. #5
    need_help12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    58
    TG_W,

    Thanks for the code. I've inserted this code, and while I'm not getting any errors, the cmd button is no longer functioning. What did I goof up?

    Private Sub cmdEnter_Click()

    'Check to see if data entered into the UserName combo box

    If IsNull(Me.cmbUserName) Or Me.cmbUserName = "" Then
    MsgBox "You must select a User Name.", vbOKOnly, "Required Data"
    Me.cmbUserName.SetFocus
    Exit Sub
    End If

    'Check to see if data is entered into the password text box

    If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
    MsgBox "You must enter a valid password.", vbOKOnly, "Required Data"
    Me.txtPassword.SetFocus
    Exit Sub
    End If

    'Check value of password in the tblLogin to see if this matches value chosen in combo box

    If Me.txtPassword.Value <> DLookup("Password", "tblUser", "[ID]=" & Me.cmbUserName.Value) Then
    MsgBox "Invalid password. Please try again.", vbOKOnly, "Required Data"
    Me.txtPassword.SetFocus
    End If

    If Me.txtPassword.Value = DLookup("Password", "tblUser", "[ID]=" & Me.cmbUserName.Value) Then
    If Forms!frmLogin!cmbUserName.Value Like "1" Then
    DoCmd.OpenForm "frmNavigationGUEST"
    Else
    DoCmd.OpenForm "frmNavigationADMIN"
    End If
    DoCmd.Close acForm, "frmLogin"
    End If

    End Sub

  6. #6
    need_help12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    58
    Disregard, user error! Resolved! And many, many thanks!

  7. #7
    TG_W is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Manvel, TX
    Posts
    299
    Lol! Ok. Glad I could help.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Code for Parameter Form
    By Huddle in forum Access
    Replies: 16
    Last Post: 02-27-2012, 01:39 PM
  2. VB Code to open a form?
    By HeadGasket in forum Access
    Replies: 1
    Last Post: 01-24-2012, 08:26 PM
  3. Cannot See The Form Code
    By howtechstuffworks in forum Forms
    Replies: 4
    Last Post: 02-25-2011, 02:11 PM
  4. Help !!!!! If, Then, Else Code in a form
    By melonwand in forum Forms
    Replies: 3
    Last Post: 02-24-2011, 09:49 AM
  5. Replies: 6
    Last Post: 11-05-2010, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums