Results 1 to 6 of 6

Thread: Fire a sub by its name (in string)

  1. #1

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Fire a sub by its name (in string)

    Hi,

    as you can see, ive an another nice question, so ive a sub, and ive the subs name in a string ->

    String = "Form1.MySub"

    and i'm want to fire THAT sub, so only on Form1.. but how can i do it?

    have you any idea?

    Thx,Jim.

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    look at the function CallByName.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284
    CallByName Form1, "MySub", VbMethod, "Yeah! :>"


    thanks man!

    anyway if ive the form objects name in a string, how can i get the form Object to use with callbyname?

  4. #4
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    I think you'd have to use a select case or something like that.

    Or you could loop through all the forms, and look for the one with the correct name.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  5. #5

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284
    Originally posted by SLH
    Or you could loop through all the forms, and look for the one with the correct name.
    cool idea, thanks

    jim.

  6. #6
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    Something i found a while back. have fun
    VB Code:
    1. '****** In a form
    2. Option Compare Text
    3. Option Explicit
    4.  
    5. 'VB6 IDE
    6. Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
    7.  
    8. ' For VB5 IDE
    9. 'Declare Function EbExecuteLine Lib "vba5.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
    10.  
    11. ' FOR Access 97/VBE.dll clients like Word 97 and Excel 97
    12. 'Declare Function EbExecuteLine Lib "vba332.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
    13.  
    14. Private Sub Combo1_Change()
    15.  
    16. End Sub
    17.  
    18. Private Sub Combo1_Click()
    19.     Text1.Text = Combo1.Text
    20. End Sub
    21.  
    22. Private Sub Command1_Click()
    23.     If ExecuteLine(Text1.Text) = True Then
    24.         Label1.Caption = "Text successfully run"
    25.     Else
    26.         Label1.Caption = "Failed to run the code"
    27.     End If
    28. End Sub
    29.  
    30. Private Function ExecuteLine(ByVal strCode As String, Optional ByVal blnCheckOnly As Boolean = False) As Boolean
    31.     ExecuteLine = EbExecuteLine(StrPtr(strCode), 0&, 0&, Abs(blnCheckOnly))
    32. End Function
    33.  
    34. Private Sub Form_Load()
    35.     With Combo1
    36.         .AddItem "HiddenProc1"
    37.         .AddItem "?HiddenProc2"
    38.         .AddItem "x = MsgBox (" & Chr(34) & "Hello World!" & Chr(34) & ", vbExclamation + vbOkCancel, " & Chr(34) & "Test MsgBox" & Chr(34) & "): MsgBox x"
    39.         .ListIndex = 0
    40.     End With
    41.     Command1.Caption = "Execute"
    42.     Label1.Caption = "Nothing run yet..."
    43. End Sub
    44.  
    45.  
    46. '****** In a module
    47. Option Explicit
    48.  
    49. Public Sub HiddenProc1()
    50.     Dim intCounter As Integer
    51.    
    52.     For intCounter = 0 To 10
    53.         Debug.Print intCounter
    54.     Next
    55. End Sub
    56.  
    57. Public Function HiddenProc2() As String
    58.     HiddenProc2 = "This is what HiddenProc2 returns"
    59. End Function

Posting Permissions

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



Click Here to Expand Forum to Full Width