calling a routine in another form via variables

Posted on 2005-03-01
Medium Priority
Last Modified: 2010-05-02
I have a situation where I have a generic form that will be called by many programs.  When the user clicks a button on the generic form, I want it to call a routine in the original program (the one that called the generic form).  The problem is the form name and routine name will be different in every circumstance and I don't want to hardcode it.  I want to pass any information needed to the generic form.  I am passing the values via an XML file so it needs to be in text form (ie. I can't pass the form object).

In my first form (frmMain) I have various routines including:

Public Sub DisplayMsg()
    MsgBox "Test"
End Sub

This form later calls a second form (frmGeneric).  At some point in this form, I need to call the DisplayMsg routine in the first form.  I know I can do this by doing frmMain.DisplayMsg.  However, I need to do it using variables.  

The following code works:

    x = "DisplayMsg"
    CallByName frmMain, x, VbMethod

However, since I need to use variables, I try the following and it does not work:

    x = "DisplayMsg"
    y = "frmMain"
    CallByName y, x, VbMethod

How can I make this work?

Question by:chlade
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
LVL 32

Accepted Solution

Erick37 earned 2000 total points
ID: 13430716
One possibility:

Dim frm as form

for each frm in forms
  if frm.name = "frmMain" then
    CallByName frm, x, VbMethod
  end if

Expert Comment

ID: 13430743
Try this.


Public Function GetFormByName(ByVal strName As String) As Form

    Dim Form
    For Each Form In Forms
        If strName = Form.Name Then
            Set GetFormByName = Form
        End If

End Function

Private Sub Command1_Click()
    x = "DisplayMsg"
    y = "Form1"
    CallByName GetFormByName(y), x, VbMethod
End Sub

Expert Comment

ID: 13430753
Oops too late... Erick37 got it.

Author Comment

ID: 13430770
That did it.  Thanks!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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