?
Solved

executing strings

Posted on 2003-03-08
8
Medium Priority
?
235 Views
Last Modified: 2010-05-01
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
Comment
Question by:vitanza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 8096333
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 8096338
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
 

Author Comment

by:vitanza
ID: 8096555
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:keyen
ID: 8096753
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
 
LVL 22

Accepted Solution

by:
rspahitz earned 200 total points
ID: 8096777
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
 
LVL 2

Expert Comment

by:navneet77
ID: 8096778
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
 
LVL 2

Expert Comment

by:navneet77
ID: 8096786
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
 
LVL 2

Expert Comment

by:DanAvni
ID: 8096909
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question