Page 1 of 2 12 LastLast
Results 1 to 40 of 57

Thread: Visual Basic and Dynamic Code.....

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    3

    Arrow

    I'm having quite an issue with Visual Basic.

    I have a custom library which has a class in it. This class has several properties which I am interested in looking at. There is no problem retrieving the values as follows:

    ....vbcode....
    x = className.propertyName
    ..../vbcode...

    but this forces me to call each property seperately. What I would rather do is have the user type in a text box the property name he chooses and have x loaded with that property value.

    ....vbcode...
    txt1.text = "propertyName"
    x= className. & txt1.text
    ..../vbcode...

    This of course does not work. What I am wondering is if there is any way to make this work. To execute a string as a piece of vb code?

    Any suggestions or comments ? I'm really up the wall on this one.

  2. #2

    Talking AH HA!

    This one goes back to when I learnt BASIC on an Amstrad CPC 6128 at the age of 12.

    I wanted to write a program that did somthing like what you wanted to do. I wanted the user to input a string and the the program to be able to show what was in the variable with the same name as that string.

    i.e

    let a=12

    <<USER INPUTS "A">

    Program send back 12.

    I just guess basic was never ment to analyse it's self, it wouldn't be basic.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    3
    so does this mean that I'm stuck with getting the user's input and using a silly case statement to execute a "code" version of that string. It sort of makes me wonder how a language could be so versitile without being able to perform such a simple task....(I guess it's also sort of funny that I have just now learned this now having been programming in VB ever since V2.......)

  4. #4
    I'm not sure you can even do that - I never worked it out. Basic really can suck

  5. #5
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    No can do in VB... you can in FoxPro though.
    -Shickadance

  6. #6
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41

    Question Dynamic VB Code

    I have tried to do the same thing: reading text from a TextBox and then entering it into a file and reading the file again. All using the Ms Script Control and help from this scriptlet.
    But unfortunately I got an error telling me an object is required: it seems I have no use from the controls in my form using the script engine. Can anyone help me here? E.g. telling me how to call a certain object or ActiveX control (.OCX) from the script file.

    Thanks!
    -=<][])a ][}yte>=-

  7. #7
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41
    P.S. here's the link to the scriptlet:
    http://members.aol.com/vbchenko/code...rce/script.txt

    and here's the code I used:

    Dim strcode As String

    Open Application.Path & "\test.txt" For Input As #1
    Do Until EOF(1)
    Line Input #1, Data
    strcode = strcode & Data & vbCrLf
    Loop
    Close #1

    ScriptControl1.Language = "VBScript"
    ScriptControl1.AddCode (strcode)

    'Hello is the sub routine or function
    ScriptControl1.Run ("hello") ' With "hello" being the script function's name
    -=<][])a ][}yte>=-

  8. #8
    Tygur
    Guest
    Actually, it can be done! It's a new feature in VB6.

    Look into CallByName.

    Here's a sample usage:
    MsgBox CallByName(Form1, "Caption", VbGet)

  9. #9
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41
    Thanks a lot!

    I am actually running this code from the VBA editor in Word 2000. But I've found the CallByName function, and used it as described:

    CallByName(frmUpload, txtCode.Value, VbGet)

    With frmUpload being my form and txtCode.Value being the code from the textbox.
    Unfortunately it returns an error informing me that the method is not supported by the object.
    Any ideas?

    Thanks!!
    -=<][])a ][}yte>=-

  10. #10
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41
    Hold on! I have it:

    CallByName(txtCode, "Value", VbGet)

    But VB won't just execute this code: it expects an equal sign (=).
    If I however try this:
    a = CallByName(txtCode, "Value", VbGet) a

    It expects a Sub or Function or something. What is the prob now?

    Bye!
    -=<][])a ][}yte>=-

  11. #11
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41

    Talking

    Of course, where's my brain?
    To use it stand-alone, you have to put it like this:

    CallByName txtCode, "Value", VbGet

    Although the code doesn't execute yet (like this sitting in a Sub by itself). What's wrong?

    Sincerely,
    -=<][])a ][}yte>=-

  12. #12
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    VB Code:
    1. a = CallByName(txtCode, "Value", VbGet) 'NO TRAILING A!!!


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  13. #13
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41
    Thanks a mil. but still it doesn't appear to execute the (VB)code from within the textbox


    Any ideas?
    -=<][])a ][}yte>=-

  14. #14
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    MsgBox CallByName(classname, txtCode, VbGet)

    Do you want to EXECUTE VB CODE, LIKE:

    VB Code:
    1. MsgBox "...GGGHHHJJJKKK"
    2. Di m i As Long
    3. i = 100


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  15. #15
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41
    I want to execute it like:

    Sub hello()
    CallByName txtCode, "Text", vbGet
    End Sub


    That OK? Because a messagebox displayed the contents correctly and txtCode is the name of my textbox control.

    TNX!!
    -=<][])a ][}yte>=-

  16. #16
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    VB Code:
    1. Sub hello()
    2. CallByName txtCode, "Text", vbGet
    3. End Sub

    Yes, it's OK.



    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  17. #17
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41

    Angry

    It still doesn't execute

    What is the problem?

    I've tried to put a few simple pieces of VB into the textbox but it won't produce anything

    e.g.
    MsgBox "I'm going NUTS! "
    -=<][])a ][}yte>=-

  18. #18
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Sub Hello()
    MsgBox CallByName(txtCode, "Text", vbGet)
    End Sub

    If that doesn't work, tell me WHAT ARE YOU TRYING TO DO?!


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  19. #19
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    there is a rotine in the vb dll that does this, it's like an api, but i don't remeber the name, it's what the immediate code window uses

    look it up somewhere like on yahoo, or search here, i've posted it before
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  20. #20
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41

    Cool

    TNX, guyz!
    Microbasic, I'm trying to actually RUN the (VB)code I typed into the (multline) Textbox. And many thanks for all the suggestions!

    And, Gwdash, thanks for the suggestion, but what keywords exactly do I use to find the routine?

    Later!!
    -=<][])a ][}yte>=-

  21. #21
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by AndrzejAntoszkiewicz
    What I am wondering is if there is any way to make this work. To execute a string as a piece of vb code?

    Any suggestions or comments ? I'm really up the wall on this one.
    I wanted the same capability and after a lot of checking around, I found two options that are close:

    1) Script using the script control

    2) VBA , which requires that you license VBA

    As far as I know, you cannot execute user input VB code from your application (it has to be VBA or Script). If anyone finds otherwise, please let me know.

    Attached is a small example that executes user input code. Not sure if this will help you or not. Good Luck!

  22. #22
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    here it is:
    VB Code:
    1. 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
    2.  
    3. ' = = = =  For VB5
    4. 'Declare Function EbExecuteLine Lib "vba5.dll"  (ByVal pStringToExec As Long, ByVal Foo1 As Long,  ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
    5.  
    6. ' = = = =  For Access 97/VBE.dll clients as Word 97 and Excel 97
    7. 'Declare Function EbExecuteLine Lib "vba332.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
    8.  
    9. Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
    10.     FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
    11. End Function
    12.  
    13. Private Sub Form_Load()
    14.     Dim TheString As String
    15.    
    16.     TheString = "MsgBox ""This is a user generated dialog"""
    17.     Debug.Print FExecuteCode(TheString, False)
    18. End Sub
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  23. #23

  24. #24
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by gwdash
    here it is: ...
    Thats awesome! I noticed that it doesnt recogize variables from the calling program, though.

    Two questions reagrding this API:

    1. Is there any way to execute a block of code?

    2. Is there anyway to pass variables to/from the executed code?

    Thanks for the info!

  25. #25
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    1. try multiple calls to the function, one for each line

    2. yes:
    VB Code:
    1. TheString = "msgbox ""This is a " & MyVariable & " message"""
    2. Debug.Print FExecuteCode(TheString, False)
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  26. #26
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by gwdash
    1. try multiple calls to the function, one for each line

    2. yes:
    VB Code:
    1. TheString = "msgbox ""This is a " & MyVariable & " message"""
    2. Debug.Print FExecuteCode(TheString, False)
    Thanks gwdash, but thats not quite what I meant. In your example the calling prog is converting the the variable to its contents before sending it to the API call.

    suppose I wanted to execute this code

    x=y+z

    and I wanted to supply y and z and return the x for use by the calling prog. Also Id like the code string to have access to all prog properties (text1.text, etc) . Can this be done?

    Thanks again for the information. This will help me out a lot if I can figure out how to make it work.

  27. #27
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    think of it as typing into the immediate window, anything you can do there, you can do with this function, it's the function used by the immediate window
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  28. #28
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444
    I think that is a very useful function.

    Thank you for the information.
    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

  29. #29
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    I hate to be such a bother about this, but I can really use this if I can figure it out. In the following code, why does debug.print "hello" instead of the contents of text1.text?

    Thanks for any help!

    VB Code:
    1. 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
    2. Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
    3.     FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
    4. End Function
    5.  
    6. Private Sub Command1_Click()
    7.     Dim a As String
    8.     a = "hello"
    9.     Dim TheString As String
    10.     TheString = "a=text1.text"
    11.     Debug.Print a
    12. End Sub

  30. #30
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    make a a public varaible, and put in the FExecuteCode line, and try again
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  31. #31
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    a = "hello"
    Dim TheString As String
    TheString = "a=text1.text"
    Debug.Print a


    Because you are Debug.Print ing a, which is = "hello"
    If you debug print TheString, you will get "a=text1.text"

    -Lou

  32. #32
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    that was embarrassing!

    Now why doesnt this work?

    VB Code:
    1. 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
    2. Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
    3.     FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
    4. End Function
    5.  
    6. Private Sub Command1_Click()
    7.     Dim a As String
    8.     a = "hello"
    9.     Dim TheString As String
    10.     TheString = "a=text1.text"
    11.     Debug.Print FExecuteCode(TheString, False)
    12.     Debug.Print a
    13. End Sub

  33. #33
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    make "a" a public variable
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  34. #34
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444
    sorry
    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

  35. #35
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    ok, tu=ried this and i get an "object required" error. any ideas?

    VB Code:
    1. Public a As String
    2. 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
    3. Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
    4.     FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
    5. End Function
    6.  
    7. Private Sub Command1_Click()
    8.     Dim TheString As String
    9.     a = "hello"
    10.     TheString = "a = text1.text"
    11.     Debug.Print FExecuteCode(TheString, False)
    12.     Debug.Print a
    13. End Sub

  36. #36
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    is this is a form? make it Private, not Public, i should have said object scope. also try making it like

    form1.text1.text
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  37. #37
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by gwdash
    is this is a form? make it Private, not Public, i should have said object scope. also try making it like

    form1.text1.text
    OK the following gets rid of the error, but a debug.prints as "hello" . I've tried a both as a Public and Private string.

    This is a form with 1 textbox and 1 command button.

    Thanks for hanging in there with me so long.

    VB Code:
    1. 'Public a As String
    2. Private a As String
    3. 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
    4. Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
    5.     FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
    6. End Function
    7.  
    8. Private Sub Command1_Click()
    9.     Dim TheString As String
    10.  
    11.     a = "hello"
    12.     TheString = "a = form1.text1.text"
    13.     Debug.Print FExecuteCode(TheString, False)
    14.     Debug.Print a
    15. End Sub

  38. #38
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by badgers
    sorry
    no worries ... I deserved to be embarrassed on that one!

  39. #39
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    i tried adding,

    FBExecuteCode("Debug.Print a", False)

    and it worked as you wanted
    weird
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  40. #40
    Member
    Join Date
    Aug 2001
    Location
    HH
    Posts
    41

    Thumbs up

    Two questions:

    1) Where must the file VBA6.DLL be located? In windows\system32?
    When it is tells me all ties with the client program are broken.
    BTW: I run the code from the Word VBA editor.

    2) How do you get the source code to display the way it does in the VBeditor? D'you just paste it?

    It seems a heated debate has been rekindled

    Thanks, all!
    -=<][])a ][}yte>=-

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