Call an embedded macro from VBA in access - ms-access

I have a macro assigned to the onClick event of a button in a form. How can I call this macro programmatically?
I tried
btnName_Click
But this does not work since there is no function called btnName_Click() ... obviously :)
I can access the onClick Member via Me.btnNewRecord.OnClick but don't see a way to run the macro.

After extensive searching, I do not believe it is possible to reference the embedded macro, and run it. You can view the XML of the macro, but I know of no way of running it or even accessing it beyond it's XML stored as a string. A possible work around would be to convert all macros to VBA. To do this:
Open the form in design view.
Click Convert Form's Macros to Visual Basic
now you should be able to call the button's code with btnName_Click as you showed in your question. Obviously if you did this, you would sacrifice the advantage of using macros (i.e. limited functionality without the user needing to trust your database).
Original Answer, which doesn't apply to Embedded Macros:
Use DoCmd.RunMacro
Example:
Docmd.RunMacro(macroname)
where macroname is a string representing the name of the macro.

Related

Macro Design - Calling Function

I have a question about the Macro Design in a table for a "After Update" function. In one database that one of my past employees built the "SetField" value as a function call. One of the functions it calls is called GetUserNAme() it is buried in another bas_AuditLog macro, but in a DB that I am building, it doesn't work, even though I thought I had all of the information copied and correct. I have attached an image here that might show my issue. Notice the red exclamation mark.
Image of Macro Builder with error
Perhaps one of you smart people can help me look at an area that in my DB that may have the missing link.
Thanks.
Ok, so in the other (working) applcaiton, there is a going to be a public function called GetUserName(), and it is a VBA function.
So, in that working applcation, you can hit ctrl-g (get to debug window), and then type in GetUserName and then hit shift f2. Your code editor should now jump to that VBA function. You need to copy that code to your new applcation (place it in a plane jane standard code module (not a forms module, and not a class module). and it might very well also use a api call.
So, when you copy over that code. Test and make sure the VBA code works. In most cases in the access debug window, you can type in this:
? GetUserName()
And it should spit out the current windows user name. (or whatever the code supposed to do). So, get the VBA function working, and once you do, then your data macro should now also work.

How to edit and copy non-VBA-macros?

Well, I know, there is a difference between VBA-macros and non-VBA-macros in Access.
Creating and editing of VBA-macros is not very different from Word/Excel: you have a text field, where you can write your code:
But what about editing of non-VBA-macros? For me, it looks like "programming using a mouse-clicking" (for me, as for the new guy in Access, it looks very weird):
Questions:
Is it a common and correct way to edit non-VBA-macros in Access "using a mouse"? Or, probably, there is another way, like shown on the 1st image?
If I have an embedded (as opposed to standalone) non-VBA-macro, how I can copy it from one database to another (or, to Stack Overflow)?
Non-VBA macros should probably not be manually edited.
You can copy the AXL by opening the macro, using Ctrl + A, Ctrl + C, and then pasting it to a text file. This is mainly useful for sharing a macro, because others can paste it, as outlined here: To paste a macro from Stack Overflow into Access.
You can copy and paste the AXL of normal, embedded and data macros. Note that data macros use a different set of functions than normal macros, and the availability differs per event. Normal and embedded macros are compatible.
You can, of course, edit the AXL. But as far as I know, there's no way to edit it from Access, and there's no validation outside of XML validation.
Note that you can convert an Access Macro into its equivalent Visual Basic code using the Convert Macros to Visual Basic option present on the Tools panel of the Macro Tools Design contextual ribbon tab:
This will result in the creation of a VBA Module similar to what may be created when you record a Macro in MS Excel or MS Word.
You can copy a macro from one database to another in the same was as with any other object in Access.
External Data -> New Data Source -> From Database -> Access -> Import

Running VBA code on an event in an Access web database

I took a already set up database from the Office.com templates (File --> New --> Office.com Templates), since this template already gave me 80% of my requirements.
I then changed the last 20% of this template where I came into the following: It seems as this template is a web database. I am not sure about the differences of a web database and a normal database in Access at all but I did find out, they do not allow me to run VBA code. At least not when I am trying to call it by an event.
Is there any way to achieve this anyways? Or are there possibly any other ways, such as converting the web database into a normal one perhaps?
Actaully, you can call VBA code from the forms event code. However, it requires a bit of a kluge.
However, you don't need to do this in your case.
Do note that you can create client objects and use client VBA code in your web application.
This means you can add/mix VBA forms into that application (these client objects of course cannot run in a web brwoser, but they can be used the client).
Suggestion #1:
to hide record navigation buttons.
Open the web form in layout mode. Move a object (dirty the design).
Whack ctrl-g for VBA command prompt. Type in this:
forms(0).NavigationButtons = False
Now, save the form. The record navigation setting will be saved with the form.
Suggestion #2:
Open the web form with VBA. Use this code:
DoCmd.OpenForm "AssetsDetails"
Forms("AssetsDetails").NavigationButtons = False
Suggestion #3:
Call VBA code from the forms load event. This is a bit of kluge, but does work:
In the forms open event, place this macro code:
OpenForm (frmRunVBA,,,Dialog)
Note that in above WEB MACRO editor the VBA form above called frmRunVBA does NOT appear in the drop down choice list - but you CAN TYPE/force in the name – this is legal.
Now create a VBA form called frmRunVBA). In the forms load event, palce this code:
Private Sub Form_Load()
Forms("AssetDetails").NavigationButtons = False
DoCmd.Close acForm, Me.Name
End Sub
So you can call VBA code, but you really don't need to if you use tip#1.

