UtterAccess.com
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 * me = NULL;
Posts: 1,079
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!


--------------------
Using Access BE over WAN is like running to your city library, ripping out pages from a book, running back home and scribble notes on it then run back and glue the pages back into the book in the library, all during a rush hour full of drunk drivers in middle of a horrible thunderstorm, and hoping nobody else has ripped out the pages you wanted.
Go to the top of the page
 
+
alfaista
post Mar 23 2011, 10:32 AM
Post #3

UtterAccess Member
Posts: 46



hello!

seaching 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 * me = NULL;
Posts: 1,079
From: Banana Republic



Did you see the post right before your post? Did you try those suggestions?


--------------------
Using Access BE over WAN is like running to your city library, ripping out pages from a book, running back home and scribble notes on it then run back and glue the pages back into the book in the library, all during a rush hour full of drunk drivers in middle of a horrible thunderstorm, and hoping nobody else has ripped out the pages you wanted.
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 * me = NULL;
Posts: 1,079
From: Banana Republic



No, the syntax would be:

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?


--------------------
Using Access BE over WAN is like running to your city library, ripping out pages from a book, running back home and scribble notes on it then run back and glue the pages back into the book in the library, all during a rush hour full of drunk drivers in middle of a horrible thunderstorm, and hoping nobody else has ripped out the pages you wanted.
Go to the top of the page
 
+
alfaista
post Mar 23 2011, 11:13 AM
Post #7

UtterAccess Member
Posts: 46



perfect!!

i 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: 28th July 2014 - 09:23 AM