Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a Sub Name as a variable 5

Status
Not open for further replies.

GulfImages

Technical User
Jul 9, 2004
60
0
0
US
Hi All,

I'm trying to trim down my code and am creating master funtions to aleviate redunancy.

I have a Sub that I want to have a variable that names another sub I want to run:

Code:
Sub NewRecord(NameofOtherSub, etc, etc)

If something happens then

NameofOtherSub

End If

I don't know if it should be:

Call NameofOtherSub(etc)
or
NameofOtherSub(etc)

in NewRecord(NameofOtherSub, etc, etc) does NameofOtherSub need to be declared as something or just a text string.

I hope this is understandable.

Thanks in advance,
Bobby


 
hmmmmmmmmmmmmmmmmmmmmmmm ... hardly seems like a TRIMMIING of code, just adding a layer of obfuscation ...





MichaelRed


 
I'm confused? Taking pages of different code that I've made for each form and condensing it into code that can be used on all forms is adding a layer of obfuscation?

I need to run a sub within a sub based on certain criteria and when it happens, I need the name of the sub to be passed form the variable in the main sub.

Is that considered a bad thing to do?
 
Like this.
[tt] execute NameofOtherSub[/tt]
If security is a concern - and it is often, should filter NameofOtherSub with some criteria.
 
Yes, there is some criteria before it runs. So It would be something like this?:

Code:
SubNewRecord(NameofOthersub, othervariable, other_variable)

Some criteria...

Execute NameofOthersub(somevariabledata)

End Sub

 
AFAIK, Execute is a VBS statement NOT implemented in VBA (as Eval too).
 
GulfImages said:
Taking pages of different code that I've made for each form and condensing it into code that can be used on all forms...

Making the subs Public in scope is the usual way of doing this. Then you can call the sub from anywhere.



 
PHV, I thought it should be Call instead of Execute as I had never seen Execute used for this. (In my limited scope).

Joe, I do understand about making subs public, thanks.

I guess I'm not explaining well.

I'm making a sub that can be used on many forms, when I do use this sub another sub will run inside of it, depending on what form it's working on. Hence the need to put the name of the second sub in the variable of the first sub.

Code:
MainSub(variable1, variable2, SecondarySub)

Which form I use determines which Sub is SecondarySub.

I'm thinking this:

Code:
MainSub(variable1, variable2, SecondarySub)

Do some stuff...

    If Criteria... then

        Call SecondarySub(variablewhatever)
            'The name of the sub is assigned 
            'in SecondarySub variable above.
    End If

End Sub

But this will not cause the SecondarySub to run, even without the criteria so I assume it's a syntax thing as that's where I usually screw up.

Any clearer now?

Thanks again for everyone taking a look.

Bobby
 
What about the Application.Run method ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I'm not sure if I need that, it's just another sub in the same database.

I just can't seem to explain this properly.

Say it was just MainSub() and inside I have criteria that tells CheckCustomer() to run, I would simply enter:

CheckCustomer or Call CheckCustomer() at the right place isnide of MainSub().

Since MainSub() is used in more than one form, I need to tell it to run CheckCustomer() or it may be CheckVendor(), etc... depending on the form.

So I'm thinking MainSub(othersub) othersub simply being a variable to hold the name of sub "CheckCustomer" or "CheckVendor" etc...

So when I use it, it would be something like MainSub(CheckCustomer) or MainSub(CheckVendor)

Maybe this can't be done.

 
I think what you are after is the CallByName function.
Code:
Public Sub HelloWorld()
    MsgBox "Howdy"
End Sub

Private Sub cmdCall_Click()
    CallByName Me, "HelloWorld", VbMethod
End Sub

Something I don't like about that is you have to make the subroutine you are calling Public in scope.

 
Thanks Joe, it's not a public vs private thing.

Using your example, I want to do something like this:

Sub cmdCall_Click(othersub)
CallByName Me, "othersub", VbMethod
End Sub

The 2 subs below would be my choices based on the form I'm using at the time:

Sub HelloWorld()
MsgBox "Howdy"
End Sub

Sub HelloSpace()
MsgBox "Howdy"
End Sub

Then I want to use my cmdCall_Click as below:

cmdCall_Click(HelloWorld) for one form but I would use
cmdCall_Click(HelloSpace) for another.

I hope I'm maling more sense now. I'm not ae a computer with Access on it right now so I can't test it. What you show may be right.
 
Joe,
I did not know about the "callbyname" function. Very interesting, and I think it does what is needed.
Gulfimages,
To go back to the original question, there is very little difference between using the "Call" statement before the name of a subroutine. It is not required. The call statement before the name of a subroutine has one subtle difference. If the sub is a function and it returns a value then using the Call statement will discard the value. Either way, you can not pass the name of the subroutine as a variable and then execute it as you originally proposed, but the call by name function may work. You could also use a select case statement. You could then pass the name of the subroutine and determine which procedure to call.
 
>[PHV]AFAIK, Execute is a VBS statement NOT implemented in VBA (as Eval too).
Yes? sorry!
 
Bobby,

You can use the Application.Run command to run a Function. Your code would look like this.

'Inside your forms:
Sub cmdCall_Click(othersub as String)
Application.Run othersub
End Sub

'Inside of a module
Function HelloWorld()
msgbox "Hello World"
End Function

Function HelloSpace()
Msgbox "Hello Space"
End Function

Let me know if this works for you. I have used something like this in a program that I use daily.

Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top