Results 1 to 11 of 11
  1. #1
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91

    How to stop error on Cancel event

    I have a global login function on several Form Open events that require the input of a password and if the password is incorrect, it will Cancel the open event. The function works fine but if the event is canceled, it throws an error; runtime '2501'. How do I prevent this error from occurring? I've tried the On Error GoTo Next but that didn't prevent it. Any help would be greatly appreciated



    Code:
    Public Function IsAdmin() As Boolean
    
    
        Dim AdminPass As String
        Dim MyPass As String
        
        MyPass = InputBox("Admin Level Security" & vbNewLine & "Enter Password")
        AdminPass = DLookup("AdminPassword", "SettingT")
        
        IsAdmin = False
        
        If AdminPass = MyPass Then
            IsAdmin = True
        Else
            IsAdmin = False
            MsgBox "Incorrect Password!", vbExclamation
        End If
    
    
    End Function
    Code:
    Private Sub Form_Open(Cancel As Integer)
    
    
        If IsAdmin = False Then
            Cancel = True
            Exit Sub
        End If
        
    End Sub

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    no idea what runtime error 2501 is, I just can't hold the thousands of codes in my head. So if the following doesn't answer your question, please provide the error description.

    your code does not close the form so your error is probably happening further down the line in the load or current event, suggest you need the docmd.close function

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Modify your code to include an error handler to trap and ignore the error, which is just an informational 'error'.

    Code:
    Private Sub Form_Open(Cancel As Integer)
        On Error GoTo Error_Handler
        If IsAdmin = False Then
            Cancel = True
            Exit Sub
        End If
    Error_Handler_Exit:
        On Error Resume Next
        Exit Sub
    Error_Handler:
        Select Case Err
            Case 2501   'ignore
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ")", vbExclamation, "Error in Sub Form_Open of Form_GetUserNameClientOnly"
        End Select
        Resume Error_Handler_Exit
    End Sub
    Change the red to your form name...
    Last edited by davegri; 05-24-2022 at 07:39 AM. Reason: added red note

  4. #4
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    I tried both examples and still getting the error

    Run-Time Error '2501':
    The OpenForm action was canceled

  5. #5
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    Ajax, you were on the right track. I found that instead of Cancel=True, if I use DoCmd.Close I don't get the error. I'd still like to know why Cancel returns an error though

  6. #6
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Quote Originally Posted by Glenley View Post
    I tried both examples and still getting the error

    Run-Time Error '2501':
    The OpenForm action was canceled

    Then the error is not caused by the form_open event being cancelled.
    When the error occurs and you click Debug, what line of code is highlighted?

  7. #7
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    On Debug the DoCmd.OpenForm is highlighted in the command button click event

  8. #8
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Quote Originally Posted by Glenley View Post
    On Debug the DoCmd.OpenForm is highlighted in the command button click event
    Then that event is the procedure that needs the error handler code. That event code was not posted.

  9. #9
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    This is the event

    Code:
    DoCmd.OpenForm "AdminF"

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I'd still like to know why Cancel returns an error though
    It's not really an error - it's information as noted above. Since flow will return to the calling sub (which you didn't post) when downstream procedures are complete, then IF the calling code depended on the form being open, all sorts of errors and problems could arise. So the calling code is 'notified' that the form isn't open thus the message. I simply trap the error number and move on so I agree with that suggestion FWIW.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    Okay I got it. I didn't post a line of code. My apologies. Thank you for the help everybody. Ajax, thank you for pointing me in the right direction. I really do appreciate it

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

Similar Threads

  1. Replies: 4
    Last Post: 06-07-2021, 02:41 PM
  2. Replies: 4
    Last Post: 02-16-2014, 06:30 PM
  3. Replies: 0
    Last Post: 03-11-2012, 09:19 AM
  4. Cancel form navigation event
    By tuna in forum Forms
    Replies: 3
    Last Post: 08-15-2010, 01:46 PM
  5. Cancel Selection Event for ListBox
    By is49460 in forum Forms
    Replies: 2
    Last Post: 08-04-2010, 05:53 PM

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
  •  
Other Forums: Microsoft Office Forums