CreateMenu()

A

Azonei

I have code to create menus using API function calls on an Access
Form
(not on the Access Menubar) that works great in Access 2003 and
earlier. When I use this code in Access 2007, I get squat. It's too
much code to post here, so in summary, here's what I'm doing:

I have a table that hold all of the information for my menus (MenuID,
Caption, Position, ParentID, and Action). In the Form_Load event I
call CreateMenu(), then loop through the items in my table and make a
call to CreatePopupMenu() if the item is a popup menu, or AppendMenu
()
if the item is a menu command item. Once the menu is built, I call
SetMenu() to attach it to the AccessForm. (Subclassing and use of
the
MSScriptControl object to run the code for each button rounds off the
method).


The process has always worked for me since I started using it in
Access 2000 until now. Can anyone shed any light on what changed in
Access 2007 to make these API calls apparently meaningless?


Thanks in advance


Azonei
 
M

Mark Andrews

You need to use the Ribbon for top level menus:
See my article on Ribbons to get started:
http://www.rptsoftware.com/help/microsoft_access_general/ribbon1.asp
and this site:
http://www.accessribbon.de/en/

for popup menus I found that code still works. Here's a ton of code I use
for one application:
You need a reference to the Microsoft Office library
HTH,
Mark
RPT Software
http://www.rptsoftware.com


Public Function CreatePopupMenu()
On Error GoTo Err_CreatePopupMenu
'Creates the popup menus that are used when right-clicking on
'an event in the calendar


Dim MenuName As String
Dim CBS As Office.CommandBars
Dim CB As CommandBar
Dim CBC As CommandBarControl
Dim CBB As CommandBarButton
Dim CBP As CommandBarPopup
Dim db As Database
Dim rs As DAO.Recordset
Dim sql As String

'****Create first menu (used when on right clicking ON an event)
MenuName = "RPTPopupOnEvent"

'delete menu if it already exists
If fIsCreated(MenuName) Then
Application.CommandBars(MenuName).Delete
End If

'create menu and appropriate commandbuttons
Set CB = Application.CommandBars.Add(MenuName, msoBarPopup, False,
False)

Set CBB = CB.Controls.Add(msoControlButton, , , , True)
CBB.Caption = "Cut"
CBB.Tag = "Cut"
CBB.FaceId = 21
CBB.OnAction = "=CutEvent()"

Set CBB = CB.Controls.Add(msoControlButton, , , , True)
CBB.Caption = "Copy"
CBB.Tag = "Copy"
CBB.FaceId = 19
CBB.OnAction = "=CopyEvent()"

Set CBB = CB.Controls.Add(msoControlButton, , , , True)
CBB.Caption = "Delete"
CBB.Tag = "Delete"
CBB.FaceId = 358
CBB.OnAction = "=CutEvent()"

Set CBB = CB.Controls.Add(msoControlButton, , , , True)
CBB.Caption = "Edit"
CBB.Tag = "Edit"
CBB.FaceId = 162
CBB.OnAction = "=EditEvent()"

Set CBP = CB.Controls.Add(msoControlPopup, , , , True)
CBP.Caption = "Status"
CBP.Tag = "Status"

