Is it better to use a ReDim array several times or reopen a recordset?

I write code in VBscript that opens a record set object and then loads an array with objects containing data from each record. My record set type does not support the property rs.RecordCount, so I either need an ReDim Preservearray when it scrolls through the record set, or I need to open the record set again after the counting cycle, because using the rs.MoveFirstafter counting cycle does not seem to work ... What will be faster? There are only 7 records in the recordset object, so I would most need ReDim.

This is one of the ways I tried, but rs.MoveFirst does not work correctly, see comments:

Function LoadData(filter_val)
   Dim arr
   Dim rs
   'Calls function that opens the rs and returns it
   Set rs = GetRS(filter_val)

   Dim counter
   counter = 0
   Do Until rs.EOF
      counter = counter + 1
      rs.MoveNext
   Loop

   ReDim arr(counter)

   rs.MoveFirst

   For i = 0 To counter
      Set arr(i) = New obj
      'attempt to load values into the object from the recordset, but get an
      'error saying 'either BOF or EOF is true, or the current record has been deleted'
      'I tried adding If statements with MsgBox print outs checking for rs.EOF or rs.BOF
      'being true right after rs.MoveFirst, but neither evaluates to true...
   Next
End Function

This method works, but I need to constantly reDim the array:

Function LoadData(filter_val)
   Dim arr
   Dim rs
   Set rs = GetRS(filter_val)

   Dim counter
   counter = 0
   ReDim arr(counter)

   Do Until rs.EOF
      Set arr(counter) = New obj

      'load data from rs into object

      rs.MoveNext

      If Not rs.EOF
         counter = counter + 1
         ReDim Preserve arr(counter)
      End If

   Loop
End Function
+4
2

, ReDim. - :

ReDim arr(-1)

, -:

Do Until rs.EOF
  ReDim Preserve arr(UBound(arr)+1)
  Set arr(UBound(arr)) = New obj

  'load data from rs into object

  rs.MoveNext
Loop

.

+3

, MSDN

GetRows (ADO)

Recordset .


array = recordset.GetRows(Rows, Start, Fields )


, .


. GetRowsOptionEnum, . adGetRowsRest.   Start. , , GetRows. BookmarkEnum.   . , , . ADO .


7, max 7.

-1

Source: https://habr.com/ru/post/1619998/


All Articles