Results 1 to 4 of 4
  1. #1
    Robert2150 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Location
    Sparks, Nv
    Posts
    102

    Code for a Login Form

    I am trying to replicate some code I found for a Login Form, the code is listed below.

    Can anyone see any obvious errors.

    I believe this code was created with Access 2010, I am using access 2016 on a 64 bit machine. I keep getting a message "Compile error: Syntax error for the line starting "If(IsNull (DLookup..........through the next line ending with Then"


    Option Compare Database

    Private Sub Command1_Click()

    If IsNull(Me.txtLoginID) Then
    MsgBox "Please enter LoginID", vbInformation, "LoginID Required"
    Me.txtLoginID.SetFocus
    ElseIf IsNull(Me.txtPassword) Then
    MsgBox "Please enter Password", vbInformation, "Password Required"
    Me.txtPassword.SetFocus
    Else
    'process the job
    If(IsNull(DLookup("[UserLogin]", "tblUser", "[UserLogin]='" & Me.txtLoginID.Value &
    "And Password='" & Me.txtPassword.Value &"'"))) Then[/U]
    MsgBox "Incorect LoginID or Password"
    Else
    'MsgBox "LoginID and Password Correct"
    DoCmd.OpenForm "Navigation Form"
    End If
    End If
    End Sub

    Any help would be appreciated, thank you.



    Robert2150

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    you dont need to store passords, just let windows authentication. They use their own user/password.


    Code:
    SUB btnLogin_Click()
    Dim sUser As String, sPass As String, sDom As String
    
    sUser = txtUser
    sPass = txtPass
    sDom = "CoDomain"
    
    if sUser = Dlookup("userid","tUsers","[userid]='" & sUser & "'") then
    If WindowsLogin(sUser, sPass, sDom) Then
       mbSafe = True
       DoCmd.OpenForm "frmMainMenu"
       DoCmd.OpenForm "frmLogin"
       DoCmd.Close
    Else
       MsgBox "LOGIN INCORRECT", vbCritical, "Bad userid or password"
    End If
    end if
    end sub
    
    '-----------------
    Public Function WindowsLogin(ByVal strUserName As String, ByVal strpassword As String, ByVal strDomain As String) As Boolean
    '-----------------
    
            'Authenticates user and password entered with Active Directory.
    
            On Error GoTo IncorrectPassword
            
            Dim oADsObject, oADsNamespace As Object
            Dim strADsPath As String
            
            strADsPath = "WinNT://" & strDomain
            Set oADsObject = GetObject(strADsPath)
            Set oADsNamespace = GetObject("WinNT:")
            Set oADsObject = oADsNamespace.OpenDSObject(strADsPath, strDomain & "\" & strUserName, strpassword, 0)
            
            WindowsLogin = True    'ACCESS GRANTED
            
    ExitSub:
            Exit Function
            
    IncorrectPassword:
            WindowsLogin = False   'ACCESS DENIED
            Resume ExitSub
    End Function




    First the db is on the network, so users already have to login to windows.
    In the db, I have a tUsers table, with userID, Name, and Level
    USERID, FIRSTN, LASTN, LEVEL
    bob12, bob smith, M
    pam4, pam jones,""
    xman, charles, xavier, F


    when user opens the db, the main menu form will open and grab the userID.
    Then lookup that persons rights in order to enable/disable controls.

    Code:
    public gvUserID 
    
    sub form_load()
    dim vLevel
    
       gvUserID =  Environ("Username")      'get userID,visible in all forms
    
    'get level from user table
       vLevel = Dlookup("[Level]","tUsers","[userID]='" & gvUserID & "'")
    
    'now, enable/disable items on form
       select case vLevel
             case "A"  'admin 
                 'all is enabled
    
             case "U"  'normal user
                 txtBox1.enabled = false
                 txtManager.enabled = false
    
             case "M"  'manager
                 txtBox1.enabled = false
       end select
    end sub

  3. #3
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,067
    Here's your code

    Code:
    Option Compare Database
    
    Private Sub Command1_Click()
    
    If IsNull(Me.txtLoginID) Then
       MsgBox "Please enter LoginID", vbInformation, "LoginID Required"
       Me.txtLoginID.SetFocus
    ElseIf IsNull(Me.txtPassword) Then
           MsgBox "Please enter Password", vbInformation, "Password Required"
           Me.txtPassword.SetFocus
        Else
           'process the job
           If(IsNull(DLookup("[UserLogin]", "tblUser", "[UserLogin]='" & Me.txtLoginID.Value & "And Password='" & Me.txtPassword.Value &"'"))) Then[/U]
                MsgBox "Incorect LoginID or Password"
          Else
              'MsgBox "LoginID and Password Correct"
              DoCmd.OpenForm "Navigation Form"
        End If
      End If
    End Sub
    What is the /U after the Then statement is that in your code or is it something put in when you posted here? if it's in your code take it out. Other than that nothing jumps out. But I agree with ranman256 use the windows authentication it's easier and more secure.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

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

Similar Threads

  1. Replies: 9
    Last Post: 06-02-2014, 08:10 AM
  2. HELP>Access 2010 User Login Form Code.
    By zaaimanm in forum Programming
    Replies: 5
    Last Post: 10-22-2012, 07:28 PM
  3. Login form code not working
    By dharmacloud in forum Forms
    Replies: 11
    Last Post: 08-22-2011, 03:08 PM
  4. Login Form Code
    By paddon in forum Programming
    Replies: 4
    Last Post: 04-08-2011, 06:48 PM

Tags for this Thread

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