Results 1 to 5 of 5
  1. #1
    tharless is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    14

    Make vba reference a table

    Hello, I am trying to set up username and passwords in my data base(access 2007), I have it set up to accept usernames and passwords using vba, but I had to write the username and password into the code. I would like for it to look at a table I created called Users and determine if the username and password are valid. This is what I have now,
    If Username = "Tim" And Password = "Rocks" Then
    MsgBox "Access Granted", vbInformation, "ViciLife"
    MsgBox "Welcome", vbInformation, "ViciLIfe"
    DoCmd.Close
    DoCmd.OpenForm "Switchboard"
    DoCmd.ShowToolbar "Ribbon", acToolbarYes
    DoCmd.SelectObject acForm, NavPane, True
    ElseIf Username = "User" And Password = "Password" Then
    MsgBox "Access Granted", vbInformation, "ViciLife"
    MsgBox "Welcome", vbInformation, "ViciLIfe"
    DoCmd.Close
    DoCmd.OpenForm "Switchboard"
    DoCmd.LockNavigationPane True
    DoCmd.ShowToolbar "Ribbon", acToolbarNo
    DoCmd.SelectObject acForm, NavPane, True
    RunCommand acCmdWindowHide



    Else: MsgBox "Please re-enter your Username and Password."

    End If
    This is so if tim enters his user name and password the database opens with admin privileges, if the default "user" and "password" are entered the ribbon and nav pane are hidden. I would like to set it up so that instead of the code actually saying username = "tim" and password = "rocks" it would reference the username and password field of my Users table, and if tim and rocks is in the users table( or what is entered in the text boxes) it would perform the rest of the code. Any help would be greatly appreciated.
    Also this code is in the button click event of a form with 2 textboxes named "Username" and "Password"

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If you search here on "login" or "password" you should find numerous examples of this type of thing. It's typically done with either a DLookup() or a recordset to get the values from the table. Here's info on DLookup() syntax if you go that way:

    DLookup Usage Samples
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    slave138's Avatar
    slave138 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    WI
    Posts
    233
    First, this method is far from fool-proof as users can get right into the table without any password by opening the DB while holding down SHIFT, but assuming that isn't important to you, I would suggest using the DLookup function:

    If Me.txtPassword.Value = DLookup("PWORD", "tblUsers", "UserID=" & Chr(34) & Me.txtUserID.Value & Chr(34)) Then
    ...
    Else
    ...
    End If

  4. #4
    tharless is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    14
    Quote Originally Posted by slave138 View Post
    First, this method is far from fool-proof as users can get right into the table without any password by opening the DB while holding down SHIFT, but I assuming that isn't important to you, I would suggest using the DLookup function:
    The people I am creating the db for do not know enough about access to know about the shift click method, basically just giving them the impression that there is nothing else there but what they see will make it to where they think that is all there is. I'm not going for complete security just the illusion lol, thank you for the code it worked perfect the way I wanted it to, and you made it look too easy, now I feel dumb lol. Thanks again.

  5. #5
    slave138's Avatar
    slave138 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    WI
    Posts
    233
    Happy to help


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

Similar Threads

  1. Change linked table reference automatically
    By kjuelfs in forum Access
    Replies: 1
    Last Post: 07-20-2010, 09:14 AM
  2. Reference table
    By Newcomer in forum Access
    Replies: 6
    Last Post: 06-03-2010, 03:08 PM
  3. Replies: 2
    Last Post: 05-09-2010, 04:10 AM
  4. Replies: 2
    Last Post: 10-27-2009, 07:09 AM
  5. Replies: 0
    Last Post: 08-01-2009, 12:43 PM

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