Login Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • choudhmh
    New Member
    • Feb 2007
    • 14

    #1

    Login Code

    I have a problem; i know its a simple code but find it difficult to contruct the code.

    I have two fields username and password. I need some code, when a user clicks the button event in the form it checks against the username and password in the table to see if its correct; if correct it let you login else a message box should show either the username or password is wrong.

    Can someone give me a sniplet of the code that i could use.

    Thanks
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Originally posted by choudhmh
    I have a problem; i know its a simple code but find it difficult to contruct the code.

    I have two fields username and password. I need some code, when a user clicks the button event in the form it checks against the username and password in the table to see if its correct; if correct it let you login else a message box should show either the username or password is wrong.

    Can someone give me a sniplet of the code that i could use.

    Thanks
    Put the following in the button click event. Assuming textbox on form for username is txtUser and for password is txtPass.

    Code:
    Dim password As String
    
    If Not IsNull(Me.txtUser) Then
      password = DLookup("Password","tblUsers","[UserName]=" & Me.txtUser)
      If Not IsNull(password) Then
        If Not IsNull(Me.txtPass) Then
          If password = Me.txtPass
            'Code to Proceed goes here
          Else
            Msgbox "You have entered an incorrect password, please re-enter", vbOkOnly
            Exit Sub
          End If
        Else
          Msgbox "You must enter a password", vbOkOnly
          Exit Sub
        End If
      Else
        Msgbox "You have entered an incorrect username, please re-enter", vbOkOnly
      End if
    Else
      Msgbox "You must enter a user name.", vbOKOnly
      Exit Sub
    End If
    Mary

    Comment

    • choudhmh
      New Member
      • Feb 2007
      • 14

      #3
      I inserted this code in the button click event handler but keep on getting a command syntax error on If password = Me.txtPass.
      at present my table has three field loginid, username and password. Do i have to specify the table through a select statement.

      Private Sub go_Click()
      On Error GoTo Err_go_Click
      Screen.Previous Control.SetFocu s


      Dim password As String

      If Not IsNull(Me.txtUs er) Then
      password = DLookup("Passwo rd", "tblUsers", "[UserName]=" & Me.txtUser)
      If Not IsNull(password ) Then
      If Not IsNull(Me.txtPa ss) Then
      If password = Me.txtPass


      Else
      MsgBox "You have entered an incorrect password, please re-enter", vbOKOnly
      Exit Sub
      End If
      Else
      MsgBox "You must enter a password", vbOKOnly
      Exit Sub
      End If
      Else
      MsgBox "You have entered an incorrect username, please re-enter", vbOKOnly
      End If
      Else
      MsgBox "You must enter a user name.", vbOKOnly
      Exit Sub
      End If
      End Sub

      Originally posted by mmccarthy
      Put the following in the button click event. Assuming textbox on form for username is txtUser and for password is txtPass.

      Code:
      Dim password As String
      
      If Not IsNull(Me.txtUser) Then
         password = DLookup("Password","tblUsers","[UserName]=" & Me.txtUser)
         If Not IsNull(password) Then
      	  If Not IsNull(Me.txtPass) Then
      		 If password = Me.txtPass
      			'Code to Proceed goes here
      		 Else
      			Msgbox "You have entered an incorrect password, please re-enter", vbOkOnly
      			Exit Sub
      		 End If
      	  Else
      		 Msgbox "You must enter a password", vbOkOnly
      		 Exit Sub
      	  End If
         Else
      	  Msgbox "You have entered an incorrect username, please re-enter", vbOkOnly
         End if
      Else
         Msgbox "You must enter a user name.", vbOKOnly
         Exit Sub
      End If
      Mary

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        I was only using these as an example as I didn't know what the textboxes on the form were called. Replace txtUser and txtPass with the name of the textboxes on the login form.

        Code:
        Private Sub go_Click()
        On Error GoTo Err_go_Click
          Screen.PreviousControl.SetFocus
        Dim password As String
        
        If Not IsNull(Me.txtUser) Then
           password = DLookup("Password", "tblUsers", "[UserName]=" & Me.txtUser)
           If Not IsNull(password) Then
              If Not IsNull(Me.txtPass) Then
                 If password = Me.txtPass
                 
                 
                     Else
                    MsgBox "You have entered an incorrect password, please re-enter", vbOKOnly
                    Exit Sub
                 End If
              Else
                 MsgBox "You must enter a password", vbOKOnly
                 Exit Sub
              End If
           Else
              MsgBox "You have entered an incorrect username, please re-enter", vbOKOnly
           End If
        Else
           MsgBox "You must enter a user name.", vbOKOnly
           Exit Sub
        End If
        End Sub

        Comment

        • choudhmh
          New Member
          • Feb 2007
          • 14

          #5
          I tried what you showed me, i still get the same error;

          "compile error: Syntax Error" on If password = Me.txtPass

          Not sure whats the problem. I've named my textboxes as txtUser & txtPass - but still get the error. Try on your one - i'm giving up on this now.

          Thanks for your help

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Add 'Then' to the end of the line

            Code:
            If password = Me.txtPass Then

            Comment

            • choudhmh
              New Member
              • Feb 2007
              • 14

              #7
              now i get
              run time error '94'
              invalid use of null

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Code:
                Private Sub go_Click()
                On Error GoTo Err_go_Click
                  Screen.PreviousControl.SetFocus
                Dim password As String
                
                If Not IsNull(Me.txtUser) Then
                   password = DLookup("Password", "tblUsers", "[UserName]=" & Me.txtUser)
                   If Not IsNull(password) Then
                      If Not IsNull(Me.txtPass) Then
                         If password = Me.txtPass Then
                           '**************** Your code here***********
                         Else
                            MsgBox "You have entered an incorrect password, please re-enter", vbOKOnly
                            Exit Sub
                         End If
                      Else
                         MsgBox "You must enter a password", vbOKOnly
                         Exit Sub
                      End If
                   Else
                      MsgBox "You have entered an incorrect username, please re-enter", vbOKOnly
                   End If
                Else
                   MsgBox "You must enter a user name.", vbOKOnly
                   Exit Sub
                End If
                End Sub
                All possible nulls should be accounted for in the above code. What line exactly is your code stopping at.

                Mary

                Comment

                • choudhmh
                  New Member
                  • Feb 2007
                  • 14

                  #9
                  The Null is stopping at:
                  password = DLookup("Passwo rd", "tblUsers", "[UserName]=" & Me.txtUser).

                  Also if i keep the:

                  On Error GoTo Err_go_Click
                  then i get this error: Label not defined.

                  If i delete that line then the null error comes in.

                  Thanks for all your help

                  Comment

                  • MMcCarthy
                    Recognized Expert MVP
                    • Aug 2006
                    • 14387

                    #10
                    Try changing password variable to a variant as follows:

                    Code:
                    Private Sub go_Click()
                    On Error GoTo Err_go_Click
                      Screen.PreviousControl.SetFocus
                    Dim password As Variant
                    
                    If Not IsNull(Me.txtUser) Then
                       password = DLookup("Password", "tblUsers", "[UserName]=" & Me.txtUser)
                       If Not IsNull(password) Then
                          If Not IsNull(Me.txtPass) Then
                             If password = Me.txtPass Then
                               '**************** Your code here***********
                             Else
                                MsgBox "You have entered an incorrect password, please re-enter", vbOKOnly
                                Exit Sub
                             End If
                          Else
                             MsgBox "You must enter a password", vbOKOnly
                             Exit Sub
                          End If
                       Else
                          MsgBox "You have entered an incorrect username, please re-enter", vbOKOnly
                       End If
                    Else
                       MsgBox "You must enter a user name.", vbOKOnly
                       Exit Sub
                    End If
                    End Sub

                    Comment

                    Working...