This is a doozy for me haha, I\'ve pretty much checked nearly every page on Google Search and I still don\'t quiet understand how to do it.
I want to create a multi
This may be off-topic, but after seeing your exact code, why aren't you using the built-in ADO function: GetRows() ?
sub grabdata
SQL_query = "SELECT * FROM MSAccess_table"
Set rsData = conn.Execute(SQL_query)
If Not rsData.EOF Then aData = rsData.GetRows()
end sub
This returns all your column # as the first index, and the rows (data) in the second.
So to loop through it, you would:
If IsArray(aData) Then
For x = lBound(aData,2) to uBound(aData,2) 'loops through the rows
Col1 = aData(0,x)
Col2 = aData(1,x)
Col3 = aData(2,x)
Response.Write "Row #" & x+1 & "<br>"
Response.Write "This is the data in Column1: " & Col1 & "<br>"
Response.Write "This is the data in Column2: " & Col2 & "<br>"
Response.Write "This is the data in Column3: " & Col3 & "<br>"
Next
End If
*NOTE: Rows (and columns) start on 0 in the array by default.
set rs = conn.execute(strQry)
arrRAY = rs.GetRows()
if isarray(arrRAY) then
do stuff
end if
(1) The best way to get an ADO resultset into a two-dimensional array is to use the .GetRows method. Then your problem just vanishes.
(2) There are two kind of arrays in VBScript. Fixed arrays are declared by specifying their UBounds:
Dim aFix(2, 3)
They can't be resized. Dynamic arrays can be changed by ReDim [Preserve]
. The best way to create such an array is
ReDim aDyn(2, 3)
if you know the starting size, or
Dim aDyn : aDyn = Array()
if you want to start with an empty one. The catch 22 is: you can use Preserve only for the last dimension.
(3) Your
Dim data2()
is an abomination - a fixed array of no size. It's a pity that the 'compiler' is too stupid to catch such a beast that VBScript can't handle properly:
>> Dim data2()
>> WScript.Echo UBound(data2)
>>
Error Number: 9
Error Description: Subscript out of range
The nastiness of the Dim a()
statement is hidden by the fact that a later ReDim
will store a proper dynamic array into that variable:
>> Dim data2() ' <-- abomination
>> ReDim data2(1,1) ' <-- overwritten by a dynamic array
>> data2(0,0) = 0
>> ReDim Preserve data2(1,5) ' last dimension increased; 'old' data preserved
>> data2(1,5) = 1
>> WScript.Echo data2(0,0), data2(1,5)
>>
0 1
Update wrt jmbpiano's comment:
(1) I gave evidence that you can't get the UBound for a variable dimmed with (), so I stick to my claim that such beasts are abominations. Just look at the question (or this one) to see that using the () will give you trouble.
(2) I said that you should use ReDim a(KnownUbound)
to 'declare' a dynamic array with known size, but I didn't give evidence for the 'Option Explicit'-compatibility of this idiom. So :
Option Explicit
ReDim a(4711)
ReDim b(4,7,1,1)
a(0) = "qed"
b(0,0,0,0) = "qed"
WScript.Echo b(0,0,0,0)
output:
cscript 19888987.vbs
qed