VBA:由MoveNext覆盖的RecordSet值

时间:2013-01-22 14:09:44

标签: sql vba iteration recordset

我在使用VBA和DAO.Recordset工作时遇到了一个奇怪的问题。 我现在正在循环记录集并保存数组中earch迭代的相应字段。 但是每次由于while循环而调用“.MoveNext”时,存储在“rs.Fields.Field(i).Value”中的信息都被覆盖,因此这些字段无法使用。我在调试过程中发现了这一点。

一些相关代码:

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

希望有人知道是什么原因造成的。提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试在每个记录中为字段/列计数运行另一个循环:

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