Solved

Call function/sub in parent form?

Posted on 2002-04-26
4
652 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

738 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