• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

executing strings

I was wondering if any of you guys know how to execute a string in VB6.

I think there's a method in VBScript that looks like this: Execute(string)

so I could write this:

Execute("Dim var1")

and the variable would get dimensioned

I would appreciate any comments or tips.
0
vitanza
Asked:
vitanza
1 Solution
 
rspahitzCommented:
I've heard that you can use the Scripting Object to achieve this, but:
   Execute("Dim var1")

would simply allocate space for a variable, them throw it away (since the execution has completed.  Maybe you meant:
   Execute("Dim " & var1)
?

Anyway, try adding the Reference "Microsfot Scripting Runtime" to your project, then instantiate it and use it as you indicated.
0
 
rspahitzCommented:
Hmm...after reviewing your Q, I have to wonder if you're trying to dynamically create a variable.  To do this, simply:

Dim SomeVariable() as String ' or Variant if type unknown
dim intArrayCntr as integer

Redim SomeVariable(0)

'...
intArrayCntr = ubound(SomeVariable) + 1

' Create new variable
redim SomeVariable(intArrayCntr)
'...

0
 
vitanzaAuthor Commented:
This is what i'm trying to do:

I have around 50 textboxes in my form. They are named txt1, txt2, txt3,... I want to write them all, one by one, to a text file. I wanted to use the Execute(string) method like this:


Open fileName for Output as #1

For i = 1 to 50
   Execute("Print #1, txt" & i & ".Text")
   ' a sample execution would be: Print #1, txt16.Text
Next i

Close #1


Is there any easy way to do something like this?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
keyenCommented:
forms have control collections (controls)

you can do something like the following:

Private Sub Form_Load()
    Dim t As TextBox
    For Each t In Me.Controls
        MsgBox t.Text
    Next
End Sub
0
 
rspahitzCommented:
As keyen indicated or...

A better way is to set up the textboxes as a control array (with the Index values set as 1..50) then this:

dim iCntr as integer

Open fileName for Output as #1

for iCntr = 1 to 50
  print #1, txt(iCntr).Text
next iCntr

close #1

--
The main reason this is better is that you have a VB6 limit of 255 differntly-named controls on a form.  An array counts as one control whereas your method counts as 50.  If you later decided to expand to more than 255, you'd have to create a new form to hold them.

--
And just to be complete, to use your existing method put this code in an appropriate event:

   Dim txtbx As TextBox

   Open fileName for Output as #1

   For Each txtbx In Me.Controls
       print #1, txtbx.Text
   Next

   close #1
0
 
navneet77Commented:
you can do something like this

Open fileName for Output as #1

For i = 1 to 50
  Print #1, controls(txt & i).Text
 
Next i

Close #1


that should work.

Cheers
0
 
navneet77Commented:
in the above i foegot the quotes
Print #1, controls("txt" & i).Text


but as rspahitz said using control array is better unless you have some reason not to.
0
 
DanAvniCommented:
just an idea: if you have a set of commands that you need to execute, create a text file with a VBS extention and write these commands to the file. then using the shell command execute the VBS file. you will only have vbscript capabilities but it will allow you to run custom commands on runtime.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now