Unable to access records in DAO Recordset

445 Views Asked by At

I am creating a DB in Access and using VBA to implement a feature to run several INSERT queries into multiple tables, based on values found in other 'template' tables.

At a certain point in the process, I am retrieving the results of a SELECT query and using the results of the query as parameters in an INSERT.

I have built and tested the query using the Access query builder so I know that the query functions as expected.

I am using the DAO library to interface with the DB and run my queries.

I have the function below which converts a recordset returned from the latter function to a collection of collections. In the function below have run into a problem where the recordset I return apparently contains zero records. This causes it to throw a 'No Current Record' exception on the line 'records.MoveLast'. What I should be seeing, which I know from the query, is a Recordset containing 2 records, with 5 fields each.

Private Function RecordsetToCollection(records As RecordSet) As Collection

Dim recordCollection As New Collection
Dim i As Integer

'Go to first record?
'Exception thrown here
records.MoveLast
records.MoveFirst

'Check if current record position before first record
If Not records.BOF Then

    'While not after last record
    While Not records.EOF

        'Collection to hold field values
        Dim fieldCollection As New Collection

        'Loop through fields
        For i = 0 To records.Fields.Count - 1

            'Add to collection
            fieldCollection.Add records.Fields(i).Value

        Next i

        'Add field collection to record collection
        recordCollection.Add fieldCollection

        Set fieldCollection = Nothing

        'Go to next record
        records.MoveNext

    Wend

End If

'Return collection
Set RecordsetToCollection = recordCollection

End Function

The recordset being fed into this function is retrieved using the following function:

Private Function GetTemplateDeliverables(TemplateProjectActivityID As Integer) As Collection
'Get Template Deliverables recordset from tbl_TemplateDeliverables using given ProjectActivityID

'Open query
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("qry_GetTemplateDeliverables")

'Add parameters
qdf.Parameters("Project Activity ID") = ProjectActivityID

'Get return recordset
Dim rst As RecordSet
Set rst = qdf.OpenRecordset()

Dim recordCollection As New Collection
Set recordCollection = RecordsetToCollection(rst)

'Get ProjectActivityID from recordset
Set GetTemplateDeliverables = recordCollection

'Clean up
qdf.Close
Set qdf = Nothing
Set rst = Nothing

End Function

Does anyone have any suggestions as to why this may be the case?

I can't see why this isn't working given that I already have functions to retrieve recordsets that are working fine, the only difference being that in those functions each record has only 1 field, whereas this has 5 fields, but I can't think why this would be a problem.

Any help would be much appreciated!

(P.S. any tips on how to improve my code would also be of help.)

0

There are 0 best solutions below