Solved

Call a procedure when procedure name is stored in variable

Posted on 2007-11-21
6
262 Views
Last Modified: 2013-12-25
Hello everyone...

With VB6:
How can I call a procedure if the procedure name is stored in a variable?
For example:

Say I have a command button named "Command1" with this code:

Private Sub Command1_Click()
     MsgBox "Hello"
End Sub

Then in another sub I have this:

Sub Test()
Dim x as string
    x = "Command1_Click"
    'Here I want to call the procedure that is stored in x (which would be command1's click event in this case)
End Sub

I want to do that because the procedure name that is stored in x will change and I will not know what it is each time it changes, just that I need to run whatever procedure that is stored in x.

Any of you know how I can do this?

Thanks all!

:-)
Albert
0
Comment
Question by:ahammar
[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
  • 3
  • 2
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 20333087
as you want to "click" a button:

Sub Test()
   Dim x as string
   x = "Command1"

   dim c as control
   set c = controls(x)
   c.Value = true
End Sub


alternatively, you would have to make the Command1_Click from Private to Public:

Sub Test()
   Dim x as string
   x = "Command1_Click"

   CallByName(me, x, vbMethod)
End Sub
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 100 total points
ID: 20333144
Some background information about CallByName
How To Use the CallByName Function to Run a Procedure
http://support.microsoft.com/kb/186143

CallByName Function  
http://msdn2.microsoft.com/en-US/library/chsc1tx6(VS.80).aspx
0
 
LVL 23

Author Comment

by:ahammar
ID: 20333257
Thanks angelIII...

I should have been more explicit in my question...
Your first example does work good as long as it is a click event, but it is actually going to be a MouseUp event.  I didn't know that would matter, but in your suggested solution, it does.

Your second example I am getting:
Object doesn't support this property or method error on the CallBName line...

Got any other ideas??

:-)
Albert
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20333507
>Your second example I am getting:
>Object doesn't support this property or method error on the CallBName line...


I need to emphasize that the event handling method is created  PRIVATE, and needs to be changed to PUBLIC to be called like this:

so, it would need to be:
PUBLIC Sub Command1_Click()  
     MsgBox "Hello"
End Sub



0
 
LVL 23

Author Comment

by:ahammar
ID: 20335146
Hi AngelIII
Yes...I didn't miss that part.  Here is a small simulation of my code.  Do you see something wrong?

Private ActControl as Control

Public Sub Command2_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
    MsgBox "Command2_MouseUp event ran"
End Sub


Public Sub mnuBlank_Click(Index As Integer)
Dim BtnName as String
    Set ActControl = Me.Controls("Command2")

    BtnName = ActControl.Name & "_MouseUp(1, 1, 1, 1)"
    CallByName Me, BtnName, VbMethod
End Sub

It does work fine for the click event, but not for the MouseUp event
I still get the error on the CallByName line.  Maybe I'm entering the arguments wrong??, or maybe I can't use arguments?? (BTW...the arguments don't matter what they are...they are not used anyway)

BTW...My code is working now, but I had to create a sub procedure to simply call each event depending on what BtnName was = to with a select case statement...but I had to list all 21 buttons...ie
Call PickButton(BtnName)

Sub PickButton(btn as string)
Select Case btn
    Case "Command1"
        Call Command1_MouseUp(1, 1, 1, 1)
    Case "Command2"
        Call Command2_MouseUp(1, 1, 1, 1)

and so on...

Although this is not a critical situation anymore, it would be nice to have something better, but if I don't, then what I have will work...

Dhaest:
I checked out that link, but I still can't figure it out...

Thanks for everything!!

:-)
Albert


0
 
LVL 23

Author Comment

by:ahammar
ID: 20335264
Ok...never mind...I got it to work and it works great!  Just had to read about it in Help and then I figured out where I needed to put the arguements...so it works good now.

Thanks for your help!

I will split the points...but I must give AngelIII the majority for being first and supplying the most information, but thanks to both of you!

:-)
Albert
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month6 days, left to enroll

626 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