Running Public Sub in parent form from child form (1 Viewer)

cpampas

Registered User.
Local time
Today, 11:00
Joined
Jul 23, 2012
Messages
218
I wonder if this is posible !
Can I run a Main form button's Public Sub , from the child form ?

Code:
str = "AAA"
[Forms]![frmMagicCont]![Procura de Documentos].[Form]![Descritivo] = str   ' this parts works fine
Call Forms([frmMagicCont]![Procura de Documentos]).Command68_Click     ' this part does not
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:00
Joined
Sep 21, 2011
Messages
14,294
Try
Code:
Call Me.Parent.Command68_Click
Are you sure that even has been changed to Public?, as it defaults to Private?
 

cpampas

Registered User.
Local time
Today, 11:00
Joined
Jul 23, 2012
Messages
218
Call Me.Parent.Command68_Click
Yes, I changed it to Public
That would work if the button was in the parent form, but (my mistake), Actualy the title in my post is incorrect since the sub to be runned is not in the parent form(frmMagicCont), but rather in the first child ([Procura de Documentos]) form, and i will be calling it from another second child form
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:00
Joined
Jan 20, 2009
Messages
12,852
Me.Parent.subformcontrolname.Form.methodname
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:00
Joined
Jan 20, 2009
Messages
12,852
Personally I prefer to have the Private button code and a Public Sub (aka Method) run the same code rather than making the button code Public.
 

Sun_Force

Active member
Local time
Tomorrow, 03:00
Joined
Aug 29, 2020
Messages
396
Me.Parent.subformcontrolname.Form.methodname
I'm trying different combination, but I keep receiving Application-defined error.

Subform Name: frmParts_Sub
parent Name : frmProcess

I'm trying to run cmdShowDrawing_Onclick() in parent by double clicking a text box in subfrom. Textbox name is txtDr.

What would be the exact code to run cmdShowDrawing_Onclick() in parent form?
thank you.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:00
Joined
Jan 20, 2009
Messages
12,852
The parent name doesn't matter. Parent is a Property of a control that refers to the parent object.

Note the subform object might not have the same name as the subformcontrol that holds it though it is by default.

If the method is in a subform on the parent:
Code:
Me.Parent.frmParts_Sub.Form.cmdShowDrawing_OnClick

If in the parent:
Code:
Me.Parent.cmdShowDrawing_OnClick

Or maybe:
Code:
Me.Parent.Form.cmdShowDrawing_OnClick

(Its Friday night and been a hard week.)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 19:00
Joined
Sep 12, 2006
Messages
15,655
Just on a general point, you wouldn't want to design a subform to run code in the parent form, I don't think. The subform ought to know about itself only. What code are you trying to run in the parent?
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:00
Joined
Jan 20, 2009
Messages
12,852
you wouldn't want to design a subform to run code in the parent form. The subform ought to know about itself only.
I don't see any particular reason why not. I've done forms that reconfigure themselves into a more compact layout when they detect they are inside a Parent form.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:00
Joined
May 7, 2009
Messages
19,242
the sub cmdShowDrawing_Onclick()
must be declared Public Sub(not Private)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 19:00
Joined
Sep 12, 2006
Messages
15,655
I don't see any particular reason why not. I've done forms that reconfigure themselves into a more compact layout when they detect they are inside a Parent form.
I was thinking more of a data management point of view, but I see what you mean.
 

Sun_Force

Active member
Local time
Tomorrow, 03:00
Joined
Aug 29, 2020
Messages
396
Just on a general point, you wouldn't want to design a subform to run code in the parent form, I don't think. The subform ought to know about itself only. What code are you trying to run in the parent?
I don't. I was just testing. OP was able to run the code and my tests brought up errors.
I was just curious why or where I'm making the mistakes.

Seems that as @arnelgp suggests, the sub should be declared as public and not private.
 

Sun_Force

Active member
Local time
Tomorrow, 03:00
Joined
Aug 29, 2020
Messages
396
the sub cmdShowDrawing_Onclick()
must be declared Public Sub(not Private)
From FormA, I can run a control's event procedure on FormB's. Even if it's private.

Forms("FormB").cmdTest_OnClick

Why when I try to run a code in parent form from it's child form, it should be public?

thanks
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:00
Joined
Feb 19, 2002
Messages
43,273
Keep in mind that when you run code on a subform, it operates on what Access thinks is the current record so make sure you know what that is.
 

isladogs

MVP / VIP
Local time
Today, 19:00
Joined
Jan 14, 2017
Messages
18,219
All code using _OnClick() needs to be replaced with _Click without the () brackets e.g. CmdShowDrawing_Click
 

Users who are viewing this thread

Top Bottom