UtterAccess.com
We have detected that you are using an unsupported web browser. We recommend you to upgrade your web browser to get the most from UtterAccess forums.
Why should and how can I upgrade?
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Call a Parent Form Function from Sub Form    
 
   
38JJC
post Sep 15 2010, 08:54 AM
Post #1

UtterAccess Addict
Posts: 246



I've created a Form with a function that totals a number of controls on the Form. Each control has the function call in the AfterUpdate event to make the total live. I also have a sub_Form in the Parent Form with controls that need to be totaled. The Function that does the totaling is in the class module on the Parent Form. How do I call this function on the AfterUpdate event on the Sub Form's controls?
Go to the top of the page
 
+
BananaRepublic
post Sep 15 2010, 09:01 AM
Post #2

Admin under the bridge
Posts: 1,157
From: Banana Republic



Basically three ways to do this:
From subform controls' event:
Implicit reference:
CODE
Me.Parent.<Name Of Function>

Explicit reference:
CODE
Forms("NameOfParent").<Name Of Function>

In either case, it is assumed you've declared the function as Public, not Private. Either methods has their merits - implicit reference will ensure your code not breaking if you rename the parent or if you use subform in different parent forms that has same functions. However, if the parent form doesn't have this function, then the code will cause an error. Explicit reference is more fragile since you're now locked to a form by that named and relying on it being loaded.
Another way is to subscribe to the subform controls' event in the parent form such as:
CODE
Private WithEvents MySubTextBox As Access.TextBox
Private Sub Form_Open(Cancel As Integer)
   Set MySubTextBox = Me.MySubform.Form.Controls("Name Of Control")
End Sub
Private Sub MySubTextBox_BeforeUpdate(Cancel As Integer)
  <Name of function>
End Sub

The advantage is that your form's function doesn't have to be public since the code is executed inside the parent form's module, and this will not run into issues where you may have subform opened in a different parent form or by its own and thus breaking the code, though you have to deal with the timing issues especially if you have codes for BeforeUpdate event behind the subform, which tends to fire before the event handler in the parent form can fire.
So, here you go, take your pick and best of luck!
Go to the top of the page
 
+
alfaista
post Mar 23 2011, 10:32 AM
Post #3

UtterAccess Member
Posts: 46



hello!
eaching for a way to do something similar. i need to call a control's code that is on the parent form, on the subform.
I would like to run a combobox's AfterUpdate event when the user has selected something on the subform, but the parent form has not be completely filled out. the afterupdate will fill it out for the user, as the combobox will have a value loaded, the user doesn't always HAVE to select a value.
so, how to go about doing this? doesn't seem difficult, but i have not come up with the correct combination yet. both the comboboxs' AfterUpdate events have been declaraed as Public.
is this possible?
thanks in advance!
Go to the top of the page
 
+
BananaRepublic
post Mar 23 2011, 10:48 AM
Post #4

Admin under the bridge
Posts: 1,157
From: Banana Republic



Did you see the post right before your post? Did you try those suggestions?
Go to the top of the page
 
+
alfaista
post Mar 23 2011, 10:49 AM
Post #5

UtterAccess Member
Posts: 46



hi! thanks for the quick response! wow!
I have tried this...
Me.Parent.Controls("conProjectTitleCB").AfterUpdate
and....
(really doesn't like this!!)
Me.Parent.Controls("conProjectTitleCB")_AfterUpdate
Go to the top of the page
 
+
BananaRepublic
post Mar 23 2011, 11:03 AM
Post #6

Admin under the bridge
Posts: 1,157
From: Banana Republic



No, the syntax would be:
!--c1-->
CODE
Me.Parent.conProjectTitleCB_AfterUpdate

You want to call a method named "conProjectTitleCB_AfterUpdate" which happens to be also the event handler for the control's conProjectTitleCB AfterUpdate event. The AfterUpdate property is used to assign an event handler or invoke a function via Expression service.
Does that help?
Go to the top of the page
 
+
alfaista
post Mar 23 2011, 11:13 AM
Post #7

UtterAccess Member
Posts: 46



perfect!!
KNEW i was trying to make it too hard!
thank so much!!
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Search  ·  Go to Top  ·  Lo-Fi Version Time is now: 20th September 2014 - 02:57 AM