Results 1 to 10 of 10
  1. #1
    cap.zadi is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2011
    Location
    KSA
    Posts
    481

    User Login Form remains Opened at backend!


    I am using this code for user loging but user form remains opened at back end.

    How this form can be closed automatically after successful login.

    Code:
    Private Sub cmdOk_Click()On Error GoTo Err_cmdOk_Click
    '-----------------------------------------------------------------------------------------------------------------------------
    ' This code is used to validate users found in the tblSecurity table. If the wrong user name or password is
    ' provided access is denied.
    ' Created by: Liam Sullivan
    ' Date Created: 08 Jan 2012
    ' Date Modified: 08 Jan 2012
    '-----------------------------------------------------------------------------------------------------------------------------
        Dim db As DAO.Database
        Dim rst As DAO.Recordset
        Dim rstV As Recordset
        Dim stDocName As String
        Dim stLinkCriteria As String
        
        Set db = CurrentDb()
        Set rst = db.OpenRecordset("tblSecurity", dbOpenDynaset)
        
        If Not IsNull(Me.txtUser) And Not IsNull(Me.txtPassword) Then
            rst.FindFirst "Password = '" & Me.txtPassword & "'" & " And UserID = '" & Me.txtUser & "'"
        
            If rst.NoMatch Then
                MsgBox "You entered an incorrect Username or Password." & Chr(13) & _
                "Please enter the correct Username and Password or " & Chr(13) & _
                "contact the Administrator for assistance.", vbOKOnly + vbCritical, "Logon Denied"
            ElseIf Me.txtPassword = "password" Then
                MsgBox "This is the first time you've logged in or your password has been reset." & Chr(13) & _
                "You must change your password before you can log in.", _
                vbOKOnly + vbExclamation, "Change Password"
                stDocName = "frmUserLogonNew"
                stLinkCriteria = "[UserID]=" & "'" & Me![txtUser] & "'"
                DoCmd.OpenForm stDocName, , , stLinkCriteria
            Else
                stDocName = "Mainmenu"
                DoCmd.OpenForm stDocName, , , stLinkCriteria
            End If
        Else
            MsgBox "You have left the Username and/or Password blank." & Chr(13) & _
            "Please enter the correct Username and Password or " & Chr(13) & _
            "contact the Adminstrator for assistance.", vbOKOnly + vbCritical, "Logon Denied"
        End If
        
        With User
            .AccessID = rst.Fields("AccessID")
            .ViewID = rst.Fields("ViewID")
            .Active = rst.Fields("Active")
            .Password = rst.Fields("Password")
            .SecurityID = rst.Fields("SecurityID")
            .UserID = rst.Fields("UserID")
        End With
        
        rst.Close
        
    Exit_cmdOk_Click:
        Exit Sub
    
    
    Err_cmdOk_Click:
        MsgBox Err.Description
        Resume Exit_cmdOk_Click
    
    
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Why is login form in backend?

    Review https://msdn.microsoft.com/VBA/Acces...-method-access
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    cap.zadi is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2011
    Location
    KSA
    Posts
    481
    Sorry, for not clear text. At back-end means at the backside of the other forms while working after login.

    Thanks for the example code.

    How it can be closed automatically after successful login?

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    I'd try this:
    Code:
    Exit_cmdOk_Click:
        docmd.close acform, me.name
        Exit Sub

  5. #5
    cap.zadi is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2011
    Location
    KSA
    Posts
    481
    Dear

    Thanks for the help

    It is working.

    Exit_cmdOk_Click:
    docmd.close acform, me.name
    Exit Sub

  6. #6
    cap.zadi is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2011
    Location
    KSA
    Posts
    481
    Hi

    I regret to inform you late which observed just now that the login form even will be closed and database remains opened if clicked on "Login" button even "no password" or "wrong password" entered.

    Kindly advise.

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Add the red code:
    Code:
        Dim db As DAO.Database    
        Dim rst As DAO.Recordset
        Dim rstV As Recordset
        Dim stDocName As String
        Dim stLinkCriteria As String
        
    If len(me.txtPassword & vbnullstring) = 0 then
        msgbox "Password cannot be blank!"
        exit sub
    end if
    
    If len(me.txtUser & vbnullstring) = 0 then
        msgbox "User cannot be blank!"
        exit sub
    end if
     
    Set db = CurrentDb()
        Set rst = db.OpenRecordset("tblSecurity", dbOpenDynaset)
    If Not IsNull(Me.txtUser) And Not IsNull(Me.txtPassword) Then
            rst.FindFirst "Password = '" & Me.txtPassword & "'" & " And UserID = '" & Me.txtUser & "'"
        
            If rst.NoMatch Then
                MsgBox "You entered an incorrect Username or Password." & Chr(13) & _
                "Please enter the correct Username and Password or " & Chr(13) & _
                "contact the Administrator for assistance.", vbOKOnly + vbCritical, "Logon Denied"
            ElseIf Me.txtPassword = "password" Then
    Or maybe this would be easier than the above:
    Code:
           Else
                stDocName = "Mainmenu"
                DoCmd.OpenForm stDocName, , , stLinkCriteria
            End If
        Else
            MsgBox "You have left the Username and/or Password blank." & Chr(13) & _
            "Please enter the correct Username and Password or " & Chr(13) & _
            "contact the Adminstrator for assistance.", vbOKOnly + vbCritical, "Logon Denied"
            exit sub
        End If
    Last edited by davegri; 06-29-2017 at 11:15 PM. Reason: alternative solution

  8. #8
    cap.zadi is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2011
    Location
    KSA
    Posts
    481
    Dear

    Thanks for the feedback.

    the only remaining part is now, it can be closed even on entering wrong password but cant be on null.

    any further advise?

  9. #9
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Re-read the original problem. Close the login form when appropriate.
    Go back to the original code and add the red lines. They will close the login form after opening the new form.
    Move the blue lines. They have to be executed before the form_Onclose event

    Code:
            If rst.NoMatch Then
                MsgBox "You entered an incorrect Username or Password." & Chr(13) & _
                "Please enter the correct Username and Password or " & Chr(13) & _
                "contact the Administrator for assistance.", vbOKOnly + vbCritical, "Logon Denied"
            ElseIf Me.txtPassword = "password" Then
                MsgBox "This is the first time you've logged in or your password has been reset." & Chr(13) & _
                "You must change your password before you can log in.", _
                vbOKOnly + vbExclamation, "Change Password"
                stDocName = "frmUserLogonNew"
                stLinkCriteria = "[UserID]=" & "'" & Me![txtUser] & "'"
                DoCmd.OpenForm stDocName, , , stLinkCriteria
                Set rst = Nothing
                DoCmd.close acform, Me.Name
            Else
                 With User
                     .AccessID = rst.Fields("AccessID")
                     .ViewID = rst.Fields("ViewID")
                     .Active = rst.Fields("Active")
                     .Password = rst.Fields("Password")
                     .SecurityID = rst.Fields("SecurityID")
                     .UserID = rst.Fields("UserID")            
                 End With
                 Set rst = nothing    
                 stDocName = "Mainmenu"
                 DoCmd.OpenForm stDocName, , , stLinkCriteria
                 DoCmd.close acForm, Me.Name
            End If
        Else
            MsgBox "You have left the Username and/or Password blank." & Chr(13) & _
    Last edited by davegri; 07-01-2017 at 04:45 PM.

  10. #10
    cap.zadi is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2011
    Location
    KSA
    Posts
    481
    Sir

    Its working fantastic now.

    Thanks for ur help.

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

Similar Threads

  1. Login user show on form
    By AccessThomas in forum Access
    Replies: 3
    Last Post: 05-11-2016, 12:14 PM
  2. Multi user login form
    By an1bone in forum Access
    Replies: 3
    Last Post: 08-03-2015, 12:58 PM
  3. Login Form. Passwords work for any User Name?
    By Mtyetti2 in forum Security
    Replies: 3
    Last Post: 01-01-2014, 04:23 AM
  4. Login Form/User ID Trouble.
    By Addanny in forum Forms
    Replies: 2
    Last Post: 07-03-2013, 10:25 AM
  5. User Login Form
    By glen in forum Forms
    Replies: 21
    Last Post: 09-17-2012, 09:09 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