Calling public sub from another form (2 Viewers)

petko

Registered User.
Local time
Today, 08:39
Joined
Jun 9, 2007
Messages
85
I've been trying to solve this problem for I while with no succes. I found no matching questions in this forum, either.

I'd like to call a public sub of a form from its subform.

Is that possible?

Thanks for any help

Peter
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:39
Joined
Sep 1, 2005
Messages
6,318
Yes, but we generally don't do this, instead putting whatever we need to be public in a separate module.

The reason is that you have to fully qualify the sub with the form where it is in like this:

Code:
Call Forms!MyFormName.MyPublicSub

Whereas the code in standard module is just:

Code:
Call MyPublicSub

HTH.
 

petko

Registered User.
Local time
Today, 08:39
Joined
Jun 9, 2007
Messages
85
Thanks a lot for your help.

I agree but sometimes it would be easier to locate a public sub in a form when the sub needs values from that forms' own fields. So reference woulbe be much easier.

I'm trying your suggestion:

Call Forms!ProjectKondicioTar.Test

But I get an error message: compile error: expected . or (

What am I making wrongly?

thanks in advance

Peter
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:39
Joined
Sep 1, 2005
Messages
6,318
You can clearly see that I don't use it alot. :)

Maybe this will work:

Code:
Forms("MyFormName").MyPublicSub

Or maybe it needs to have ()s:

Code:
Forms!MyFormName.MyPublicSub()

HTH.
 

ChrisO

Registered User.
Local time
Today, 16:39
Joined
Apr 30, 2003
Messages
3,202
Try: -

Me.Parent.Test

Hope that helps.

Regards,
Chris.
 

EraserveAP

Registered User.
Local time
Today, 02:39
Joined
Jul 22, 2008
Messages
56
Form_frmName.PublicSubName(anyVars)

That’s always worked for me.
 

Arpeggione

Registered User.
Local time
Yesterday, 23:39
Joined
Apr 8, 2008
Messages
18
Three years later....just a note...

Thank you EraservAP - your answer does the trick!

regards,

Karen
 

Simon_MT

Registered User.
Local time
Today, 07:39
Joined
Feb 26, 2007
Messages
2,177
I would use a Module and With CodeContextObject you aviod referencing the Form or Report although SubForms have to be specified with the Parent / Form or Report expressed by a [Dot].

Simon
 

poduska

Registered User.
Local time
Today, 02:39
Joined
Jul 29, 2008
Messages
13
Thank you over 4 years late;)

I am surprised that
Form_frmName.PublicSubName(anyVars)
works but it does. :D

I have not used Form with an underscore before, ! or . but not _.

Access.....:banghead::banghead:
 

mdlueck

Sr. Application Developer
Local time
Today, 02:39
Joined
Jun 23, 2011
Messages
2,631
I would use a Module and With CodeContextObject you aviod referencing the Form or Report although SubForms have to be specified with the Parent / Form or Report expressed by a [Dot].

Agreed.

Shared code belongs in Modules and Classes, not on Forms or Reports.

With Forms I understood that Subroutines are actually Events of the Form. In other words, live code which will fire if the Form receives the particular event. I doubt you really intend for so much behavior overhead with your shared subroutine. Perhaps the same is true of Reports.

Oh petko...
Location: Sopron, Hungary
My wife and I visited there this past Winter/Spring. ;)
 

mdlueck

Sr. Application Developer
Local time
Today, 02:39
Joined
Jun 23, 2011
Messages
2,631
I'd like to call a public sub of a form from its subform.

Is that possible?

Sounds like you need the subform suggestion I posted here:

Relocate Previously Selected Record in a Multiple Items Form
http://www.access-programmers.co.uk/forums/showthread.php?p=1178436#post1178436

That is code on the subform reaching around through the parent form back to itself! :cool: Simply referring to Me the subform was not good enough as the subform control had lost focus when the button was pushed on the main / parent form.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:39
Joined
Jan 20, 2009
Messages
12,851
A Public sub (ie a Method of the Object) should be called exactly like any other method.

Depending on where it is called from:
Code:
Forms!formname.method paramater1, parameter2 ...
Me.Parent.method parameter1, parameter2 ...
Me.subformname.Form.method parameter1, parameter2
etc

