Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I add code to my forms' event procedures programatically?

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
US
Say I want to add a piece of code to every Form_Open sub in the forms in my database. I know how to raise the event, but can I actually process the code somewhere programatically (maybe within the collections somewhere)?

Any help would be greatly appreciated!
 
To some extent, it will depend on the version of access that you use. 2002 will likely be easier than its predecessors. But why don't you put the code in a global procedure in a module and then all you need is a single line of code in each of the Open events. Itv would be possible to change the code in the module programmatically, i'd think.
 
You'd be asking for runtime metaprogramming here...so an engine would have to produce code at runtime for those containers that hold the code that responds to runtime events. Can't be done.
 
I reckon it can be done (Access 2K+)
Goto online help, and search for keyword "Module". Choose the topic "Modules Property"; then click on the "Modules Collection" link on the right.

You're sort of in the right area now to experiment with what you want to do. n particular, you might want to check out the "Add From File" method, under the blue "Methods" link on the top.

Good luck; let us know how you go,
Steve
 
Sorry--read your submission late...it appears that you want to do this at design time. However, you're still asking for a meta-reference, i.e. "let me use code to do something to the containers that hold the code". There may be a way using the DAO.Containers("Modules").Documents(eachmodule) collection (you can extract code from this I think), but you're asking for a sophisticated, programmable object model operating against the programmable object model. (If I had a place to stand and a long lever I could move the world.)
 
Quehay,

Sorry, but don't understand what you are saying re "you want to do this at design time", "using long levers to move the world" etc. The poster has said he wants to do this on a forms OnOpen event (ie. runtime); nothing about when he wants to raise the code.

Here are the basics as I see them relating to this thread(and I hav'nt done this 'dynamic coding' before, so I'm attempting to draw on some fundamentals to help answer this question).

(a) The Modules collection contains source code for the various modules within it. Review its properties and objects within to confirm this.

(b) Microsoft Access is an interpretive language, compiling "on the fly" if necessary to produce runtime (target) code which it then uses to execute the program code.

(c) The above being true, there is nothing to prevent a (new or existing) module's source code from being "prepared" from within Access (at runtime), for subsequent use.

(d) Once that module has been prepared and appropriately closed/saved, it becomes just another object in the database, waiting to service the application when called to do so (eg. no different to a querydef or tabledef).

(e) My brief investigation into this capability (prompted by this thread) leaves me confident that its not only possible, but is a design intention, consistent with the languages ability to create and maintain other types of objects from within.

SBendBuckeye,

I guess what I'm interested in, is what sorts of things you want to do in terms of customising your form code when the form is opened; Actually, you'd probably have to trigger your "Code modification code" BEFORE a form is opened, for the code to be invoked when the form is opened.

Please let us know what it is you're trying to achieve. If its a genuine candidate for "dynamic programming" then I think you'll spark some real interest; if not, then perhaps we'll be able to point you on how to achieve what you want with more traditional static code.

Cheers,
Steve
 
Lookup in the Help file "CreateEventProc method".
There is more than enough in the examples and
"See Also" to get you moving in the right direction.
 
Sorry, I haven't been able to check this since I posted my initial question. I didn't mean to be so ambiguous.

An example of what I would like to do is to loop through all of the forms in the allforms collection and add the phrase "Set OrderByOn = True" to the Form Open event for each form.

I try to do this manually now, but I sometimes forget and then if its late and I'm tired, it may take a few minutes of frustration if my form is not coming in ordered properly before I remember I need to set this property.

Another example would be code cleanup. As I am working on things, I will sometimes put a comment with my initials after it so I can find it later. Again, it would be nice to be able to spin through the collect of forms and check the code behind it.

Hopefully, this will clarify my intent. I will try to get back here tomorrow to read the responses above.

Thanks for the help!
 
When Access becomes a language there are a lot of C and Pascal programmers who will find this interesting (I know this isn't what you literally meant). There are compilation directives that can change what happens based on environment. However a look at the DAO object model just doesn't show a lot of methods available for the Containers("Modules").Documents("AModule") objects. I looked in Helen Feddema's [excellent] DAO Object Model reference book (O'Reilly) and don't see anything there. Hey I'm open to learning if something can be done here, but sceptical until it's proven. ;-)

The allusion was to Archimedes who said "Give me a place to stand and a lever long enough, and I can move the world".
 
Quehay,

a couple of points

(a) Access has a language built into it; its called VBA, though I take your point regarding conditional compilation. You can of course emulate conditional compilation using if statements and global constants, though Im not sure how this fits into the scope of the question.

(b) You wont find a lot about something like CreateEventProc (as mentioned in Raskews post earlier) in the DAO book by Reilly, as CreateEventProc is not part of the DAO library; its part of the standard Access library.

(c) Use the online help, or better, the object browser to verify the existence of this method. Best of all, check out the following web site:


Hope this helps reduce the sceptism (hopefully eliminate it).

SBentBuckeye,

Not too convinced that your reasons provide adequate justification for using this approach, but would be interested to see what you come up with.

Incidentally, what I would be like to see, is when you create a new procedure, it automatically creates a template of comment lines at the top with Author Name, Date, Description heading etc. That would be useful, and I'd be curious if anyone knows whether it's do-able using any of the module modification methods.

Cheers,
Steve
 
I stand corrected. Thanks for your persistence Steve (I should have investigated RAskew's suggestion too). I'm also really intrigued, no I'm amazed that this has been lurking there all this time; not sure when this will be useful though, as opposed to hand coding or copy & paste.

This put a working sub in a general module:

[tt]
Public Sub TryItOut()
Dim Mdl As Module

Set Mdl = Modules("module1")

Mdl.AddFromString "Public strTest as string"

Mdl.AddFromString "Public Sub TestIt" & vbCrLf & _
"strTest = ""Does this work?""" & vbCrLf & _
"MsgBox strTest,vbExclamation " & vbCrLf & _
"End Sub"


End Sub[/tt]
 
Hey Quehay, if you added a call to your new function at the end of the TryItOut function, does it execute?

Also, would it still work if your DB was compiled in to an MDE?

Thanks Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top