-
Call sub from variable
I would like to call a sub (or function if necessary) from a variable. Currently I am having difficulties doing this. I get an error. The error is Compile error: Expected Sub, Function, or Property. The code I am using is below:
Sub Command1_Click()
SubToCall = "Call" & "Me"
SubToCall
End Sub
Sub CallMe()
' Process data here
End Sub
Any suggestions on how to make this work would be greatly apprecieated.
Thanks
-
vbwannabe
I dont know if what you want to do is possible. What exactly are you trying to do? there is an addressof() function that allows you to get the address of a function. or maybe you can use a case structure for example
Private Sub Command_Click()
Select Case VaribleName
Case FunctionNameOne
FunctionNameone()
Case FunctionNameTwo
FunctionNameTwo()
Case Else
Messagebox "No such Function Name"
End Select
End sub
sorry best i can do :(
-
I need to process and evaluate incoming comm port data 1 of 16 different ways. The process varies widely so I will not have any common code. The process will not change during the program execution and is determined at startup by a series of radio buttons. My intent was to run the proper procedure based on radio button input. I would assign a variable to the radio button which would execute the correct procedure on incoming comm port data. I did not want to evaluate a select case for each data input since the process would remain constant throughtout the program execution. I appreciate your input. Thanks for taking the time to respond.
-
The CallByName Function will do what you want. You could even append the index of the option button to the name of the function too.
-
-
Thanks for the input. I will give this a try.
-
Use CallByName
eg.
Code:
Public Function calval(ByVal x As Integer) As Integer
calval = x * x
End Function
Private Sub Form_Load()
MsgBox CallByName(Form1, "calval", VbMethod, 10)
End Sub