我正在尝试创建一个带有ADODB Recordset并将其数据复制到新记录集的函数。
为此,我使用 do 循环遍历源记录集的每一行。在循环中,我需要使用为每个循环移动每一行的Fields集合,以捕获其数据。
然而,VB6似乎在每个循环的上窒息 - 它突出显示for循环的名称 s iterator(下例中的“fld”)并抛出未定义的变量错误。
奇怪的是,当没有放在 do 循环中时,每个循环完全相同的工作正常。
测试用例1 (无法定义“For Each fld”):
'targetTableName As String:要创建的新表的名称 'sourceRecordSet As ADODB.Recordset:包含要发布到新targetTableName的结果的open recordset
Public Sub createTableFromRecordset(targetTableName As String, sourceRecordSet As ADODB.recordSet)
'(irrelevant code omitted)
'create MDB RS object
Dim targetRecordSet As ADODB.recordSet
Set targetRecordSet = mdbQuery("select * from targetTableName;")
'write data to recordset
sourceRecordSet.MoveFirst ' to be safe
targetRecordSet.MoveFirst ' to be safe
While Not sourceRecordSet.EOF
targetRecordSet.AddNew
For Each fld In sourceRecordSet.Fields 'fails here, hilighting fld
'do work
Next fld
sourceRecordSet.MoveNext
Loop
'(irrelevant code omitted)
End Sub
测试用例2 (能够定义“For Each fld”就好了):
Private Sub testCase2()
'Create a source dataset
Dim sourceRs As ADODB.Recordset
Set sourceRs = functionThatGetsRecordset("(a query)")
'Create target db conn
Dim mdbConn As ADODB.Connection
Set mdbConn = functionThatGetsConn()
'iterate through source's fields
For Each fld In sourceRs.Fields 'works fine
'do work
Next fld
End Sub
答案 0 :(得分:5)
尝试两件事:
Dim fldField As Field