Solved

Call a procedure when procedure name is stored in variable

Posted on 2007-11-21
6
261 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
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…

734 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