简体   繁体   中英

VBA: RecordSet values overwritten by MoveNext

I've encountered a strange problem during some work with VBA and a DAO.Recordset. I'm currently looping throgh the recordset and save the corresponding fields for earch iteration in an array. But every time I call ".MoveNext" due to the while-loop, the information stored in the "rs.Fields.Field(i).Value" is being overwritten, thus the fields are unusable. I've figured this out during debugging.

Some relevant code:

If Not rs Is Nothing Then
      If rs.RecordCount > 0 Then
        With rs
            While Not .EOF
                ReDim Preserve fieldSet(0 To i + 1) As DAO.Fields
                Set fieldSet(i) = rs.Fields
                i = i + 1 ' the values are still intact at this point
                .MoveNext ' here's where there's only "No current record" stored inside the value-field
            Wend
        End With
      End If

Hopefully somebody has an idea what causes this. Thanks in advance.

Try running another loop within for the field/column count in each record:

Do While Not rs.EOF
   For j = 0 to rs.Fields.Count
     Set fieldSet(j) = rs.Fields.Field(j).Value 
     '-- do stuff
   Next j
   rs.MoveNext
Loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM