1
votes

I am attempting to fill a 2D array with items in a recordset, but I keep getting the following error: Microsoft VBScript runtime error '800a0009' Subscript out of range: 'UBound'

I've also tried changing the UBound to Ubound(ProxState,1), but that just gives me the following error on the line following the start of the 'For' loop: Microsoft VBScript runtime error '800a0009' Subscript out of range

Dim ProxCount, LastProx
ProxCount = 0
Do While Not objRS.EOF
  ProxCount = ProxCount + 1
objRS.MoveNext
Loop

LastProx = ProxCount - 1
objRS.MoveFirst
Dim ProxState

ProxState = Array(LastProx,1)
For i = 0 To UBound(ProxState,0)
  ProxState(i,0) = objRS("ProximityName")
  ProxState(i,1) = objRS("InState")
Next
3
ProxState = Array(LastProx,1) does not create a two-dimensional (LastProx x 1) array, but an one-dimensional array containing LastProx and 1.Ekkehard.Horner

3 Answers

2
votes

You can use GetRows() to assign it all in one go instead of looping.

ProxState = objRS.GetRows()

See: http://www.asp.happycodings.com/DataAccess/code1.html

0
votes

One more way:

Dim ProxCount
Dim ProxState()
ProxCount = 0

If (not objRS.Eof) Then
    ProxCount = objRS.RecordCount

    ReDim ProxState(ProxCount - 1, 1)

    For i = 0 To ProxCount - 1
      ProxState(i, 0) = objRS("ProximityName")
      ProxState(i, 1) = objRS("InState")
    Next

End If
0
votes

Oops I guess I was wrong about that. Apology! THE FOLLOWING IS INCORRECT. Making sure you know that Ubound gives you number of elements so... if you change your code as follows...

For i = 0 To UBound(ProxState,0)-1

then it should work