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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use a Variable to Refer to a Sub Procedure

Status
Not open for further replies.

Bonediggler1

Technical User
Jul 2, 2008
156
US
Hello-

Is it possible to use a variable to refer to a sub procedure? VBA only seems to recognize the literal name when compiling the code. E.G:

Dim strSubName as String
Dim strCustName as String 'Customer name

strSubName = strCustName & "_Process" 'Literal sub name

Select Case strCustName
...
Case "A"
Call strSubName
...
End Select

In this case strSubName would translate into "A_Process", which is the literal name of another sub procedure. However when compiling, the error "Expected sub, function, or property" pops up.

Thank you!
 
This sounds a bit unorthodox but you could try use the Eval() function like:
Code:
Sub CallAnother()
    Dim strSubName As String
    Dim strCustName As String 'Customer name
    strCustName = "A"
    strSubName = strCustName & "_Process()" 'Literal sub name
   
    Select Case strCustName
    Case "A"
       Call Eval(strSubName)
    Case Else
        '
    End Select

End Sub
Public Function A_Process()
    MsgBox "Test"
End Function

Duane
Hook'D on Access
MS Access MVP
 
Why would you want to do this? It seems like a work around for some poor programming construct. I am sure there is a better way to get the same results.
 
Given my programming skills, I am pretty sure it is a poor programming construct. Perhaps you could offer a solution?
 
Ok...perhaps I should have started with this.

Anyway, to make a long story short I have a variable "strCustName" and 5 customer-specific sub procedures used to process their files after import. The 5 sub procedures are all named like "Sub Customer Name_Process".

The sub procedures are called from a case statement based on strCustName. So instead of writing,

Select Case strCustName
Case A
Call A_Process
Case B
Call B_Process

...and so on, I would prefer:

Select Case strCustName
Case A, B
Call strCustName & "_Process"
....
End Select

It just saves extra code.

Thanks again for the help!


 
You should never hard code against information in your database - it's a maintenance nightmare!

A better solution is to add some new field to the Customer record, such as Category, and go to the correct subroutine based on that.

(And please, please don't tell me you create a new subroutine for each customer.)
 
dhookum/JoeAtWork-

Thanks for the valuable advice. The problem I am facing is that the DB is used only for importing & processing files that come from various customers in various formats, and then using VBA to transform their data into a standardized output format that is in turn loaded into our A/R system - I am not storing data.

Given this scenario, how would I avoid storing these customer-specific processing routines in different sub procedures?
 
I agree with Remou that you may have a case. However, I generally try to build tables and process that all might identify the structure of the imported files. I always assume the structures will change and I would much rather change some data than dig through code.

Duane
Hook'D on Access
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top