Solved

Call a procedure when procedure name is stored in variable

Posted on 2007-11-21
6
259 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
  • 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
Technology Partners: 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!

 
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

Industry Leaders: 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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
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…

679 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