CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Aug 2002
    Location
    Lucknow
    Posts
    129

    Cool Execute function as string

    Hi buddies.

    I have a little problem.I want to execute a function(defined by me)
    as a string.
    Acctually I have given function name nad parameter in .INI file.parameters may be field of a table in database.So by fetching all information from .INI file and database n\and after concatenating I got whole function string now I want to execute
    this.
    Regards
    Yujvendra

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332
    Take a look at the CallByName function.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Aug 2002
    Location
    Lucknow
    Posts
    129

    Cool Reply

    Hi buddy.
    Thanx for response.
    But CallbyName works for function of some object not for simple user defined function.If u find some way pls. response me.

    Regards
    Yujvendra

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Play with this

    Code:
    Option Explicit
    'add a reference to microsoft script control
    'yes, they call it a control, but you can add a reference to it
    'via menu->project->references
    Private myScript As ScriptControl
    Private Sub Form_Load()
    
    Set myScript = New ScriptControl
    'you need to tell it you want to use vbscript
    'and not microsoft Jscript
    myScript.Language = "Vbscript"
    'add your sub or your function
    'remember you DO NOT have the "private" 
    'or "public" statement here 
    'do not forget  you do not have data type also,
    'but only variant variables
    '(but you can use Cstr, Cdbl, Cbool and so on
    'if you need)
    myScript.AddCode "sub showmyMessage" & vbCrLf & "MsgBox ""Ehy, does this really works?"" " & vbCrLf & "end sub"
    'Now run the added sub or function:
    myScript.Run "showmyMessage"
    Set myScript = Nothing
    End Sub
    By the way: did you already vote?? (see below, red lines)
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332
    The CallByName function works with Any function, including your own.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054
    Marketing our skills - please participate in the survey and share your insights
    -

  7. #7
    Join Date
    Mar 2001
    Location
    County Durham, England
    Posts
    238

    Question Nearly There . . .

    Cheers Cimperiali


    I have adapted that code but get error 424

    Object required: 'DataReportEEList'

    The code now looks like ;


    Private Sub ScriptReport(RepName As String)

    Set myScript = New ScriptControl
    myScript.Language = "Vbscript"

    myScript.AddCode "Sub ShowReport" & vbCrLf & RepName & ".Show 1" & vbCrLf & "End Sub"

    'Now run the added sub or function:
    myScript.Run "ShowReport"
    Set myScript = Nothing

    End Sub




    What am I doing wrong ? the report is in the project and the name is the same ?

  8. #8
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    Hi Cimperiali

    I tried your code, and it worked well.
    But when I tried changing the procedure to a function which takes parameters, I encountered an error like - "Object does not support this property or method", or so



    Suhaib

  9. #9
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    GOT IT MYSELF !!

    works something like this .....

    myScript.run "myfunction", <param1>, <param2>, ...... <paramn>


    Thanks

    Suhaib

  10. #10
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    But Signor ...

    How do you hold the values returned by this funtion ???


    Suhaib

  11. #11
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Suhaib,

    The run method automatically returns the value from the function
    Busy

  12. #12
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Judgey,

    If you want to access an object (say a form) inside the script try to add it first by issuing the AddObject method

    s.AddObject "MyForm", Form1
    Busy

  13. #13
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    Thread1

    Could you tell me how I can store this return value to a variable and display it ?


    Suhaib

  14. #14
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Suhaib,

    myScript.AddCode "function hello(byval name)" & vbCrLf & "hello = ""hello "" & name" & vbCrLf & "end function" & vbCrLf

    varRet = myScript.run("hello", "Suhaib")

    MsgBox varRet

    Got it?
    Busy

  15. #15
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Lightbulb

    The vba.CallByName is applicable too for methods (sub/func) within the object. The object can be a form.
    Busy

Page 1 of 2 12 LastLast

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

Featured