Dynamically create a procedure or function

Posted on 2005-05-04
Last Modified: 2010-05-02
Anyone know how to do this?  I want to create a procedure at startup.

Let's say...

Private Sub HelloWorld()
Dim sMsg as string
sMsg = "Hello World"
MsgBox sMsg
End Sub
Question by:born4code
    LVL 14

    Expert Comment

    by:Shiju Sasidharan
    LVL 14

    Accepted Solution

    hi try this
    'Goto Project Menu ->References
    'add reference Microsoft Script Control 1.0
    Private Sub Command1_Click()
        Dim sc As New ScriptControl
        sc.Language = "VbScript"
        sc.AddCode _
            "Function HelloWorld()" & vbCrLf & _
            "    Dim sMsg" & vbCrLf & _
            "    sMsg = ""Hello World""" & vbCrLf & _
            "    MsgBox sMsg" & vbCrLf & _
            "End Function"
        sc.Eval "HelloWorld()"
    End Sub


    LVL 2

    Author Comment

    Thank you. Works great.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now