Write code with code

raindrop3

Registered User.
Local time
Today, 02:27
Joined
Sep 6, 2001
Messages
98
Hi,

i was wondering if it is possible to write code (vba) with vba. I need to build some kind of wizard, to add a command button to a form and behind the command button in the 'on click' event must come some code that the 'wizard' generates.

Does anyone know if this can be done?

greetings,

Albert
 
From Access Help:

Create your own wizards, builders, and add-ins

To develop your own add-in (wizard, builder, or menu add-in), you can set several database properties and create a USysRegInfo table in the add-in database (*.mda, *.mde) so that it can be installed by using the Add-in Manager. You can also develop a COM add-in that can be installed by the Add-in Manager. For more information, see Chapter 11, "Add-ins, Wizards, Libraries and Templates," in The Microsoft Office 2000/Visual Basic Programmer's Guide.
 
I won't address the bit about the command button, but I was able to add a "Form_OnFormat" event routine for every report in a list of over 100 reports. The routine to be created wrote an entry in an auditing table every time the report header was formatted. It is unimportant as to what was actually logged. The fact was that Access wouldn't do it automatically and I couldn't get WindowsNT to identify individual reports using its own event logger. (No, I didn't expect WNT to do so, either.)

What I did to implement this was write a routine that visited each report in the database.Containers!Reports.Documents collection. In order to run the routine, I made it a public function and created a macro to do a RunCode of this function.

So OK, the routine gets run by the macro. It opens the database and starts enumerating the documents collection as noted above. Each document is the name of a report, which I opened.

If the report had a module, I did a search using the module.Find method to see if an auditing reference was already present. (The name of the auditing subroutine is distinctive enough that the search was never ambiguous. It was either present or not and there was never a name that could have even been close enough to cause confusion.)

If the report had no module, I created one for it. Turns out that even if there is no module present, all you have to do is open the report.Module object. Opening it creates a module if necessary. It is empty except for the default general header that ALL modules carry with them, the second line of which is usually "Options Database."

If the module lacked an auditing reference, I then performed a module.AddFromFile (from a pre-defined external file that held my generic auditing code. The included code looked up names using Me.Name and looked up other things in similarly generic ways.)

Once all of that was done for the report, I closed the module and report, then continued the loop over the Reports.Documents collection until this looped reached its natural end.

Voila! When it was done, I had created code in event routines for every one of several dozen reports. (The others already had the auditing code in place, but the .Find method told me that when I ran the update.)

So without regard to the "button" part of your question, I will say "emphatically yes you can create code in a module from another module."

I hope the above overview gives you at least a hint of how to approach this topic. I would include the code but this is a military site and I am not supposed to make specific code public. What I told you doesn't violate our security so feel free to use the approach.
 
The_Doc_Man,

Thank you very much for your answer. There are very useful things I can think about. The part of the pre-defined file is interessting. I can take some time, but when I got it working for a commandbutton, I will let it know!

Thanks for your time.

Albert.
 

Users who are viewing this thread

Back
Top Bottom