|
-
Apr 22nd, 2004, 03:25 PM
#1
Thread Starter
Hyperactive Member
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.
-
Apr 22nd, 2004, 03:32 PM
#2
Thread Starter
Hyperactive Member
...
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'"
-
Apr 22nd, 2004, 03:55 PM
#3
Thread Starter
Hyperactive Member
...
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.
-
Apr 22nd, 2004, 04:16 PM
#4
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:
Dim sc As New ScriptControl
Dim aryMe(3) As Variant
sc.Language = "VBScript"
sc.AddObject "var", Form1
aryMe(0) = "if 1 > 2 then x = 5"
sc.ExecuteStatement aryMe(0)
Bruce.
Last edited by Bruce Fox; Apr 22nd, 2004 at 04:28 PM.
-
Apr 22nd, 2004, 06:04 PM
#5
The picture isn't missing
Are you trying to access the variable? You would need to set the variable as public in the DECLARATIONS section.
-
Apr 22nd, 2004, 09:56 PM
#6
Thread Starter
Hyperactive Member
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.
-
Apr 23rd, 2004, 08:34 AM
#7
Thread Starter
Hyperactive Member
-
Apr 23rd, 2004, 08:57 AM
#8
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:
sc.ExecuteStatement ("aryMe(0) = ""Hello""")
Replace each ' with TWO double quotes. See if that helps.
TG
-
Apr 23rd, 2004, 09:05 AM
#9
Thread Starter
Hyperactive Member
...
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?
-
Apr 23rd, 2004, 08:36 PM
#10
The picture isn't missing
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.
-
Apr 24th, 2004, 11:51 AM
#11
Thread Starter
Hyperactive Member
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...
-
Apr 24th, 2004, 12:12 PM
#12
The picture isn't missing
It won't work. First of all, this has to be in declarations:
VB Code:
Dim sc As New ScriptControl
Dim aryMe(3) As Variant
Dim myTempVar As Variant
Second of all, they have to be public:
VB Code:
Public sc As New ScriptControl
Public aryMe(3) As Variant
Public myTempVar As Variant
-
Apr 24th, 2004, 04:44 PM
#13
Thread Starter
Hyperactive Member
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.
-
Apr 24th, 2004, 04:54 PM
#14
The picture isn't missing
This works for me:
VB Code:
Option Explicit
Public MyString As String
Private Sub Form_Load()
MyString = "hi"
ScriptControl1.Language = "VBScript"
ScriptControl1.AddObject "var", Me
ScriptControl1.AddCode "msgbox var.MyString"
End Sub
-
Apr 24th, 2004, 05:06 PM
#15
Thread Starter
Hyperactive Member
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.
-
Apr 24th, 2004, 05:08 PM
#16
Thread Starter
Hyperactive Member
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.
-
Apr 24th, 2004, 06:04 PM
#17
The picture isn't missing
A bit more complicated. Add a class module, put this code in it:
VB Code:
Option Explicit
Dim arr(0) As String
Public Property Get MyArr(Index As Integer) As String
MyArr = arr(Index)
End Property
Public Property Let MyArr(Index As Integer, ByVal sValue As String)
arr(Index) = sValue
End Property
and then this code in the form:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim MyClass As New Class1
MyClass.MyArr(0) = "hi"
ScriptControl1.Language = "VBScript"
ScriptControl1.AddObject "var", MyClass
ScriptControl1.AddCode "msgbox var.myarr(0)"
End Sub
-
Apr 26th, 2004, 10:09 AM
#18
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|