sql = "SELECT EventStatus from tblEventStatus"
Set db = CurrentDb()
Set rs = db.OpenRecordset(sql, dbOpenDynaset)
If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
Do While Not rs.EOF
Set CBB = CBP.Controls.Add(msoControlButton, , , , True)
CBB.Caption = rs("EventStatus")
CBB.Tag = rs("EventStatus")
CBB.FaceId = 1
CBB.OnAction = "=AdjustPopupMenu(""" & rs("EventStatus") & """)"
rs.MoveNext
Loop
End If
rs.Close


'****Create second menu (used when on right clicking NOT ON an event)
MenuName = "RPTPopupOffEvent"

'delete menu if it already exists
If fIsCreated(MenuName) Then
Application.CommandBars(MenuName).Delete
End If

'create menu and appropriate commandbuttons
Set CB = Application.CommandBars.Add(MenuName, msoBarPopup, False,
False)

Set CBB = CB.Controls.Add(msoControlButton, , , , True)
CBB.Caption = "Paste"
CBB.Tag = "Paste"
CBB.FaceId = 22
CBB.OnAction = "=PasteEvent()"

Set CBB = CB.Controls.Add(msoControlButton, , , , True)
CBB.Caption = "Add..."
CBB.Tag = "Add..."
CBB.FaceId = 530
CBB.OnAction = "=AddEvent()"


Exit_CreatePopupMenu:
Set rs = Nothing
Set db = Nothing
Exit Function

Err_CreatePopupMenu:
MsgBox Err.Description
Resume Exit_CreatePopupMenu


End Function

Public Function AdjustPopupMenu(txtStatus As String)
'Adjusts which status values shows a check mark (only one can be checked)
Dim CBS As Office.CommandBars
Dim CB As CommandBar
Dim CBC As CommandBarControl
Dim CBB As CommandBarButton
Dim sql As String

Set CBS = Application.CommandBars
Set CB = CBS("RPTPopupOnEvent")
Set CBC = CB.Controls("Status")
For Each CBB In CBC.Controls
If (CBB.Caption = txtStatus) Then
CBB.State = msoButtonDown
Else
CBB.State = msoButtonUp
End If
Next
Set CBB = Nothing
Set CBC = Nothing
Set CB = Nothing
Set CBS = Nothing
End Function
Public Function AdjustStatusAfterPopupIfNeeded(EventID As String)
'Adjusts the collection if the user changed the status value using a popup
menu
Dim CBS As Office.CommandBars
Dim CB As CommandBar
Dim CBC As CommandBarControl
Dim CBB As CommandBarButton
Dim sql As String
Dim oldStatus As String
Dim newStatus As String

oldStatus = m_oEvents.Item(EventID).AppointmentStatus
newStatus = oldStatus

Set CBS = Application.CommandBars
Set CB = CBS("RPTPopupOnEvent")
Set CBC = CB.Controls("Status")
For Each CBB In CBC.Controls
If (CBB.State = msoButtonDown) Then
newStatus = CBB.Caption
End If
Next
Set CBB = Nothing
Set CBC = Nothing
Set CB = Nothing
Set CBS = Nothing

If (newStatus <> oldStatus) Then
m_oEvents.Item(EventID).AppointmentStatus = newStatus
m_oEvents.Item(EventID).RecordStatus = "SAVE"
End If
End Function

Public Function CreateTestBar()
Dim I As Integer
Dim x As Integer
Dim y As Integer

'i the popmenus
'x is the buttons
x = 1
I = 1
y = 1
strMenuName = "PopUpTest40833" 'Make sure you don't have a button named
"ButtonTest40833", this function would delete it.
If fIsCreated(strMenuName) Then
Application.CommandBars(strMenuName).Delete
End If

Set cmdNewMenu = Application.CommandBars.Add(strMenuName, msoBarPopup,
False, False)
For I = 1 To 100
Set cctlSubMenu = cmdNewMenu.Controls.Add(Type:=10)
With cctlSubMenu
.Caption = I
.BeginGroup = True
End With
y = x + 50
For x = x To (y)
Set CBarCtl =
cctlSubMenu.Controls.Add(Type:=msoControlButton)
With CBarCtl
.Caption = Chr(34) & x & Chr(34)
.FaceId = x
End With
Next
Next
cmdNewMenu.Visible = True

End Function
Function fIsCreated(strMenuName) As Boolean

Dim intNumberMenus As Integer
Dim I As Integer

intNumberMenus = Application.CommandBars.count

fIsCreated = False

For I = 1 To intNumberMenus
If Application.CommandBars(I).Name = strMenuName Then
fIsCreated = True
I = intNumberMenus
End If
Next

End Function
 
A

Azonei

You need to use the Ribbon for top level menus:
See my article on Ribbons to get started:http://www.rptsoftware.com/help/microsoft_access_general/ribbon1.asp
and this site:http://www.accessribbon.de/en/

for popup menus I found that code still works.  Here's a ton of code I use
for one application:
You need a reference to the Microsoft Office library
HTH,
Mark
RPT Softwarehttp://www.rptsoftware.com

...






- Show quoted text -


Thanks Mark, but it's not what I was looking for. I know how to add
menus to the Access Ribbonbar/Commandbar. The application I'm
building won't show Access at all and therefore the menu needs to be
on the actual form, not on the Office Ribbon/Commandbar. I can do
this in Access 2003, but the code no longer works in 2007. (I have
seen a lot of posts that say you can't add a menu directly to your
Access form, but yes you can...or at least you could.)

Thanks

Azonei
 
M

Mark Andrews

But in Access2003 and older isn't a menu on a form just a top level menu?
Travels with the form etc....
Why wouldn't you just use a ribbon menu for the form in Access2007? You can
have different ribbons for each form.

I guess you are saying you invented your own way of doing menus?
If you are not using the commandbar (via code) and don't want to use the
ribbon I would suggest you post the API calls
and perhaps someone can help.

I do most of my apps so either the user doesn't know it's in Access (use
runtime etc...) or at least hide the main database window/menus.

I would like to see your code (perhaps just to have it myself). I'll take a
look if you could zip it and send it over. Maybe I'll see what you need to
change to
have it work in Access2007.

Mark
RPT Software
http://www.rptsoftware.com





You need to use the Ribbon for top level menus:
See my article on Ribbons to get
started:http://www.rptsoftware.com/help/microsoft_access_general/ribbon1.asp
and this site:http://www.accessribbon.de/en/

for popup menus I found that code still works. Here's a ton of code I use
for one application:
You need a reference to the Microsoft Office library
HTH,
Mark
RPT Softwarehttp://www.rptsoftware.com

...






- Show quoted text -


Thanks Mark, but it's not what I was looking for. I know how to add
menus to the Access Ribbonbar/Commandbar. The application I'm
building won't show Access at all and therefore the menu needs to be
on the actual form, not on the Office Ribbon/Commandbar. I can do
this in Access 2003, but the code no longer works in 2007. (I have
seen a lot of posts that say you can't add a menu directly to your
Access form, but yes you can...or at least you could.)

Thanks

Azonei
 
L

Linq Adams via AccessMonster.com

Since we're talking about proven code that doesn't work when ported to Access
2007, the question has to be asked: Does any code run? Code in 2007 won't run
unless your database resides in a folder that has been declared a “trustedâ€
location.

To trust your folder, click:

Office Button (top left)
Access Options (bottom of dialog)
Trust Center (left)
Trust Center Settings (button)
Trusted Locations (left)
Add new location (button)
 
A

Azonei

Hi Mark

I think you're starting to get it...I AM using the API calls and yes,
I think this is a pretty unique solution. You can find a copy of the
code I've written at http://rdedwards.uuuq.com/portfolio/accessmenu.zip

When you open the database and run the main form, I think you'll see
more clearly what I'm tryin to do. Rather, if you open it with Access
2003 or earlier you'll see. If you use 2007 you won't and that is
precisely the issue I'm trying to solve.

Roy
 
A

Azonei

It's not a issue...the location of my database is in a trusted
folder. Code runs just fine...you just can't see the results of it.
 
A

Azonei

Okay...that link isn't working for some reason. If you want to see
the menu in action or the code, shoot me an email.
 
A

Azonei

But in Access2003 and older isn't a menu on a form just a top level menu?
Travels with the form etc....
Why wouldn't you just use a ribbon menu for the form in Access2007?  You can
have different ribbons for each form.

I guess you are saying you invented your own way of doing menus?
If you are not using the commandbar (via code) and don't want to use the
ribbon I would suggest you post the API calls
and perhaps someone can help.

I do most of my apps so either the user doesn't know it's in Access (use
runtime etc...) or at least hide the main database window/menus.

I would like to see your code (perhaps just to have it myself).  I'll take a
look if you could zip it and send it over.  Maybe I'll see what you need to
change to
have it work in Access2007.

Mark
RPT Softwarehttp://www.rptsoftware.com
I wouldn't go so far as to say I've "invented" a way of working with
menus - just figured out how to take advantage of what what
available.

It really isn't practical to post the entire code here - it's a lot of
code, it requires database tables and references and some cautions
(because of the subclassing involved). I have a zipped version of the
database at http://cid-7a5100c2be6268e0.skydrive.live.com/embedrow.aspx/.Public/AccessMenu.zip
If you run it with Access 2003 you'll see why using the Ribbon/
CommandBars isn't practical. If you run it with Access 2007 you'll
see - well, you'll nothing which is the nature of the problem.

Thanks
 

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