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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Variable Names??

Status
Not open for further replies.

combs

Programmer
Apr 18, 2002
78
US
I'm not sure if this is possible..... but here's what I'm trying to do...

I have declared several variables (see code below) and then am trying to assign values to them in a loop. The values are being pulled from the database successfully, but then the code is not working when I try to assign them the values. What am I doing wrong??

Code:
Response.Buffer = True
  Dim conn
  Dim rs
  Dim strSql
  Dim dbPath
  Dim intTemp
  Dim strNT1, strNT2, strNT3
  Dim strNL1, strNL2, strNL3
  Dim strCAT1, strCAT2, strCAT3, strCAT4
  Dim strCAL1, strCAL2, strCAL3, strCAL4
  intTemp = 1
  dbPath = "D:\hosting\********\access_db\***.mdb"
  '============================
  'Open Database Connection
  '============================
	Set conn = Server.CreateObject("ADODB.Connection")
	conn.Open "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & dbPath
	Set rs = Server.CreateObject("ADODB.Recordset")
	
	strSql = "SELECT * FROM tblLinks WHERE Type = 'News';"
	rs.Open strSql, conn, 1
	If Not rs.EOF Then
		rs.MoveFirst
		Do While Not rs.EOF
			[B]strNT & intTemp = rs("Text")
			strNL & intTemp = rs("Link")[/B]
			intTemp = intTemp + 1
			rs.MoveNext
		Loop
	End If
	rs.Close
        Set rs = Nothing
        conn.Close
        Set conn = Nothing

I'd appreciate any suggestions....
Thanks!
 
Ahhhh.... It came to me - finally!
Use Arrays!!
See code below that works:
Code:
Response.Buffer = True
  Dim conn
  Dim rs
  Dim strSql
  Dim dbPath
  Dim intTemp
  Dim strNL(3)
  Dim strNT(3)
  Dim strCAT(4)
  Dim strCAL(4)
  intTemp = 1
  dbPath = "D:\hosting\******\access_db\****.mdb"
  '============================
  'Open Database Connection
  '============================
	Set conn = Server.CreateObject("ADODB.Connection")
	conn.Open "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & dbPath
	Set rs = Server.CreateObject("ADODB.Recordset")
	
	strSql = "SELECT * FROM tblLinks WHERE Type = 'News';"
	rs.Open strSql, conn, 1
	If Not rs.EOF Then
		rs.MoveFirst
		Do While Not rs.EOF
			strNT(intTemp) = rs("Text")
			strNL(intTemp) = rs("Link")
			intTemp = intTemp + 1
			rs.MoveNext
		Loop
	End If
	rs.Close
     .....
Thanks anyway!
 
You can create dynamic variables using eval/execute but you're in dodgy territory there.
 
Any even better way of dumping a recordset to an array is to use the GetRows() method of the recordset object. It is very fast and benchmarks have shown that using GetRows() and then looping through an array is generally 7-11 times faster than looping through the recordset itself.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top