Code Samples for Businesses, Schools & Developers

First Published 5 Nov 2023


Sometimes, you may wish to prevent a form or report being opened from the navigation pane. For example:
a)   to prevent unauthorised users opening the object
b)   where other code needs to run before the object is opened

Of course, you could just hide the object itself or the navigation pane but most Access users know how to reverse these changes.

However there are two simple methods of blocking forms and reports being opened from the navigation pane:



Method 1 - Using OpenArgs

Use this approach to restrict access to specified users. Add code to the On Open event of the form/report.

CODE:

Private Sub Form_Open(Cancel As Integer)

     If Nz(Me.OpenArgs, "") <> "isladogs" Then
           MsgBox "You do not have permission to open this form!", vbCritical, "Action Blocked"
           Cancel = True
      End If

End Sub



When the object is clicked in the navigation pane, this message appears but the form doesn't open:

No Permission Message

If the user enters DoCmd.OpenForm "YourFormName" in the VBE Immediate Pane, the same message appears followed by an Access error message:

Error 2501 Message

To open this successfully, you must use the specified OpenArgs:

DoCmd.OpenForm "YourFormName",  ,  ,  ,  ,  , "isladogs"




Method 2 - Using Application.CurrentObjectName property

Use this approach to completely block all access from the navigation pane or the Immediate pane. Add code to the On Open event of the form/report.

CODE:

Private Sub Form_Open(Cancel As Integer)

On Error GoTo Err_Handler

      'Uses the Application.CurrentObjectName property
      'https://msdn.microsoft.com/en-us/library/office/ff196795.aspx

      'block opening item from nav pane
      If Application.CurrentObjectName = Me.Name Then Cancel = True

Exit_Handler:
      Exit Sub

Err_Handler:
      If Err = 2501 Then Exit Sub

      MsgBox "Error " & Err.Number & " in Form_Open procedure : " & vbCrLf & Err.Description
      Resume Exit_Handler

End Sub



I use the second approach in several of my apps including the ShiftBypassQuit example app from my article: Manage the Shift Bypass Property



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 5 Nov 2023



Return to Code Samples Page




Return to Top