Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaring variables dynamically

Status
Not open for further replies.

Sarky78

Programmer
Oct 19, 2000
878
GB
Hi there i have a problem that i need to be able to define a number of variables based on a database information, so obviously i can't just define these in the code, i need some way of looping over the database results and to create the variable and recordset at runtime. I want to be able to loop over objLocations and create a variable and recordset for all of the records returned by objLocations

strLocations ="Select * from tblLocations"
objLocations.Open strLocations

while not objLocations.EOF
define new_vbl 'dynamically created
define recordset_vbl 'dynamically created
Set recordset_vbl=Server.CreateObject("ADODB.Recordset")
Set recordset_vbl.ActiveConnection = objConn

new_vbl = "SELECT * from table1, table2 WHERE LocationID=" & objLocations("LocationID")
recordset_vbl.Open new_vbl
wend

does anyone have any ideas of how i can achieve this?
 
VBScript 5.5 (and 5.0?) opens up a new method called execute

ex:
for i = 0 to 5
execute "Dim newVar" & a
next

will create you 6 new variables,
newvar0,
newVar1,
newVar2,

etc..

hope this helps
leo
 
thanks for that i will give that a try today, quick question though. how do i find out what version of vbscript is running at present, as this is going to a client site, so we will have to prepare things at their end.
 
try using arrays,
it worked for me in an ASP page.
<%
for i = 1 to nivel
Dim versub(1000)
versub(nivel) = &quot;versub&quot;&nivel

Dim connver(1000)
connver(nivel) = &quot;connver&quot;&nivel

Set versub(nivel) = Server.CreateObject(&quot;ADODB.RecordSet&quot;)
connver(nivel) = &quot;SELECT * FROM table&quot;
Set versub(nivel) = connection.execute(connver(nivel))
loop
%>
 
put this at the top of any old file:

<%=ScriptEngine%><BR>
<%=ScriptEngineMajorVersion%><BR>
<%=ScriptEngineMinorVersion%><BR>

ScriptEngine is the default scripting engine

I have VBScript 5.5

the output of this would be
VBScript
5
5

hope this helps
leo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top