access 2007 bug - intermittent query parameter prompts

I am working on an Access 2007 application that was created by someone else. It has a strange, intermittent bug in which it prompts the user for query parameters when the main form is opened. The query parameters are clearly not necessary, because the error does not always occur.
The very strange "fix" to this problem is to open and close a particular module before opening the main form. Then the form opens without parameter prompts. However, of course I can't ask end users to open and close modules.
I tried using a macro to open and close the module when the database is opened. That fixes the bug, but leaves the VBA code window open, so that's no good.
Has anyone run into anything like this before? Any suggested solutions, workarounds, debugging tips, etc?
If you use the "Database Documenter" feature and check "yes" to all the options, you will obtain an exhaustive report that should let you trap your problem parameter. Export this report as an .rtf or .pdf document, so it is searchable. Identify a keyword from the dialog prompt, and search on that.
Once you check the query objects using the Documenter, check your VBA code. You'll do this by stepping through code in the IDE. If the main form has subforms, they are opened with (within) the main form. And they load before the main form.
Identify those subforms.
Sprinkle
breakpoints in their code modules
(if you find a Load function, that
is highly relevant).
If the main form has a
code module, do the same there.
Have a look for global variables in the module that needs to be opened and closed or any variable that is referenced in the module belonging to the form.
Access displays the Enter Parameter Value dialog box when you open an object that contains an identifier or expression that Access cannot interpret. You need to determine the source object. Here's a step-by-step guide:
http://office.microsoft.com/en-us/access-help/why-does-access-want-me-to-enter-a-parameter-value-HA010274377.aspx

Creating access CommandBar with timer event using VBA

I have Office 2003.
I would like to create Commandbar with timer in MS Access using VBA.
What I want is on ever half a second command bar button gets the name of active forms and lists name of forms.
How can I do that?
You can create a CommandBar using a Macro object in Excel 2003. However, needing to update it every 1/2 second is complete overkill. Forms won't get added or dropped very frequently, so you should have a static list for both performance and maintenance reasons.
The best way is to actually make a custom menu (an AddMenu line in the Autoexec macro) that calls a different macro with all the menu entries in it. Actual command bars require a good amount of VB to make them work properly.