Question

VBScript - Building Variable Names

Asked by: david_levine

I have a need to keep many variables on 1 specific page. I have the code completely written and everything works.

For example, I have 12 variables for the same type of thing (no they aren't months ;) like Act1, Act2, Act3... Act12.

Is there any way to reference the variables more dynamically? I'm using VBScript. I'd like to do something like:

For x = 1 to 12
   Act&x = 0
Next

Instead of
Act1 = 0
Act2 = 0
Act3 = 0 ... Act12 = 0

Is there anyway to do that?

I know I can use arrays, but at some point I need to asign them to variables and I'd like to do it in a loop vs individually. I'm pretty sure I could use the Dictionary object as I have used in the past, but I want to try doing without that so the code is a bit more straightforward.

Lastly, is there anyway to put multiple statements on a line using VBScript in ASP?

Thanks!!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2000-06-17 at 09:18:03ID10475579
Tags

vbscript

,

variable

,

names

Topic

Active Server Pages (ASP)

Participating Experts
3
Points
100
Comments
21

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. vbscript dictionary bug?
    when passing a scripting dictionary in the session from one page to another, one of the items is having random text strings written over it. what was once "4523" becomes "INNT/System32/asp.dll" or "90." Is this a problem with the vbscript dict...
  2. Difference between ASP and VBScript
    What is exact difference between VBScript and ASP?
  3. VBSCRIPT And ASP?
    What Is Different Between VBSCRIPT And ASP? Which Should I Learn First? I Know HTML. VBSCRIPT <html> <head> </head> <body> <% @ Language=VBScript %> <% Response.Write "Hello World!" %> </body> </html> ASP <HTML...
  4. ADO and VBScript
    How do I use ADO with VBScript?
  5. Emulating the "this" pointer in ASP/VBScript
    I am seeking comments/ideas on how I might emulate the "this" pointer in ASP/VBScript. The "this" pointer is available in many object oriented languages including C++, Java and PHP, but is not available in ASP/VBScript. Question: If you need a "this&...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: MarinePosted on 2000-06-17 at 09:29:56ID: 2973771

make an array of them
Dim acct(12)
now you can access them like this
acct(0)
acct(1)
etc.

 

by: MarinePosted on 2000-06-17 at 09:31:20ID: 2973777

to assign value to all of them you can do it like this.
for i = 0 to 11
  acct(i) = "Value " & i
next
now acct will contain
value 0
value 1
value 3
value 4
etc

 

by: MarinePosted on 2000-06-17 at 09:32:17ID: 2973783

don't forget to declare it as i showed in first comment
Dim acct(12)

 

by: MarinePosted on 2000-06-17 at 09:34:26ID: 2973788

Marine changed the proposed answer to a comment

 

by: MarinePosted on 2000-06-17 at 09:35:41ID: 2973792

you can't build variable names like you want to. Why don't you want to use array ?

you can use concanitation on variables but that won't build a variable for you. use array.

 

by: mattykPosted on 2000-06-17 at 09:37:14ID: 2973797

The execute function is what you're lookig for here.  Here's a code snippet demoing this (this is clinet side for ASP the msgbox won't work):

<script language=vbscript>
      dim act3,act4
      dim x
      x=3
        act4=7
      execute("act" & x & "=5")
      msgbox act3:msgbox act4
</script>

Also note that I've included the syntax for putting multiple statements on a line - use a colon to distinguish each statement

 

by: MarinePosted on 2000-06-17 at 09:40:48ID: 2973810

thats not what he wants. you still have 2 declared separate variables he want to build them at run time which will generate them. It's not possible. All variables must be explicitly defined, thats not true in vb or asp but this means if one doesn't exist you can create one like you want.

 

by: mattykPosted on 2000-06-17 at 09:52:27ID: 2973873

The execute command will still do the job:

<script language=vbscript>
      Option explicit
      dim x
      x=3
      execute ("dim act"&x)
      execute("act" & x & "=5")
      msgbox act3
</script>

or

<script language=vbscript>
      Option explicit
      execute ("dim act"&cstr(3))
      execute("act" & 3 & "=5")
      msgbox act3
</script>

with no variables at all created : note the need to explicitly convert to string in the dim statement in the second sample.

 

by: david_levinePosted on 2000-06-17 at 10:08:32ID: 2973992

First, thanks for all the responses.

I can/will use an array, but I need to eventually assign them to variables as I explained in the initial question.

I'm trying the execute() function, but I get a syntax error. Here's the code snippet...

<%
dim x
for x = 1 to 12
            execute("dim PassExp"&cstr(x))
            execute("PassExp"&cstr(x)&" = '1/1/01'")
next

for x = 1 to 12
            response.write "PassExp"&cstr(x)&"="&execute("PassExp"&cstr(x))&"<br>"
next
%>


The syntax error is on the 2nd execute stmt where I try to assign a date. Any ideas?

Also, any way to code multiple statements per line using VBScript & ASP?

Thanks.

 

by: MarinePosted on 2000-06-17 at 10:12:22ID: 2974004

i also tried using execute and got same error. It seems to me that's that invalid statement. Mr devid_levin you will get better performance using arrays then using variables that have the same use for them just a point. From my experience this can't be done as you try to do it.

 

by: david_levinePosted on 2000-06-17 at 10:23:55ID: 2974111

I think I narrowed it down. I shouldn't use cstr() in the code I posted.

That last error I need to get around has to do with the response.write statement I posted. I need it to put out the value for the variable. In your code, you hard-coded the variable. Is there a way to refer to the variable all on it's own?

Here's the statement that gets an error now:
response.write "PassExp"&x&"="&execute("PassExp"&x)&"<br>"

when executed in the loop produces the following error:
Microsoft VBScript runtime error '800a000d'

Type mismatch: 'PassExp1'

I guess it doesn't know what to do with just the expression PassExp1.

Any ideas?

 

by: david_levinePosted on 2000-06-17 at 10:31:57ID: 2974130

Ok, got ! :)

If I break out the response.write and make the variable piece, on it's own line as:

execute("Response.write PassExp"&x)

it works!

Ok, do you know if you can put multiple commands per line?

Respond and I'll give you the points.

 

by: mattykPosted on 2000-06-17 at 10:35:48ID: 2974141

Here is the working code not the need to use the eval function when you want to actually write out the var values:

<%
dim x
for x = 1 to 12
execute("dim PassExp" & x)
execute("PassExp" & x & "="&chr(34)&"1/1/01"&chr(34))
next

for x = 1 to 12
response.write "PassExp"&x&"="&eval("PassExp"&x)&"<br>"
next
%>

or to run it  multiple lines

<%
dim x
for x = 1 to 12
 
execute("dim PassExp" & x&":PassExp" & x & "="&chr(34)&"1/1/01"&chr(34))
next

for x = 1 to 12
response.write "PassExp"&x&"="&eval("PassExp"&x)&"<br>"
next
%>

 

by: mgfranzPosted on 2000-06-17 at 10:38:44ID: 2974147

Multiple commands per line???

 

by: david_levinePosted on 2000-06-17 at 10:41:52ID: 2974151

Thanks mattyk! Exactly what I needed!!

The colon is the way to code multiple stmts per line. Thanks for that!

And thanks for the answer!

 

by: david_levinePosted on 2000-06-17 at 10:42:22ID: 2974152

Comment accepted as answer

 

by: MarinePosted on 2000-06-17 at 10:48:17ID: 2974176

still i don't understand the use of this method.

 

by: david_levinePosted on 2000-06-17 at 11:04:57ID: 2974253

Marine,

I'll use an array internally on the page, but if I want to for example, post a form with variables, I need to set/get them into an array.

Here's an example:

You can have up to 12 different values for something. I create 12 different form fields, each with a unique name like Exp1, Exp2, ... Exp12

Those values are posted from the form and I get read them using Request.Form("Exp1")

Well, like I mentioned in the original question, I can do something like:
Exp(1) = Request.Form("Exp1")
Exp(2) = Request.Form("Exp2") etc

Or now I can do the following in a loop instead of individually coding each:

Exp(x) = evaluate("Request.Form('Exp"&x&"')")

Make sense?

 

by: mgfranzPosted on 2000-06-17 at 11:05:29ID: 2974256

Marine, kinda slick... I had to look it up too;

The Execute method allows you to call another ASP page from inside an ASP page. When the called ASP page completes its tasks, you are then returned to the calling ASP page. The overall effect is very similar to a function or subroutine call. Any text or output from the called ASP page will be displayed on the calling ASP page. The Execute method is a more useful alternative to using server-side includes.

In contrast, the Transfer method allows you to transfer from one ASP page to another without returning to the calling ASP page.

There is one mandatory argument.

Path

The Path argument is a string specifying either the absolute or relative path of the ASP page being called. The file name must be included in the path. The entire Path must be enclosed inside a pair of quotes. The Path argument can also include a query string.

Code:
----------CallingAsp.asp----------
<HTML>
<BODY>

How now <%Server.Execute("CalledAsp.asp")%> cow?

</BODY>
</HTML>

----------CalledAsp.asp----------
<%
Response.Write "pink"
%>

Output:
How now pink cow?

 

by: david_levinePosted on 2000-06-17 at 14:50:58ID: 2975364

So, we are all Experts here, so if someone still has questions, feel free to ask!

I converted the long ASP page to a much shorter page using the execute and eval functions.

David

 

by: mgfranzPosted on 2000-06-17 at 16:54:22ID: 2975927

I have been working on using the Application object to wrap full includes up as an app instead of an include, this way the page would get loaded once, instead of taking three or four trips through for each load.

I will have to investigate this Execute function more... ;-)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...