Add Custom ToolBar in MSAccess 2007 using VB code

Add a custom Toolbar :

To add a custom toolbar we can first create the CommandToolBar and then add the controls to it. We can also define the method to be called on the click of the button by assigning the method name to the ‘OnAction’ property of the control.

Below is a simple subroutine which can be taken as an example on how to add a custom tool bar in MSAccess using VBA code.

Example :-

Private Sub CreateOpenToolBar()

‘Create the instance of the CommandBar & CommandBarControl Dim myCommandBar As CommandBar

Dim myCommandBarCt As CommandBarControl

‘On error go to the label PROC_ERR
On Error GoTo PROC_ERR

‘Add a tool bar to the Ribbon bar and assign it to the CommandBar object Set myCommandBar = CommandBars.Add(“TestinnTool”, msoBarTop, True, False) myCommandBar.Top = True

myCommandBar.Protection = msoBarNoMove

‘Add a button control to the command bar Set myCommandBarCt = myCommandBar.Controls.Add(Type:=msoControlButton) With myCommandBarCt .Caption = “Show Message” ‘Add a label the user to see .TooltipText = “Show Message On Click” ‘ToolTip text for the button .OnAction = “=ShowMessage()” ‘Add the name of the function to call

End With

PROC_EXIT:
Exit Sub

PROC_ERR: ‘Show this error message if any error occurs on executing the code MsgBox “An error has occured while attempting for creating toolbar” & _ vbNewLine & “Error details below:” & vbCrLf & vbCrLf & Err.Number & “: ” & Err.Description

Resume Next

Resume
End Sub

References :

Visual Basic for Applications Microsoft Office 12.0 Object Library

Microsoft Access 12.0 Object Library

150 150 Burnignorance | Where Minds Meet And Sparks Fly!