Solved

Call function/sub in parent form?

Posted on 2002-04-26
4
648 Views
Last Modified: 2012-06-27
I have form frmParent that calls form frmChild via:
frmChild.Show vbModal, Me

If I have a function/sub in frmParent's code, how can I call that from frmChild's code? An important thing to note is that the function/sub needs to modify controls on frmParent.
0
Comment
Question by:dplambert
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
mdougan earned 50 total points
ID: 6972206
You can do this if the Function/Sub in the frmParent is declared as Public.

The syntax in the frmChild's code would be:

frmParent.runSomeParentCode()

And the parent code can do whatever it wants to with it's controls
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6972367
If you will call function from different parts of your program, i recomend you that use a module and write those public functions there.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6972622
Richie, I used to think the same thing, but then it occured to me that a form is a lot like a Class, just with a GUI part.  I started encapsulating program functionality by Form and by Class, and stopped using modules for just about everything.  I even include methods in the form that are used to Show the form, and rarely issue a frmChild.Show... instead, I might have a Function in frmChild that is called:

Public Function AddContact(lDeptID) as boolean
   Load frmChild ' probably not necessary
   txtDeptID.Text = lDeptID
   frmChild.Show vbModal
' Now, user has clicked OK which issues a me.hide
   InsertContactToDB
   AddContact = True
End Function

Then, in frmParent, you can call to Add the contact just by issuing a method call:

if frmChild.AddContact(CLng(txtDeptID.Text)) Then
   RefreshContacts()
End if

And that sort of encapsulates all the GUI and other functionality associated with adding a contact.  Plus, I can now copy that frmChild to another project and not have to worry about dependencies with other modules....
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6972672
OK. i only do that way when create Active x controls.
Cheers
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now