Run Event Code Of Another Form

wazza

Registered User.
Local time
Today, 10:00
Joined
Apr 23, 2004
Messages
104
Hi

Please help - this is really frustrating me....

I have two forms - one form contains quite a bit of code attached to a button (on click event)

I have now produced another form - I want to run the same code as the event on-click in the original form

I have normally called procedures using the name;
wzdfinish_click

How do i call an event in another forms module collection ....????

Many thanks!
 
Put it in a global module and run it.

???
kh
 
- how do i run the global module from a forms, button click?

- thers quite a lot of code, calulating varibles relevant to the form, it seems it would be a quick win to simply run the other forms on click event - is this possible?
 
How do i run the global module from a forms, button click?
Pretty basic programming stuff. Try looking through Access help. If you get stuck on a specific problem, re-post...


There's quite a lot of code, calulating varibles relevant to the form, it seems it would be a quick win to simply run the other forms on click event - is this possible?

Even if you could it would seem to be a bad idea as your code would become spaghetti code rather quickly...

(Just my opinion. Sorry, I know that doesn't help much as a short term solution...)
kh
 
When you have common code, it belongs in a standard module.
 
There are times when it is advantageous to call code in another Form.


Code:
[color=green]'  From Parent to Child Form[/color]
Me.ctlSubForm.Form.wzdfinish_Click

[color=green]'  From Child to Parent Form[/color]
Me.Parent.wzdfinish_Click

[color=green]'  From Child to Parent of Parent Form[/color]
Me.Parent.Parent.wzdfinish_Click

[color=green]'  From a Public Module directly to Main Form[/color]
Forms("frmMain").wzdfinish_Click

[color=green]'  From a Public Module to SubForm via Main Form[/color]
Forms("frmMain")("ctlSubForm").Form.wzdfinish_Click
However, in all cases the Form must be open, the event procedure must be public and the event is not raised.

If the called event procedure needs any arguments then those arguments need to be passed and must be the correct data type.

Hope that helps.

Regards,
Chris.
 
This post has been there for a long time but the exact answer hasn't been provided.

To run an event procedure in a form from another form (which is rarely but occasionally useful) you must do two things:

1) Remove the Private keyword in front of the name of the Sub you want to call. If you want you can add the Public keyword before the name of the Sub but it is not necessary. If it is not Private then it is Public.

2) Simply call the Sub (now Public) in the other form using the following syntax:
Forms("frmOtherFormName").ControlName_Event

For example in the case I am working on I had to call my Sub using:

Code:
Forms("frmTyreMaintenance").cboShowTrailers_AfterUpdate
Hope this helps
 
Last edited:
>>This post has been there for a long time but the exact answer hasn't been provided.<<

How does your answer differ?

Chris.
 
Hi Chris. When I read your post I didn't notice you mentioned "the event procedure must be public". Plus I was annoyed at all the post before that discourage the approach. So, I didn't click that you indeed gave the tip. So the post was answered.

My post, then, makes it a bit clearer how to make a Sub Public and not Private. If I was a bit new in VBA I may no be fully aware that by removing Private it becomes Public.

So, no offence mate.

Daniel
Melbourne
 
dlamarche

I know the post is old, but THANK YOU SO MUCH!!!
I tried everything and completely forgot about private/public scopes.
 
You just attacked a 12 year old post, and added no real value to the forum.

If you need help and you have a question to ask, or if you plan to make a contribution to this place, I warmly welcome you.

If you are going to kick through the ashes and complain about what you find, please consider spending your time in more valuable pursuits.

Thanks,
 
The mods would probably ask us to take this dialog to the cooler, but...

Since you brought it up, if it's such a bad program why are you trying to learn how to use it?
 
Cool - outside my flippant response a few years back, did you get your question answered in regard to running code from a second form?
 
So when referencing a sub form you must reference the subform object that reside on the main form first:

forms!myMainformName.mySubFormOjectname!mySubformName etc

You may have tried this. My apologies if you have...
 
I think the late ChrisO gave the answer in post 6.
in all cases the Form must be open, the event procedure must be public and the event is not raised.

If the called event procedure needs any arguments then those arguments need to be passed and must be the correct data type.

and as Ken said you must have the proper syntax to reference controls on other forms
 
I am a bit rusty and may have my syntax mixed up...

Seems like the best option is to slice out the code to be called from multiple places and put it in a separate module. But it sounds like this has already been attempted...
 
yeah - sounds like you inherited a monster. Is the re-write slated for MS Access or some other platform?
 

Users who are viewing this thread

Back
Top Bottom