Results 1 to 18 of 18

Thread: Eval

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263

    Eval

    Dire need of help guys. I am trying to assign a string value to an existing variable, using the ScriptControl. Anyone know how to do this? Here's what I have (nuts and bolts of my problem):

    Code:
        Dim sc As New ScriptControl
        Dim aryMe(3) As Variant
        
        sc.Language = "VBScript"
        sc.AddObject "var", Form1
        sc.ExecuteStatement ("aryMe(0) = 'Hello'")
    As you can see, I'm surrounding the string "Hello" with single ticks...not working (syntax error). Any thoughts would be appreciated.

    When I run it with "var." in front of "aryMe(0)", I still get the same error.
    Last edited by j_jewell; Apr 22nd, 2004 at 03:34 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263

    ...

    If I could do this, this would be fine too:
    Code:
        Dim sc As New ScriptControl
        Dim aryMe(3) As Variant
        Dim myTempVar As Variant
        
        sc.Language = "VBScript"
        sc.AddObject "var", Form1
        myTempVar = "Hello"
        sc.Eval ("var.aryMe(0) = var.myTempVar")
    But alas, that produces a "Object doesn't support this property or method: 'var.aryMe'.

    If I take out the "var.", the error I get is: "Type Mismatch: 'aryMe'"

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263

    ...

    Code:
    Private Sub Form_Load()
        Dim sc As New ScriptControl
        Dim aryMe(3) As Variant
        Dim aryName As String
        Dim myTempVar As Variant
        Dim strElement, strLeft, strRight, str2Rip As String
        
        str2Rip = "aryMe(0) = Hello World"
        strLeft = Left(str2Rip, InStr(str2Rip, "=") - 1)
        strRight = Mid(str2Rip, InStr(str2Rip, "=") + 1)
        aryName = Left(strLeft, InStr(strLeft, "(") - 1)
        strElement = Mid(strLeft, InStr(strLeft, "(") + 1, (InStr(strLeft, ")") - InStr(strLeft, "(")) - 1)
        
        Select Case aryName
            Case "aryMe"
                aryMe(CInt(strElement)) = strRight
            Case "aryYou"
                aryYou(CInt(strElement)) = strRight
        End Select
    End Sub
    The above code will work, but it isn't dynamic, it only works if we will always know the names of the arrays, but that is part of the problem, we might have more arrays in the future and I really don't want to reprogram the .exe every time we add an array.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    What is your goal?
    Is it to simply take a string (that contains an Array name and its value for that element)
    and strip out the Array name?


    IThe reson I ask is that the string your stripping has an Array name that is also declared by you?????



    Anyways, the ExecuteStatement needs a 'statement', like:
    VB Code:
    1. Dim sc As New ScriptControl
    2.     Dim aryMe(3) As Variant
    3.    
    4.     sc.Language = "VBScript"
    5.     sc.AddObject "var", Form1
    6.     aryMe(0) = "if 1 > 2 then x = 5"
    7.     sc.ExecuteStatement aryMe(0)





    Bruce.
    Last edited by Bruce Fox; Apr 22nd, 2004 at 04:28 PM.

  5. #5
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Are you trying to access the variable? You would need to set the variable as public in the DECLARATIONS section.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263

    More Explanation

    I'm sorry, I should've explained my goals a little better.

    I have a program, which basically automates the creation of vb .exe files. It reads through an excel spreadsheet, and based upon whether the text in a cell is blue (meaning it's an input cell) or a formula (function) or other (label), I build the appropriate code.

    Now, I got down the path, and some of these spreadsheets are rather large. And, the number of labels and textboxes being created (there's a textbox for every input and a disabled textbox for every function) grew beyond 255...so, I had to use control arrays.

    Well, I can't run the functions on the textbox values themselves, without putting in some extra code, so instead, I created public arrays which mirror each control, and are dimensioned appropriately (string, double, etc.). Now then, the piece that I am on is the last piece, saving and opening the values from the form.

    I am saving the values into a proprietary file (e.g. ".xxx") and I have created a vb app that I have associated .xxx files with. This middle app is instantiated when a user double clicks a .xxx file, the middle app then calls the appropriate .exe (since the .xxx file will contain essentially two pieces of data, first, the filename and location that is being opened, second is all of the values in the following format:

    aryMyStringArray(0) = Abracadabra
    aryMyStringArray(1) = Hakuna Matata
    aryMyNumericArray(0) = 1
    aryMyNumericArray(1) = 5

    ...so, the appropriate executable is called, the executable then opens the ".xxx" file which was passed in the command string as an argument. Now, here's where you all come in. I'm reading down through these statements, which assign values to array variables, and I need a way to basically say:

    eval("aryMyStringArray(0) = 'Abracadabra'")

    ...obviously, as I have stated, that code does not work, I would like something that can utilize either the eval statement or the executestatement statement, so that my code can be dynamic and handle different named arrays...making the app a little more extendible for the future.

    Sorry again that I didn't explain better earlier, thanks for reading, and thanks in advance for your help.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    bump

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    in VB, be it VBScript or VBS, strings are enclosed in double quotes " not single quotes '

    So, sc.ExecuteStatement ("aryMe(0) = 'Hello'") won't work. It sees the first ' and it thinks it is a comment and so stops. But the line if then incomplete.... error.

    Try this instead:

    VB Code:
    1. sc.ExecuteStatement ("aryMe(0) = ""Hello""")
    Replace each ' with TWO double quotes. See if that helps.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263

    ...

    I tried it, but I got the error:

    "Type Mismatch: 'aryMe'"

    ...I wonder if it's trying to read aryMe as a sub or function since it's in the executestatement?

  10. #10
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    you don't understand. the script control does not 'know' your vb code. It can only access the objects that you added to it using addobject. You are trying to access an array in your sub, but the script control does not know that there is an array. There's no way to do what you want. There are workarounds. Add them in a public class module and scriptcontrol.addobject them to the scrip control. In the script, you would access them like you would access any object.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    you don't understand
    I do, though, see my second post:

    Code:
        Dim sc As New ScriptControl
        Dim aryMe(3) As Variant
        Dim myTempVar As Variant
        
        sc.Language = "VBScript"
        sc.AddObject "var", Form1
        myTempVar = "Hello"
        sc.Eval ("var.aryMe(0) = var.myTempVar")
    ...so, I was already trying the AddObject method, and using the item I added "var" to reference the array...I'll try it again with the new code techgnome gave me and see if it makes a difference...

  12. #12
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    It won't work. First of all, this has to be in declarations:

    VB Code:
    1. Dim sc As New ScriptControl
    2.     Dim aryMe(3) As Variant
    3.     Dim myTempVar As Variant

    Second of all, they have to be public:

    VB Code:
    1. Public sc As New ScriptControl
    2.     Public aryMe(3) As Variant
    3.     Public myTempVar As Variant

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    Thanks for the advice. I haven't been copying and pasting my exact code, though. The code that I have was declaring them as Public in the declarations, but the code I was testing with the other day (when I made these posts) was on a different machine and was just that, test code. Thanks again for the advice, though...and I'll see if techgnome's suggestion remedies the problem.

  14. #14
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    This works for me:
    VB Code:
    1. Option Explicit
    2. Public MyString As String
    3. Private Sub Form_Load()
    4.     MyString = "hi"
    5.     ScriptControl1.Language = "VBScript"
    6.     ScriptControl1.AddObject "var", Me
    7.     ScriptControl1.AddCode "msgbox var.MyString"
    8. End Sub

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    I still get the error message:

    "Object doesn't support this property or method: 'var.aryMe'"

    ...when I try the quotation suggestion from techgnome and the use of the addObject "var", frmName

    ...so, I'm at a loss.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    Yeah, that works for me too, but try referencing a value from an array such as this:

    Code:
    Option Explicit
    Public MyString(1) As String
    Private Sub Form_Load()
        MyString(0) = "hi"
        ScriptControl1.Language = "VBScript"
        ScriptControl1.AddObject "var", Me
        ScriptControl1.AddCode "msgbox var.MyString(0)"
    End Sub
    ...that's my problem, I need to use an array and I can't get it to work with an array.

  17. #17
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    A bit more complicated. Add a class module, put this code in it:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim arr(0) As String
    4.  
    5.  
    6. Public Property Get MyArr(Index As Integer) As String
    7.  
    8.     MyArr = arr(Index)
    9.  
    10. End Property
    11.  
    12. Public Property Let MyArr(Index As Integer, ByVal sValue As String)
    13.  
    14.     arr(Index) = sValue
    15.  
    16. End Property

    and then this code in the form:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim MyClass As New Class1
    5.  
    6.     MyClass.MyArr(0) = "hi"
    7.     ScriptControl1.Language = "VBScript"
    8.     ScriptControl1.AddObject "var", MyClass
    9.     ScriptControl1.AddCode "msgbox var.myarr(0)"
    10. End Sub

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    I think that might work! Thanks for the help!

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