Solved

Call a procedure when procedure name is stored in variable

Posted on 2007-11-21
6
258 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 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