View Single Post
  #7 (permalink)  
Old October 1st, 2003, 11:59 AM
Bob Bedell Bob Bedell is offline
Friend of Wrox
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

Hi Sam,

Here's the scoop on custom command bars:

First, the command bar object is not part of the Access object model, its part of the Office object model, so make sure you have a reference set to the most recent version of the "Microsoft Office x.0 Object library" installed on your system (probably 10.0).

Second, to create a custom command bar, you need code like the following:

Sub CreateCustomCommandBar()
   On Error Resume Next
   Dim cbr As CommandBar
   Dim btn As CommandBarButton
   Set cbr = CommandBars("Open A Form")
   If Err <> 0 Then
      Set cbr = CommandBars _
      .Add(Name:="Open a Form", Position:=msoBarTop)
   End If

   Set btn = cbr.Controls("Open a Form")

   If Err <> 0 Then
      ' Custom button doesn't already exist, so create it.
      Set btn = cbr.Controls.Add(msoControlButton, , , , True)
   End If
   With btn
      .Caption = "Open a Form"
      .BeginGroup = True
      .OnAction = "OpenAForm"
      .Style = msoButtonCaption
   End With
End Sub

After running this code, go to View -> Tools -> Open a Form and you'll be able to select your new comman bar and add it to your interface.

Third, to make the command bar do something when clicked, you need one more procedure. Notice above that the .OnAction property of the control button is set to "OpenAForm". This is a procedure call. Here's the procedure:

Sub OpenAForm()
    DoCmd.OpenForm "Form1"
End Sub

So, clicking on your new command bar button will open a form called Form1. Of course, Form1 needs to be created first.

That's about all there is to it. :)