Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2004
    Location
    Huntington, IN
    Posts
    54

    Unanswered: Call a sub using variable name

    Is there a way to call a specific routine using a variable?

    For instance, my routine may be working varControl which may represent Control1, Control2 or Control3. After varControl is updated, I want to go on to the appropriate afterupdate routine (Control1_AfterUpdate, Control2_AfterUpdate...etc). Essentially, I need to call varControl & "_AfterUpdate"

    I can't seem to trial-and-error this, nor could I find a thread on this topic.

    Any help would be appreciated.

    Thank you.

    Matt

  2. #2
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1
    Hi

    I am not sure if this is what you mean, but you can write a private sub in the form module that takes a parameter when called, so you can determine where it was called from, and execute the relevant code (Select Case ?).

    Then call the sub from the AfterUpdate even of all the required controls with the relevant parameter specified for that control.

    Would that do the job ?


    MTB

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I was about to suggest that you simply pass a parameter to the function.
    Code:
    Function Test(NameOfControl As String)
    
    If NameOfControl = ___ Then
    '(or case statement)
    
    End Function
    
    ...
    
    Call Test(me.activecontrol.name) 'unsure of this line
    Call Tes(<write your control name here>) 'this might be simpler
    correct my syntax please
    George
    Home | Blog

  4. #4
    Join Date
    Mar 2004
    Location
    Huntington, IN
    Posts
    54
    Thanks MTB and georgev: that solution WILL work! I've got about 15 possible controls that the variable may represent, so the case statement won't be that bad.

    Ideally, I was hoping to somehow directly call the appropriate sub with the sub name 'created' via concatenation of string variables. That would be cleaner and would avoid having to hard-code the control names.

    But I'll take what works. Thanks for your time and ideas.

    Matt Wilcox

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    AfterUpdate is not really a sub or a method - it is an event procedure and is triggered by the event. Why do you need to fire it manually? Why not put the code in the appropriate event and therefore have it fire automatically when the event ocurs?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Jan 2007
    Location
    California, USA
    Posts
    520
    There is a way to "execute" a routine that is in a variable. You would do this with the Eval() function. For example, if you want to execute a function named "rptEmployees()", your code would look like:
    Code:
    Dim FunctionToRun as String
    Dim ReturnVariable as Variant
      FunctionToRun = "rptEmployees"
      ReturnVariable = Eval(FunctionToRun)
    This code will run the function named in the "FunctionToRun" variable and return the results in "ReturnVariable"
    Hope this helps,

  7. #7
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I believe, though, it needs to be public and return a value (i.e. functions only).
    Testimonial:
    pootle flump
    ur codings are working excelent.

  8. #8
    Join Date
    Jan 2007
    Location
    California, USA
    Posts
    520
    Thanks Pootle Flump! I had assumed it would be public but much better to state it up front. I did always talk about running a function, but again I did not state functions only. It really does not NEED to return a value, but it does need to be a function. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •