Results 1 to 11 of 11

Thread: Login VBA code

  1. #1
    Join Date
    Feb 2006
    Posts
    23

    Unanswered: Login VBA code

    Hi there,

    I have adapted a piece of code to allow users to login into the database. I have two types of users, that of student and that of admin. If the admin logs in I want them to be taken to a different form than ordinary users.

    Here is the code i have so far


    Code:
    Private Sub Login_Click()
    
    'Check to see if data is entered into the UserName combo box
    
    If IsNull(Me.cboUser) Or Me.cboUser = "" Then
    MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
    Me.cboUser.SetFocus
    Exit Sub
    End If
    
    'Check to see if data is entered into the password box
    
    If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
    MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
    Me.txtPassword.SetFocus
    Exit Sub
    End If
    
    'Check value of password in tblUsers to see if this
    'matches value chosen in combo box
    
    If Me.txtPassword.Value = DLookup("Password", "Users", "[UserID]=" & Me.cboUser.Value) Then
    
    UserID = Me.cboUser.Value
    
        If Me.cboUser = AdminID Then
            DoCmd.OpenForm "AdminForm"
        Else
         DoCmd.OpenForm "Studentform"
        End If - My attempt at trying to get them to go to different forms!
    
    'Close logon form and open splash screen
    
    'DoCmd.Close acForm, "frmLogn", acSaveNo
    'DoCmd.OpenForm "Questions"  Just a test to see if it would open a form
    
    
    
    
    Else
    MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
    Me.txtPassword.SetFocus
    End If
    
    'If User Enters incorrect password 3 times database will shutdown
    
    intLogonAttempts = intLogonAttempts + 1
    If intLogonAttempts > 3 Then
    MsgBox "You do not have access to this database.Please contact admin.", vbCritical, "Restricted Access!"
    Application.Quit
    End If
    
    
    End Sub

    Any help would be much appreciated

    Ross

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    So what's the problem?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Feb 2006
    Posts
    23
    Quote Originally Posted by pootle flump
    So what's the problem?

    haha oooops,

    well the problem is that I can get the code to go to one form, but it will always be the same, no matter who logs in admin or student. I want them to go to different forms.

    Code:
    If Me.txtPassword.Value = DLookup("Password", "Users", "[UserID]=" & Me.cboUser.Value) Then
    
    UserID = Me.cboUser.Value
    
        If Me.cboUser = AdminID Then
            DoCmd.OpenForm "AdminForm"
        Else
         DoCmd.OpenForm "Studentform"
        End If - My attempt at trying to get them to go to different forms!
    The above code doesnt work - I think the logic is correct but im not sure what syntax to use.. Basically if Admin logs in, load admin form, else load student form. Hope that explains better!

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Ok - the first thing I would suggest is to make a backup (copy) of your db. Then go to (in the VBA environment):
    Tools->Options->Require Variable Declaration

    Then go to each module and put
    Option Explicit
    at the very top of each one.
    Click Debug-> Compile YourProjectName

    Eeek - did it find errors?

    You have been creating variables willy nilly and this is a major cause of bugs. You need to explicitly declare all your variables (and their data types). Your problem is with:
    Code:
    If Me.cboUser = AdminID Then
    Where the heck did AdminID come from? It just appeared and Access let you because variable declaration is not requried (this is a v. v. poor default setting and was reversed for .NET btw).

    HTH
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Your new clean code shouild be something like the below. I have assumed that admin staff login with "AdminID".
    BTW - if this is a real student\ staff app then you had better learn a lot about Access security. Students will access data very very easily unless you really know how to lock down an access db.

    Code:
    Private Sub Login_Click()
     
    Dim UserID As String
     
    'Check to see if data is entered into the UserName combo box
     
    If IsNull(Me.cboUser) Or Me.cboUser = "" Then
    MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
    Me.cboUser.SetFocus
    Exit Sub
    End If
     
    'Check to see if data is entered into the password box
     
    If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
    MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
    Me.txtPassword.SetFocus
    Exit Sub
    End If
     
    'Check value of password in tblUsers to see if this
    'matches value chosen in combo box
     
    If Me.txtPassword.Value = DLookup("Password", "Users", "[UserID]=" & Me.cboUser.Value) Then
     
    UserID = Me.cboUser.Value
     
    If Me.cboUser = "AdminID" Then
    DoCmd.OpenForm "AdminForm"
    Else
    DoCmd.OpenForm "Studentform"
    End If 'My attempt at trying to get them to go to different forms!
     
    'Close logon form and open splash screen
     
    'DoCmd.Close acForm, "frmLogn", acSaveNo
    'DoCmd.OpenForm "Questions" ' Just a test to see if it would open a form
     
    Else
    MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
    Me.txtPassword.SetFocus
    End If
     
    'If User Enters incorrect password 3 times database will shutdown
     
    intLogonAttempts = intLogonAttempts + 1
    If intLogonAttempts > 3 Then
    MsgBox "You do not have access to this database.Please contact admin.", vbCritical, "Restricted Access!"
    Application.Quit
    End If
     
    End Sub
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Feb 2006
    Posts
    23
    Thanks for the advice about declaring variables, I have gone through and sorted all of that now. However with regards to the code, I am still having probls. If the user selects 'AdminID' in the combo box, this code is not recognising this, and is activating the else statement and loading the studentform, not the adminform. It must be te "If Me.cboUser = "AdminID" " line that is incorrect. Any further thoughts on this?

    Thanks once again

    Code:
    If Me.txtPassword.Value = DLookup("Password", "Users", "[UserID]=" & Me.cboUser.Value) Then
     
    UserID = Me.cboUser.Value
     
    If Me.cboUser = "AdminID" Then
    DoCmd.OpenForm "AdminForm"
    Else
    DoCmd.OpenForm "Studentform"
    End If 'My attempt at trying to get them to go to different forms!

  7. #7
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Add the line below:
    Code:
    UserID = Me.cboUser.Value
     
    Debug.Print Me.cboUser
    If Me.cboUser = "AdminID" Then
    The value will be printed to the immediate window (View-> Immediate Window).
    EDIT - please post the result

    btw1 - read up on Debugging VB (Breakpoints, stepping through code etc). A few hours of reading will save you hours of headaches.

    btw2 - what purpose is UserID serving?
    Last edited by pootle flump; 02-17-06 at 10:32.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  8. #8
    Join Date
    Feb 2006
    Posts
    23
    i added that line of code - it apears if admin is selected, the value returned is one, and if student is selected, the value returned is two.

    Im not entirely sure what value UserID is serving, i took this code from a tutorial, so some of it was blindly copied down.

    Thanks for your advice on debugging, I shall definately look into that

  9. #9
    Join Date
    Feb 2006
    Posts
    23
    Quote Originally Posted by rcwilkinson
    i added that line of code - it apears if admin is selected, the value returned is one, and if student is selected, the value returned is two.

    Im not entirely sure what value UserID is serving, i took this code from a tutorial, so some of it was blindly copied down.

    Thanks for your advice on debugging, I shall definately look into that
    solved it

    Code:
    If Me.cboUser.Value = 1 Then
    DoCmd.OpenForm "Questions"
    Else
    DoCmd.OpenForm "Topics"
    End If

  10. #10
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by rcwilkinson
    i added that line of code - it apears if admin is selected, the value returned is one, and if student is selected, the value returned is two.
    Ah - when you say one do you mean
    "one"
    (the word one as text)
    or
    1
    (the number 1 as a number)?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  11. #11
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by rcwilkinson
    Im not entirely sure what value UserID is serving, i took this code from a tutorial, so some of it was blindly copied down.
    The answer is nothing
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •