Categories
Microsoft Access

Custom Shortcut Menu

For most Access applications that are created for end users (as opposed to yourself), you don’t want the user to be able to go to Design View. This post specifically deals with the right click or shortcut menu.

If you right click on a form one of the menu items is Design View. You can show no menu at all on all forms in the database or on a form by form basis.

Remove shortcut menu completely

Access Options Dialogue Box

From File, Options, choose Current Database on the left, scroll down to the Ribbon and Toolbar Options and untick Allow Default Shortcut Menus.

Remove From Specific Forms

Change the Shortcut Menu property of the form to No.

Create a Custom Shortcut Menu

Add a Reference to the Microsoft Office 15.0 / 16.0 Object Library. (In the VBA Code window, choose Tools, References.)

Create a new Module (Insert, Module) and copy and paste this code. DO NOT paste into a form or report module.

Public Sub CreateRightClickBar()
'Run this once only.
 Dim cmbRC As CommandBar
 Dim cmbButtonCopy As CommandBarButton
 Dim cmbButtonCut As CommandBarButton
 Dim cmbButtonPaste As CommandBarButton
 Dim cmbButtonClose as CommandBarButton
 Dim strBarName As String
 strBarName = "CustomRightClick"
 On Error Resume Next
 CommandBars(strBarName).Delete
 On Error GoTo 0
 Set cmbRC = CommandBars.Add(strBarName, msoBarPopup, False)
 Set cmbButtonCopy = cmbRC.Controls.Add(msoControlButton, 21)
 Set cmbButtonCut = cmbRC.Controls.Add(msoControlButton, 19)
 Set cmbButtonPaste = cmbRC.Controls.Add(msoControlButton, 22)
Set cmbButtonClose = cmbRC.Controls.Add(msoControlButton, 1567)
 'Cleanup
 Set cmbRC = Nothing
 Set cmbButtonCopy = Nothing
 Set cmbButtonCut = Nothing
 Set cmbButtonPaste = Nothing
 Set cmbButtonClose = Nothing
End Sub

In the Immediate Window (Ctrl + G) type the name of the procedure (CreateRightClickBar) and press Enter.

Back in Access Options you will now see your new Shortcut Menu Bar is available.

Choosing your new shortcut menu bar

As previously, you could bypass this and apply the menu to each form individually via the form’s Shortcut Menu Bar property. (Ensure the Shortcut Menu property is set to Yes.)

Cleanup

You only need to run the code once to create the menu bar so you don’t need to save the code or the module. You should also remove the reference to the Microsoft Office 15.0 / 16.0 Object library.

Code Analysis

For most users, the ability to right click on a form and close it, rather than navigating to the cross to close the form is a great time saver. Copying and pasting is obviously so too. So in most cases this code is sufficient, but it’s possible to find the ID’s of other button in the Microsoft Office Library (21, 19, 22, 1567) to include in the menu – but that’s for another time!

Categories
Microsoft Access

SQL Server Write Conflict

When using a SQL Server backend, if you get a “Write Conflict” message when editing data, try adding a field of data type timestamp to the table. I often just name it “x”. (Remember to relink in Access).

Categories
Microsoft Access

Execute a Stored Procedure from Access

Create a pass through query in Access. Its content should call the sp like this:

Exec spCusInfo 65

The stored procedure will usually need a parameter, in the example, 65 could be the CusID. If sending a string, surround the parameter in single quotes. If there is more than 1 parameter, separate them with commas.

If you execute the query, you will always get the data for CusID=65. So in a button click event or an AfterUpdate event somewhere add VBA code like this:

Dim qry As QueryDef

Set qry = CurrentDb.QueryDefs("qrySpCusInfo")

qry.SQL = "exec spCusInfo " & Nz(Me.txtCusID, 0)

Me.Requery

See also https://oakdome.com/programming/MSAccess_StoredProcedureDataSource.php