Solved

Call a procedure when procedure name is stored in variable

Posted on 2007-11-21
6
253 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
>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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now