Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2010
    Posts
    12

    Unanswered: Using VB in Access

    Hello, I've learned how to created a login form through a youtube tutorial for my database. It has instructed me to create a button that has the following event procedure (using code builder).

    Username.SetFocus
    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"
    ElseIf Username = "staff2" and Password = "staff2" Then
    MsgBox "Access Granted", vbinformation, "CD Shop"
    MsgBox "Welcome", vbinformation, "CD Shop"
    DoCmd.Close
    DoCmd.OPenForm "F_Switchboard_MainMenu"
    ElseIf 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.

    However, when i tried to save it, a message box appears.
    Run-time error '3709':
    The search key was not found in any record.

    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
    Oct 2009
    Posts
    340
    ummm...well; you disable code by putting a: ' at the beginning of the line

    so you should cut this down into single elements and test.

    Username.SetFocus really should be me.Username.SetFocus and is a distraction at the moment so I would disable it.

    I prefer the Case method rather than ElseIf - but as a newbie I would even further suggest you chop those up into separate If/And - - it is much easier for you to understand.

    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"

    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"

    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

    this way you can disable each section cleanly - and test bit by bit......

  3. #3
    Join Date
    Oct 2009
    Posts
    340
    sorry forgot - each of those sections needs to have
    EndIf

  4. #4
    Join Date
    Jun 2010
    Posts
    12
    hey thank you. I've changed it. but then, now instead of run-time error '3709', it became

    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...'

    do you know what am i supposed to do?
    Last edited by Jesnie; 06-25-10 at 12:33.

  5. #5
    Join Date
    Oct 2009
    Posts
    340
    oh - you are closing the form you are working in.....

    take out all those docmd.close

    in the open of the new form it will open on top of the existing form....generally that is ok and actually preferable... but if not then put in one docmd.close at the bottom of the code after whichever form is going to open has opened....

    before that close command - you have to reset the focus to the current form.....so it won't think you are trying to close the form that was just opened...so reset the focus on your current form...
    me.focus
    docmd.close....

  6. #6
    Join Date
    Jun 2007
    Location
    Maitland NSW,Australia
    Posts
    388
    Why have you started another thread when you also have the same problem listed at http://www.dbforums.com/microsoft-access/1657915-login-form-vbulletin.html.

    Various memebrs has spent time replying to your previous thread, please read your previous thread for some advice.

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Quote Originally Posted by Poppa Smurf View Post
    Why have you started another thread when you also have the same problem listed at http://www.dbforums.com/microsoft-access/1657915-login-form-vbulletin.html.

    Various memebrs has spent time replying to your previous thread, please read your previous thread for some advice.
    ...good point, and well made
    I'd rather be riding on the Tiger 800 or the Norton

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
  •