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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Private Sub in one form from another form 5

Status
Not open for further replies.

TimTDP

Technical User
Feb 15, 2004
373
ZA
I have a pop up form (frmPopUp) that is opened via a command button on another form (frmMain)

On frmMain is a Private Sub called "UpDateLstFiles"

On frmPopUp is a "CloseForm" command button. When the user clicks this button I need to run the privat sub "UpDateLstFiles" inside frmMain

What is the syntax for this?

 
You can't.

The procedure is "private" to the form it is contained within, and can only be called from objects contained within the procedures parent form.

If you need to run the procedure from more than one form you need to declare it as public in a standard module.

Ed Metcalfe.

Please do not feed the trolls.....
 
If a form has a procedure that is not declared as private, you can say:
Form_FormName.ProcedureName

 
Remou,

Am I correct in thinking that this will only work if the form is open though?

It's a curious option to be honest - I've never really seen the point...

Ed Metcalfe.

Please do not feed the trolls.....
 
In my version, Access 2000, it works whether or not the form is open. I originally got the information from the MSDN Library disk that came with this version of Access.
 
Ah, yeah, but I think using the syntax

[tt]Form_FormName.ProcedureName[/tt]

vs

[tt]Forms!FormName.ProcedureName[/tt]

creates/opens an instance of the form to make that happen, while using the forms collection, will give an exception when the form isn't open.

For the fun of it, why not try something like this - assuming a form named frmTest:

[tt]dim lngCount as long
for lngCount = 0 to forms.count - 1
msgbox forms(lngCount).name
next lngCount
Form_frmTest.MyProcedure(SomeParameter)
for lngCount = 0 to forms.count - 1
msgbox forms(lngCount).name
next lngCount[/tt]

See also if the "Unhide" command is available in the Window menu afterwards.

Roy-Vidar
 
Thanks for the information RoyVidar, it is indeed as you say.
 
It seems I've got a star for giving duff information! :)

Just out of curiousity can anyone give and example of when you would want to call a procedure of one form from another rather than making the procedure public in a standard variable? I can understand what access is doing (creating an instance of the form's class then calling the routine), but why would you want to do it this way?

Ed Metcalfe.

Please do not feed the trolls.....
 
How are ya TimTDP . . .

Although you can't call a private routine from another code module, [blue]you can call it from a public routine in the same module![/blue] Just add a user defined routine that calls the private routine you desire. You call the public routine with [blue]remou's[/blue] post:
Code:
[blue]Public Sub RoutineName()
   Call TextboxName_AfterUpdate
End Sub[/blue]

Calvin.gif
See Ya! . . . . . .
 
On frmMain I have a command button that populates a list box with file names. The list of file names is restricted to those with common graphic extensions.( gif, jpg, png...) Rather than hard code these extensions, I get them from a table. (tblFileExtensions). On frmMain, I have another command button that opens a pop up form (frmPopUp), which is bound to tblFileExtensions. When the user closes this PopUp form I want to requery the list box. This is why I want to call the procedure in frmMain from frmPopUp.

 
TimTDP,
There is a lot of utility is calling procedures from one form in another, but you may be able to do what you are doing a little simpler

docmd.openform "frmPopUp",,,acdialog
me.lstBoxGraphic.requery

If you open the pop up form as a dialog form the code stops running until you return control back to it. So you can do whatever you need to do on the popup, and then when you close it the next line of code executes.

Here are a couple things to remember. A form is an object property of a form class. A form object is added to the forms collection when you open it and it is removed when you close it. So if you reference a form object using the forms collection it has to be open to be in the collection, else you get an error. Something like:
forms("frmName")
forms!frmName
Instead of using the collection containing open form objects, you can go directly to the form class and get the form object. Something like
dim myForm as new form_frmName
 
Well...if you change 'Private' to 'Public', you can call the procedure from everywhere, as long as the form is open:

Set frm = Screen.ActiveForm
Call frm.Command1_Click


will actually 'click' the button on that form.

Tested and used extensively for some time now.


HTH



[pipe]
Daniel Vlas
Systems Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top