The reference with the underscore is a call directly to the class module rather than the instance of the object. It becomes ambiguous when there are multiple instances of the class.

Call is mainly used to refer to a function when a return value is discarded. Access is expecting the "(" with Call because it is expecting a function. BTW If you put parentheses around a single parameter when passing parameters to a Sub it will be forced to pass ByVal regardless of the argument declaration in the Sub.

I use custom methods on forms all the time. Anyone who doesn't is not structuring their code very well.

An example. A subform requires the Enabled property of buttons to be set depending on the state of something in the main form. If you are using this code then you are doing it clumsily.

Code:
If whatever Then
   With Me.subform.Form
      .button1.Enabled = False
      .button2.Enabled = True
etc

Add the state setting as a public sub on the subform object itself.

Code:
Public Sub ButtonState (ByVal Enable as Boolean)
   With Me
      .button1.Enabled = Enable
      .button2.Enabled = Not Enable
etc

Set the button state from the main form:

Code:
If whatever
   Me.subform.Form.ButtonState True
etc

Or my preferred technique

Code:
Me.subform.Form.ButtonState (x = y)
 

ChrisO

Registered User.
Local time
Today, 16:39
Joined
Apr 30, 2003
Messages
3,202
Poduska.

Form_frmName.PublicSubName

The above may work but it may not work as intended.

If the Form is Open then PublicSubName is called.

If the Form is closed an instance of the Form is opened with Visible = False. If the Form has Open and/or Load events they are called before the PublicSubName. The Form then remains Open but invisible and it is not in the Forms collection. Any subsequent calls to PublicSubName work without creating a new instance of the Form.

We can then open another instance of that Form from the database window and that Form will be visible. If we close the visible Form then its Unload and/or Close events will fire, if they exist. Then the invisible Form will close and fire its Unload/Close events if they exist.

There would be many different ways it could produce unexpected results.

So yes, Form_frmName.PublicSubName does work; but do we really want it to work that way?

If, after all that, you still wish to call a public procedure in a closed Form then this would probably be better:-
Code:
Sub TestIt()
    Dim frm As New Form_frmName

    frm.PublicSubName
    
End Sub

Here, again, the Open and Load events would run before PublicSubName is called.

The advantage is that the variable frm, used to reference the new instance of Form_frmName, will automatically get set to Nothing when it goes out of scope. When it gets set to Nothing the instance of the Form will be closed which will call the Unload and Close events if they exist.

That will at least mean that the instance of Form_frmName will not be left dangling somewhere not knowing if it is open or closed.

Chris.
 

EraserveAP

Registered User.
Local time
Today, 02:39
Joined
Jul 22, 2008
Messages
56
Poduska.

Form_frmName.PublicSubName

The above may work but it may not work as intended.

If, after all that, you still wish to call a public procedure in a closed Form then this would probably be better:-
Code:
Sub TestIt()
    Dim frm As New Form_frmName

    frm.PublicSubName
    
End Sub
Chris.

Appreciate the input. Myself I never had an issue, but should the need arise again will use your method going forward.
 

DrChocolate

New member
Local time
Today, 02:39
Joined
Feb 21, 2012
Messages
7
Dear Eraserve,

Your post from July, 2008 hit the spot. Thanks for being so clear in your explanation -- I wuz going nuts over the fragile syntax of a call.

DrChocolate
 

memaphu

New member
Local time
Yesterday, 23:39
Joined
Mar 25, 2010
Messages
1
You can clearly see that I don't use it alot. :)

Maybe this will work:

Code:
Forms("MyFormName").MyPublicSub

Or maybe it needs to have ()s:

Code:
Forms!MyFormName.MyPublicSub()

HTH.

After 10 years :) this one works well for me.
Thanks You!
 

metalis

New member
Local time
Today, 14:39
Joined
Jun 29, 2017
Messages
1
Thank you over 9 years late.
Access.....:banghead::banghead:
 

ashleedawg

"Here for a good time"
Local time
Yesterday, 23:39
Joined
Jun 22, 2017
Messages
154
Wow, 86000 views on this thread, 5-Star rating... That's amazing! Does that make this "Access Question #1"?

So everyone knows this now, right? There will be a test next week. EraserveAP will be facilitating.
:)
 

Users who are viewing this thread

Top Bottom