Populating Variant Array With GetRows (1 Viewer)

cameron.scrimgeour

Registered User.
Local time
Today, 12:40
Joined
May 7, 2009
Messages
21
Having some problems trying to populate my Array. Heres the code...


Sub LoadArray()

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim avarRecords As Variant


Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT ID FROM TABLE1 WHERE ID < '10'")


avarRecords = rst.GetRows(3)

End Sub




The avarRecords array doesnt seem to take in the data from the GetRows command.

I have done a CountRows on the recordset and know that there is records there to be read.
 

DCrake

Remembered
Local time
Today, 20:40
Joined
Jun 8, 2005
Messages
8,632
Microsoft Say...

Use the GetRows method to copy records from a Recordset. GetRows returns a two-dimensional array. The first subscript identifies the field and the second identifies the row number. For example, intField represents the field, and intRecord identifies the row number:

avarRecords(intField, intRecord)
To get the first field value in the second row returned, use code like the following:

field1 = avarRecords(0,1)
To get the second field value in the first row, use code like the following:

field2 = avarRecords(1,0)
The avarRecords variable automatically becomes a two-dimensional array when GetRows returns data.

If you request more rows than are available, then GetRows returns only the number of available rows. You can use the Visual Basic for Applications UBound function to determine how many rows GetRows actually retrieved, because the array is sized to fit the number of returned rows. For example, if you returned the results into a Variant called varA, you could use the following code to determine how many rows were actually returned:

numReturned = UBound(varA,2) + 1
You need to use "+ 1" because the first row returned is in the 0 element of the array. The number of rows that you can retrieve is constrained by the amount of available memory. You shouldn't use GetRows to retrieve an entire table into an array if it is large.

Because GetRows returns all fields of the Recordset into the array, including Memo and Long Binary fields, you might want to use a query that restricts the fields returned.

After you call GetRows, the current record is positioned at the next unread row. That is, GetRows has the same effect on the current record as Move numrows.

If you are trying to retrieve all the rows by using multiple GetRows calls, use the EOF property to be sure that you're at the end of the Recordset. GetRows returns less than the number requested if it's at the end of the Recordset, or if it can't retrieve a row in the range requested. For example, if you're trying to retrieve 10 records, but you can't retrieve the fifth record, GetRows returns four records and makes the fifth record the current record. This will not generate a run-time error. This might occur if another user deletes a record in a dynaset-type Recordset. See the example for a demonstration of how to handle this.
 

cameron.scrimgeour

Registered User.
Local time
Today, 12:40
Joined
May 7, 2009
Messages
21
I am only trying to return one field through my query, ID.

Does this mean I cannot use Variant Arrays?

If not, what can I use? And how can I populate it
 

Users who are viewing this thread

Top Bottom