Declare variable from a string

Posted on 2002-07-30
Last Modified: 2010-05-01
In a function, i receive a string. In this function, i need to declare a variable who will get the name of what this string contain. For example, if i receive a string who contain "testing", i will have to declare a variable who will be named testing like if i done dim testing. How can i do that, it's so easy in asp or php.

Question by:mammouth
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

Expert Comment

ID: 7187939
You cannot really do that in VB using one of the "simple" data types like String. However, you can create a variable of a more complicated data type.

For example, you can use New to create a new instance of an object. You could make a class called DynamicVariable and give it Name and Value properties. Then when you need to make a new "testing" variable, you can do something like this:

    Dim new_variable As DynamicVariable

    Set new_variable = New DynamicVariable
    new_variable.Name = "testing"
    new_variable.Value = "whatever"

Store the variables in a Collection so you can get them and their values later.
LVL 70

Expert Comment

by:Éric Moreau
ID: 7187942
This can't be done. If you think about it, VB is compiled and variables needs to be declared before the compilation occurs.

If you tell us what is your need, maybe we will be able to suggest you a workaround.

Author Comment

ID: 7188001
Here is an example of my code

I have 5 checkbox chk0, chk1, chk2, chk3, chk4 each one is an array of a different size. ex.


These checkbox are on cell of different grid on sstab.

For now i have this code

select case
    case 0:
        for i=0 to ubound(chk0)
        for i=0 to ubound(chk1)
end select

i want to be able to put this with something like that

myvar="chk" &
for i=0 to ubound(myvar)

It is possible, When i try the answer from RodStephens, i get an error.

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 100 total points
ID: 7188018
try this:

myvar="chk" &
for i=0 to ubound(controls(myvar))


Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month4 days, 21 hours left to enroll

635 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