VBScript function to create a two-dimensional array, like GetRows does
This is asp classic using VBScript, and no, it ain't moving to .net anything, so don't even ask.
OK, so the classic way to get data out of a d开发者_StackOverflowatabase is to use GetRows:
Dim MyRecords
Dim rs, conn
[...database opening stuff...]
If Not rs.EOF Then
MyRecords = rs.GetRows
End If
[...close database & set to Nothing...]
Note how MyRecords is not dimmed as an array; it only becomes one after the GetRows
call.
My question is, how do I do something similar without using GetRows
? For example, if the data needs to come from Request.Form
instead of the database? (Doing something like "If conditions are met, then get data from database, else get data from form, but display the data the same way regardless where it came from".)
There's an Array
function in vbScript, but it only creates one-dimensional arrays - it's kinda like a limited version of the Split
function, as far as I can tell. I need two dimensions. (Backwards two dimensions, no less, to match the way GetRows
works - i.e. the first dimension is the columns, the second dimension is the rows.)
I can't use dynamic arrays (Dim MyRecords()
, then later ReDim MyRecords(x,y)
) because then the GetRows
will throw an error.
Is there a way to do what I want, or do I have to resign myself to juggling two different arrays, one for the database, the other for the form? Or worse, use a Do While
loop to populate the array from the database... //shudder.
Just use:-
Dim MyRecords
Then later use
ReDim MyRecords(x, y)
You don't need to declare the MyRecords as an array in order to use ReDim.
Perhaps you can add the form data to a disconnected recordset
and then proceed as for a standard recordset?
I've accepted Anthony's answer because it is clearly the simplest and best way to proceed, but here's what I had come up with before reading his answer: use a function that returns an array. That is, write a "GetFormData" function that dims its own array variable, populate it from the form (or wherever), and then assign that array as the value of the function.
Dim MyRecords
If [something] Then
MyRecords = GetFormData([...])
Else
[...]
MyRecords = rs.GetRows
[...]
End If
Function GetFormData([...])
Dim FormArray()
[...]
FormArray(i,j) = Request.Form(...)
[...]
GetFormData = FormArray
End Function
精彩评论