Link to home
Start Free TrialLog in
Avatar of doug5516
doug5516

asked on

Programatically invoking a menu in Access

Is there a way to invoke a menu item programatically in Access?  If so, how would I do it?  Specifically, when the use clicks a button on an Access form, I want the export... menu under file to be called for a specific table.
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image


How about this:

DoMenuItem Method
See AlsoApplies ToExampleSpecificsDisplays the appropriate menu or toolbar command for Microsoft Access.

expression.DoMenuItem(MenuBar, MenuName, Command, Subcommand, Version)
expression    Required. An expression that returns one of the objects in the Applies To list.

MenuBar   Required Variant. Use the intrinsic constant acFormBar for the menu bar in Form view. For other views, use the number of the view in the menu bar argument list, as shown in the Macro window in previous versions of Microsoft Access (count down the list, starting from 0).

MenuName   Required Variant. You can use one of the following intrinsic constants.

Intrinsic constants:
acFile
acEditMenu
acRecordsMenu

You can use acRecordsMenu only for the Form view menu bar in Microsoft Access version 2.0 and Microsoft Access 95 databases. For other menus, use the number of the menu in the menu name argument list, as shown in the Macro window in previous versions of Microsoft Access (count down the list, starting from 0).
 

Command   Required Variant. You can use one of the following intrinsic constants.

Intrinsic constants:
acNew
acSaveForm
acSaveFormAs

acSaveRecord

acUndo

acCut

acCopy

acPaste

acDelete

acSelectRecord

acSelectAllRecords

acObject

acRefresh

For other commands, use the number of the command in the command argument list, as shown in the Macro window in previous versions of Microsoft Access (count down the list, starting from 0).
 

Subcommand   Optional Variant. You can use one of the following intrinsic constants.

Intrinsic constants:
acObjectVerb
acObjectUpdate
The acObjectVerb constant represents the first command on the submenu of the Object command on the Edit menu. The type of object determines the first command on the submenu. For example, this command is Edit for a Paintbrush object that can be edited.

For other commands on submenus, use the number of the subcommand in the subcommand argument list, as shown in the Macro window in previous versions of Microsoft Access (count down the list, starting from 0).
 

Version   Optional Variant. Use the intrinsic constant acMenuVer70 for code written for Microsoft Access 95 databases, the intrinsic constant acMenuVer20 for code written for Microsoft Access version 2.0 databases, and the intrinsic constant acMenuVer1X for code written for Microsoft Access version 1.x    databases. This argument is available only in Visual Basic.


Note   The default for this argument is acMenuVer1X, so that any code written for Microsoft Access version 1.x databases will run unchanged. If you're writing code for a Microsoft Access 95 or version 2.0 database and want to use the Microsoft Access 95 or version 2.0 menu commands with the DoMenuItem method, you must set this argument to acMenuVer70 or acMenuVer20.


Also, when you are counting down the lists for the Menu Bar, Menu Name, Command, and Subcommand action arguments in the Macro window to get the numbers to use for the arguments in the DoMenuItem method, you must use the Microsoft Access 95 lists if the Version    argument is acMenuVer70, the Microsoft Access version 2.0 lists if the Version argument is acMenuVer20, and the Microsoft Access version 1.x lists if Version is acMenuVer1X (or blank).


Note  There is no acMenuVer80 setting for this argument. You can't use the DoMenuItem method to display Microsoft Access 97 or Microsoft Access 2000 commands (although existing DoMenuItem methods in Visual Basic code will still work). Use the RunCommand method instead.


Remarks

Note  In Microsoft Access 97, the DoMenuItem method was replaced by the RunCommand method. The DoMenuItem method is included in this version of Microsoft Access only for compatibility with previous versions. When you run existing Visual Basic code containing a DoMenuItem method, Microsoft Access will display the appropriate menu or toolbar command for Microsoft Access 2000. However, unlike the DoMenuItem action in a macro, a DoMenuItem method in Visual Basic code isn't converted to a RunCommand method when you convert a database created in a previous version of Microsoft Access.


Some commands from previous versions of Microsoft Access aren't available in Microsoft Access 2000, and DoMenuItem methods that run these commands will cause an error when they're executed in Visual Basic. You must edit your Visual Basic code to replace or delete occurrences of such DoMenuItem methods.

The selections in the lists for the menu name, command, and subcommand action arguments in the Macro window depend on what you've selected for the previous arguments. You must use numbers or intrinsic constants that are appropriate for each MenuBar, MenuName, Command, and Subcommand argument.

If you leave the Subcommand argument blank but specify the Version argument, you must include the Subcommand argument's comma. If you leave the Subcommand and Version arguments blank, don't use a comma following the Command argument.

Example
The following example uses the DoMenuItem method to carry out the Paste command on the Edit menu in Form view in a Microsoft Access 95 database:

DoCmd.DoMenuItem acFormBar, acEditMenu, acPaste, , acMenuVer70
            
The next example carries out the Tile command on the Window menu in Form view in a Microsoft Access version 2.0 database:

DoCmd.DoMenuItem acFormBar, 4, 0, , acMenuVer20
            
Also ... another useful related link ... possibly for future reference:


http://www.jamiessoftware.tk/articles/menubars.html
       
Not sure it that article mentions it, but ... when you are in the Customize mode (dialog) ... if you hold down the Control key, you can drop and drag (ie, copy) menu items from one place to another ... handy for making a new menu that is similar another one.

mx
Avatar of doug5516
doug5516

ASKER

Sorry, this does not work.  It seems that the problem is that I can not get the Export... menu under File to activate.  After looking more closely at the problem, I am not sure it would work as I hoped anyway for the following reason:  when I manually click on Export..., the dialog that pops up is to Export the form not a table.  So it appears that even if I was able to programatically activate Export... from the form I would get the Export dialog for the form.  I am trying to find a way to call the Export... dialog box to export a table programatically.  I thought using the menus was the way to go, but I am no longer certain of that.
doug5516:,
<I want the export... menu under file to be called for a specific table.>
...Wrong approach perhaps.

If you want to "Export" a specified table then simply use on of the "TransferXXX" command in Access.
For example:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", "c:/Table1.xls", True
(This command "Exports" Table1 to an Excel 2000 file and includes the fieldnames on row 1)
DoCmd.TransferText acExportDelim, , "Table1", "c:\Table1.txt", True
(This command "Exports" Table1 to a Delimited text file and includes the fieldnames on row 1)

You don't really need to invoke a Generic "Export" menu.
As you can see even if you use Sendkeys:
SendKeys "%{F}{E}", False
... It tries to export the currently selected form.

Further proof is that if you click "Create Form in Design View", (Effectively NOT selecting any 1 Form)...Export in the file menu is greyed out!

HTH

Jeff Coachman
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I do not mind if boaq2000 gets split points are all the points.  None of the original solutions (those posted on 5.7.2007) were able to help me with what I was trying to accomplish.  Before boaq2000 posted his solution on 10.12.2007, I had already found a work around for what I needed.  However, this was my fault for not removing the question as soon as possible.  I just no longer want the question open.  To be honest, I did not even notice that boaq2000 post a reply as I had moved on to other projects well before then.  So his solution is fine.
doug5516,

Thanks.

Can you share with us what your workaround was?
(This way this Q has more value as a PAQ)

JeffCoachman