Changing drop-down menu at run time

A

Albert D. Kallal

I find that if you just create the menus using the UI..then you don't have
to boehter with code.

Futher, since you can specify a menu bar for each form..thent he "switcign"
of fomrs will autoamclity switch the avalingl menu otpons for you.

Also, you can most certainly run code for each menu item. You simply place
the name of the function in the on-action property of the menu item.

=MyPrintInvoice()


So, the above setting in the on-action propery will run your function in a
public model called MyPrintInvoice. Note that you need to slightly change
your coding habits..since your menu code now must PICK UP the curretly
active screen. So, my menu code OFTEN looks like:

Public Function MyPrintInvoice:
Dim tblgroupid As Long
Dim frmActive As Form

Set frmActive = Screen.ActiveForm

tblgroupid = frmActive.frmMainClientB.Form!id

If frmActive.InvoiceNumber = 0 Then
' no invoice number yet....gen a new one..
frmActive.InvoiceNumber = GetNextInvoiceNumber()
frmActive.Refresh ' write to disk

etc. etc. etc...

Note how in place of me.InviceNumber I can now use frmActive. So, you need
to as a general rule grab the active screen and shove it into a variable for
a lot of your screen code.

Here is some ideas on using menus to make things easy:

http://www.attcanada.net/~kallal.msn/Articles/UseAbility/UserFriendly.htm
 
J

Jonathan Blitz

I want to built the menu bar at run time.

I see there is the docmd.addmenu command but from what I can see it can only
add an entry that will run a macro.

I need to be able to add entries that can open forms directly or to run
functions from a module.

How do I do this?


--
Jonathan Blitz
AnyKey Limited
Israel

"When things seem bad
Don't worry and shout
Just count up the times
Things have worked themselves out."
 
C

Chris Nebinger

You can access existing menu options like:

CommandBars("UserMenu").Controls(3).Visible = True

I'm sure there is a way to create them, something like:

From the Access Help Files:


Set myBar = CommandBars("Custom")
Set myControl = myBar.Controls.Add
(Type:=msoControlComboBox, Id:=1)
With myControl
.AddItem "First Item", 1
.AddItem "Second Item", 2
.DropDownLines = 3
.DropDownWidth = 75
.ListHeaderCount = 0
End With



Chris Nebinger
 
J

Jonathan Blitz

My problem is that I need to change the contents of the menu bar according
to the user (was he/she is allowed to do) and the current state of the
application. For this reason I need it to be very dynamic.

--
Jonathan Blitz
AnyKey Limited
Israel

"When things seem bad
Don't worry and shout
Just count up the times
Things have worked themselves out."
 
J

Jonathan Blitz

I could just define all the menus and then hide parts as required.
This is very dirty.

It would be far nicer to be able to build them from scratch from the
contents of an SQL table.

This was I can change the menu structure without having to go into the
program code.

--
Jonathan Blitz
AnyKey Limited
Israel

"When things seem bad
Don't worry and shout
Just count up the times
Things have worked themselves out."
 
G

Graeme Richardson

1) Create a shortcut menu "MyShortcut"
2) Create a new entry on the shortcut with no text. You will change this as
required. Note the index number
3) Set the Shortcut Menu property on the controls (or form, I 'spose) you
want to use it on
4) Instantiate the class on opening the form (Module level declaration must
use withevents - see 6)
5) Trap the MouseDown event, looking for the right mouse button, and call
setShortcut method
6) Listen for the 'ShortcutClick' event

DISCLAIMER: The class is 'bound' to the Shortcut menu. Reusing the Shortcut
menu on other forms will raise the event in EVERY bound class.
No guarantee that the code will compile since I've edited it to remove some
of my 'quirks'. Hopefully straightforward to fix.

Two sections of code. Class code and form code.
' clsShortcut STARTS
Option Explicit

Private Const COMMANDBAR_NAME = "MyShortcut"
Private Const SHORTCUT_INDEX = 11 ' Index number of control mapped to
mctlShortcut

Private mcbarCommandBar As CommandBar
Private WithEvents mctlShortcut As CommandBarButton

Public Event ShortcutClick(ByVal vstrOption As String)

Private Sub Class_Initialize()
On Error GoTo Err_Class_Initialize

Set mcbarCommandBar = Access.CommandBars(COMMANDBAR_NAME)
Set mctlShortcut = mcbarCommandBar.Controls(SHORTCUT_INDEX)

Call setShortcut ' Reset the control from last use

Exit_Class_Initialize:

Exit Sub
Err_Class_Initialize:

If Not (Err.Source Like "*.*") Then Err.Source =
"clsShortcut.Class_Initialize"

Err.Raise Err.Number, Err.Source

End Sub

Public Sub setShortcut(Optional ByVal vvarCaption As Variant, Optional
vvarTag As Variant = "")
On Error GoTo Err_setShortcut

If IsMissing(vvarCaption) Then
mctlShortcut.Caption = "Open..."
mctlShortcut.Caption = ""
Else
mctlShortcut.Caption = vvarCaption
mctlShortcut.Tag = vvarTag
End If

mctlShortcut.Visible = mctlShortcut.Caption <> ""

Exit_setShortcut:

Exit Sub
Err_setShortcut:

If Not (Err.Source Like "*.*") Then Err.Source =
"clsShortcut.setShortcut"

Err.Raise Err.Number, Err.Source

End Sub

Private Sub mctlShortcut_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
On Error GoTo Err_mctlShortcut_Click

RaiseEvent ShortcutClick(mctlShortcut.Tag)

Exit_mctlShortcut_Click:

Exit Sub
Err_mctlShortcut_Click:

If Not (Err.Source Like "*.*") Then Err.Source =
"clsShortcut.mctlShortcut_Click"

Beep
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbExclamation,
Err.Source

Resume Exit_mctlShortcut_Click

End Sub
' clsShortcut STOPS

' form STARTS
Option Explicit

Private WithEvents mCShortcut As clsShortcut

Private Sub Form_Load()
On Error GoTo Err_Form_Load

Set mCShortcut = New clsShortcut

Exit_Form_Load:

Exit Sub
Err_Form_Load:

If Not (Err.Source Like "*.*") Then Err.Source = Me.Name & ".Form_Load"

Beep
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbExclamation,
Err.Source

Resume Exit_Form_Load

End Sub

Private Sub MyControl_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
On Error GoTo Err_FkFishSpecies_MouseDown

If Button = acRightButton Then

Call mCShortcut.setShortcut("Caption Text", "Tag Text")

End If

Exit_MyControl_MouseDown:

Exit Sub
Err_MyControl_MouseDown:

If Not (Err.Source Like "*.*") Then Err.Source = Me.Name &
".MyControl_MouseDown"

Beep
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbExclamation,
Err.Source

Resume Exit_cboFkFishSpecies_MouseDown

End Sub

Private Sub mCShortcut_ShortcutClick(ByVal vstrOption As String)
On Error GoTo Err_mCShortcut_ShortcutClick

Me.SetFocus

Select Case vstrOption
Case "Tag Text"
' Do something
Case Else
' Raise [programmer] error?
End Select

Exit_mCShortcut_ShortcutClick:

Exit Sub
Err_mCShortcut_ShortcutClick:

If Not (Err.Source Like "*.*") Then Err.Source = Me.Name &
".mCShortcut_ShortcutClick"

Beep
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbExclamation,
Err.Source

Resume Exit_mCShortcut_ShortcutClick

End Sub
' form code STOPS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top