?
Solved

calling a routine in another form via variables

Posted on 2005-03-01
4
Medium Priority
?
172 Views
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?

Thanks,
Chris
0
Comment
Question by:chlade
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
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
next
0
 
LVL 7

Expert Comment

by:Smallint
ID: 13430743
Try this.

Cheers

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
    Next

End Function

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

Expert Comment

by:Smallint
ID: 13430753
Oops too late... Erick37 got it.
0
 

Author Comment

by:chlade
ID: 13430770
That did it.  Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

571 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