Results 1 to 6 of 6
  1. #1
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151

    How to cancel a loop message box

    I am trying to build a login screen where it prompts for a password only for a certain user. I have gotten all this to work except for one issue. On the loop, unless they enter the correct result, even if they click cancel or the X, they can not leave the loop. How would I modify the code to allow for the cancel to close the InputPw message box and go back to the login form.

    Thanks,

    Private Sub LoginUser_Click()


    un = LoginUser 'Combo drop box listing users

    If un = "sw" Then 'un is public variable for logged in user initials. If username matches prompt for password
    InputPw 'Public Function for input box to return password as string

    Do Until pw = "aa" 'expected password
    MsgBox "Incorrect Password. Please try again", vbOKCancel
    InputPw
    Loop 'keep prompting for password until correct
    DoCmd.Close 'if password is correct close login screen
    DoCmd.OpenForm "frmMenu", acNormal, , , , acNormal 'Open Main Menu Screen
    End If

    If un <> "sw" Then
    DoCmd.Close
    DoCmd.OpenForm "frmMenu", acNormal, , , , acNormal
    End If

    End Sub

  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,518
    You can test for a zero length string ("") which is what the input box will return if they click cancel.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    I figured that part out. But I wasn't sure where to place that code and if it is zero what code do I use to go out of the loop. If I put this code to test for null between lines 3 and 4, won't it still continue to line 4 and then loop back?

    1 Do Until pw = "aa"
    2 MsgBox "Incorrect Password. Please try again", vbOKCancel
    3 InputPw
    4 Loop

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Not if you use:

    Exit Do
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Thanks for your help!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Cancel Message or Replaced
    By azhar2006 in forum Macros
    Replies: 7
    Last Post: 04-12-2015, 03:20 PM
  2. Replies: 2
    Last Post: 03-19-2015, 01:33 PM
  3. Replies: 5
    Last Post: 05-27-2014, 02:44 PM
  4. Y N cancel message box
    By cantord in forum Forms
    Replies: 8
    Last Post: 11-28-2012, 02:20 PM
  5. Yes/No/Cancel Message Box
    By lynnmc26 in forum Access
    Replies: 0
    Last Post: 04-01-2011, 07:26 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