how can i call a private sub using a variable that contains the name of the sub?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Reg9
    New Member
    • Feb 2010
    • 2

    how can i call a private sub using a variable that contains the name of the sub?

    Private Sub cmdProcessEvent s_Click()
    strEventID = "Audit0001"

    'This works:
    Audit0001

    'These do not:
    Eval (strEventID)
    Application.Run strEventID

    End Sub

    Private Sub Audit0001()
    MsgBox "Here i am"
    End Sub
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    Not sure it is possible with a private sub but you might get it working with callbyname function... doubt it,... but


    Good Luck

    Comment

    • yarbrough40
      Contributor
      • Jun 2009
      • 320

      #3
      If you find an answer to this on your own, please post it. I would also love to know how to do this as well....

      Comment

      • Reg9
        New Member
        • Feb 2010
        • 2

        #4
        This worked for me!
        Thank you vb5prgrmr very much for your suggestion!
        The code that i got to work is listed below.

        For others who may want to do this, here are some points to be aware of:

        1 I did quite a bit of searching before posting my own question. Other people seemed to be satisfied when they were pointed to Eval or Application.Run . I do not know why these worked for others and not for me. Therefore, i do not know under what circumstances they might be a better choice.

        2 I am working in Access 2003 VBA within a Form Module.

        3 In order to get CallByName to work, i had to change the subroutine from Private to Public, even though it is in the same form module.

        4 The "object" is the current form, Me.
        The "procname" is my public subroutine that is named in my variable.
        The "calltype" is VbMethod

        Here is my test that worked:

        Private Sub cmdTest_Click()
        Dim strSubNm As String
        strSubNm = "Test"
        CallByName Me, strSubNm, VbMethod
        End Sub

        Public Sub Test()
        MsgBox "It worked!"
        End Sub

        Comment

        • yarbrough40
          Contributor
          • Jun 2009
          • 320

          #5
          very cool! got it!

          Comment

          Working...