Results 1 to 14 of 14

Thread: Login form VB

  1. #1
    Join Date
    Jun 2010
    Posts
    12

    Unanswered: Login form VB

    Hello, I've created a login form for my database. I also created a button that has the following event procedure (using code builder).

    My friend suggested me to use this code,

    If Username = "staff1" And Password = "staff1" Then
    MsgBox "Access Granted", vbInformation, "CD Shop"
    MsgBox "Welcome", vbInformation, "CD Shop"
    DoCmd.Close
    DoCmd.OpenForm "F_Switchboard_MainMenu"
    End If

    If Username = "staff2" and Password = "staff2" Then
    MsgBox "Access Granted", vbinformation, "CD Shop"
    MsgBox "Welcome", vbinformation, "CD Shop"
    DoCmd.Close
    DoCmd.OPenForm "F_Switchboard_MainMenu"
    End If

    If Username = "manager1" and Password = "manager1" Then
    MsgBox "Welcome, Manager", vbinformation, "Manager Area"
    MsgBox "Please Exercise Caution When Altering Back End", vbinformation, "Manager Area"
    DoCmd.Close
    DoCmd.OPenForm "F_Switchboard_MainMenu"
    Else
    MsgBox "Please re-enter your Username and Password."
    End If

    The button runs perfectly at first.

    However, when i tried to save it, a message box appears.
    Run-time error '2467'
    The expression you entered refers to an object that is closed or doesn't exist.

    And if I click the debug button,
    the Visual Basic highlights the first line of the second section. Oh and if I hover over that line, a bubble pops up saying ' Password = < The expression you entered refers to an object that is closed or...'

    The next time I opened the database and tried to log in, the button no longer works anymore.

    If you dont mind, can you kindly please explain to me why does this happen and what can i do to solve this error?

    I'm just a beginner in using Ms Access and Visual Basic, so much explanation is needed.

    Please help me. Thank you.

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    You should either use Select/Case or ElseIf. As soon as one condition is met, you close the form but continue testing. You want to end testing after a condition is met. You could also add an Exit Sub at the end of each, but the cleaner method is one of the above. They will naturally guide the code where it needs to go.
    Paul

  3. #3
    Join Date
    Jun 2007
    Location
    Maitland NSW,Australia
    Posts
    388
    Please post a copy of your database and I will have a look at your problem. I noticed that you are opening the same Main menu for each user in your code, should the Manager have a separate Main menu?

  4. #4
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    By the way, I'd add that your method is more "hard-coded" than I would use in a production application. I'd have a user table. You have the user enter their name and password, then verify the password against the table. Go on if it matches, otherwise not. That's much more dynamic in the long run.
    Paul

  5. #5
    Join Date
    May 2010
    Posts
    601
    Quote Originally Posted by Jesnie View Post
    Hello, I've created a login form for my database. I also created a button that has the following event procedure (using code builder).

    My friend suggested me to use this code,

    If Username = "staff1" And Password = "staff1" Then
    MsgBox "Access Granted", vbInformation, "CD Shop"
    MsgBox "Welcome", vbInformation, "CD Shop"
    DoCmd.Close
    DoCmd.OpenForm "F_Switchboard_MainMenu"
    End If

    If Username = "staff2" and Password = "staff2" Then
    MsgBox "Access Granted", vbinformation, "CD Shop"
    MsgBox "Welcome", vbinformation, "CD Shop"
    DoCmd.Close
    DoCmd.OPenForm "F_Switchboard_MainMenu"
    End If

    If Username = "manager1" and Password = "manager1" Then
    MsgBox "Welcome, Manager", vbinformation, "Manager Area"
    MsgBox "Please Exercise Caution When Altering Back End", vbinformation, "Manager Area"
    DoCmd.Close
    DoCmd.OPenForm "F_Switchboard_MainMenu"
    Else
    MsgBox "Please re-enter your Username and Password."
    End If

    The button runs perfectly at first.

    However, when i tried to save it, a message box appears.
    Run-time error '2467'
    The expression you entered refers to an object that is closed or doesn't exist.

    And if I click the debug button,
    the Visual Basic highlights the first line of the second section. Oh and if I hover over that line, a bubble pops up saying ' Password = < The expression you entered refers to an object that is closed or...'

    The next time I opened the database and tried to log in, the button no longer works anymore.

    If you dont mind, can you kindly please explain to me why does this happen and what can i do to solve this error?

    I'm just a beginner in using Ms Access and Visual Basic, so much explanation is needed.

    Please help me. Thank you.
    Try this VBA code (Access uses VBA)

    Code:
    If Me.Username = "staff1" And Me.Password = "staff1" Then
       MsgBox "Access Granted", vbInformation, "CD Shop"
       MsgBox "Welcome", vbInformation, "CD Shop"
       DoCmd.Close acForm, Me.Name
       DoCmd.OpenForm "F_Switchboard_MainMenu"
    
      Exit Sub
    
    End If
    
    If Me.Username = "staff2" and Me.Password = "staff2" Then
       MsgBox "Access Granted", vbinformation, "CD Shop"
       MsgBox "Welcome", vbinformation, "CD Shop"
       DoCmd.Close  acForm, Me.Name
       DoCmd.OPenForm "F_Switchboard_MainMenu"
    
      Exit Sub
    
    End If
    
    If Me.Username = "manager1" and Me.Password = "manager1" Then
       MsgBox "Welcome, Manager", vbinformation, "Manager Area"
       MsgBox "Please Exercise Caution When Altering Back End", vbinformation, "Manager Area"
       DoCmd.Close  acForm, Me.Name
       DoCmd.OPenForm "F_Switchboard_MainMenu"
    
      Exit Sub
    End If
    
    
    MsgBox "Please re-enter your Username and Password."
    
    
    End Sub
    I would avoid the use of reserved words for you own object names.

    It would make the code a whole lot easier if you put the users in a table and looked up the password. This will allow as many users as need. You will be able to add users or change passwords without modifying the VBA code.

    See: Boiler Plate Database
    Boyd Trimmell aka HiTechCoach HiTechCoach.com (free access stuff)
    Microsoft MVP - Access Expert
    BPM/Accounting Systems/Inventory Control/CRM
    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I've never understood why developers want to roll their own security / logon, when there are tools that will do that for you. especially doing it in code. you have a security vulnerability, you have a maintenance issue. thats why I'd ALWAYS advocate using the network logon as the mechanism to authenticate the user AND add that network logon to your workgroup. so when you need to check if a user is authenticated you read the workgroup file and check the permissions for that user.

    as some advertisers would say

    ...simples

    the real advantages
    maintaining the user permissions becomes entirely data driven so there is no development time required each time a user changes roles or permissions. if a user leaves then their logon disappears in a puff of smoke (or at least once the network trolls pull their finger out)
    its pretty robust, theres no problem of multiple people using the same logon (unless your network allows it)
    the user doesn't have to remember yet another effing logon and password
    the password changes are managed by the network trolls (so there is less fallback on you if the password is compromised, after all if the network trolls did their job there would have been no vulnerability
    ..and so on
    ...see the code bank for examples
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Jun 2010
    Posts
    12
    Quote Originally Posted by pbaldy View Post
    By the way, I'd add that your method is more "hard-coded" than I would use in a production application. I'd have a user table. You have the user enter their name and password, then verify the password against the table. Go on if it matches, otherwise not. That's much more dynamic in the long run.
    As in adding a new table to the database?

    So I just create a new table with two fields, namely Username and Password?
    How do i run a verification check upon clicking that button?

  8. #8
    Join Date
    May 2010
    Posts
    601
    Might want to Check out the Boiler Plate link in post #5.
    Boyd Trimmell aka HiTechCoach HiTechCoach.com (free access stuff)
    Microsoft MVP - Access Expert
    BPM/Accounting Systems/Inventory Control/CRM
    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

  9. #9
    Join Date
    Jun 2010
    Posts
    12
    I've checked that out.

    I find it really complicated and I dont know how to incorporate that into my system
    Last edited by Jesnie; 06-26-10 at 12:18.

  10. #10
    Join Date
    Jun 2010
    Posts
    12
    Quote Originally Posted by HiTechCoach View Post
    Try this VBA code (Access uses VBA)

    Code:
    If Me.Username = "staff1" And Me.Password = "staff1" Then
       MsgBox "Access Granted", vbInformation, "CD Shop"
       MsgBox "Welcome", vbInformation, "CD Shop"
       DoCmd.Close acForm, Me.Name
       DoCmd.OpenForm "F_Switchboard_MainMenu"
    
      Exit Sub
    
    End If
    
    If Me.Username = "staff2" and Me.Password = "staff2" Then
       MsgBox "Access Granted", vbinformation, "CD Shop"
       MsgBox "Welcome", vbinformation, "CD Shop"
       DoCmd.Close  acForm, Me.Name
       DoCmd.OPenForm "F_Switchboard_MainMenu"
    
      Exit Sub
    
    End If
    
    If Me.Username = "manager1" and Me.Password = "manager1" Then
       MsgBox "Welcome, Manager", vbinformation, "Manager Area"
       MsgBox "Please Exercise Caution When Altering Back End", vbinformation, "Manager Area"
       DoCmd.Close  acForm, Me.Name
       DoCmd.OPenForm "F_Switchboard_MainMenu"
    
      Exit Sub
    End If
    
    
    MsgBox "Please re-enter your Username and Password."
    
    
    End Sub
    I would avoid the use of reserved words for you own object names.

    It would make the code a whole lot easier if you put the users in a table and looked up the password. This will allow as many users as need. You will be able to add users or change passwords without modifying the VBA code.

    See: Boiler Plate Database
    I've tried that out. but it doesnt work. Am I required to change the names of any of my textbox?

  11. #11
    Join Date
    May 2010
    Posts
    601
    Quote Originally Posted by Jesnie View Post
    I've tried that out. but it doesnt work. Am I required to change the names of any of my textbox?
    Please post the VBA code you are using. Be sure to include all the code for the event/procedure/function.
    Boyd Trimmell aka HiTechCoach HiTechCoach.com (free access stuff)
    Microsoft MVP - Access Expert
    BPM/Accounting Systems/Inventory Control/CRM
    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

  12. #12
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Quote Originally Posted by Jesnie View Post
    As in adding a new table to the database?

    So I just create a new table with two fields, namely Username and Password?
    How do i run a verification check upon clicking that button?
    Yes; you can use a DCount() to compare the password for the input user against the table (a count of 0 indicates no match).
    Paul

  13. #13
    Join Date
    Jun 2007
    Location
    Maitland NSW,Australia
    Posts
    388
    Have a look at a Login database that I uploaded at another forum, it uses Dcount and Dlookup to validate the user's access etc.
    See message 44 of this for the demo version. I suggest you also start at the beginning of thread for various explanations etc. How to create user login form (MS Access 2000 - 2003) - Page 3 - Database Journal Forums. This is a sample database that displays a switchboard for level of access the module mod_display_menu performs the validation by check the user's id and password in the table.

  14. #14
    Join Date
    Jun 2010
    Posts
    12
    ok thank you!

Posting Permissions

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