在回答的过程中
this question
,我编写了一个简单的函数来测试ms access表是否包含所提供数组中的所有字段:
Function ValidateFields(strTbl As String, arrReq As Variant) As Boolean
Dim fld
Dim fldTmp As Field
On Error GoTo err
For Each fld In arrReq
Set fldTmp = CurrentDb.TableDefs(strTbl).Fields(fld)
Next fld
ValidateFields = True
err:
Exit Function
End Function
?ValidateFields("TempTable", Array("Field1", "Field2", "Field3"))
False
不过,为了提高效率,我尝试将fields集合分配给
For Each
循环:
Function ValidateFields(strTbl As String, arrReq As Variant) As Boolean
Dim fld
Dim fldTmp As Field
Dim colFld As Fields
Set colFld = CurrentDb.TableDefs(strTbl).Fields
On Error GoTo err
For Each fld In arrReq
Set fldTmp = colFld(fld)
Next fld
ValidateFields = True
err:
Exit Function
End Function
现在,如果我把
On Error
语句,我收到以下行错误
Set fldTmp = colFld(fld)
突出显示原因:
运行时错误“3420”:
对象无效或不再设置。
为什么变量
colFld
失去它的价值
为每个人
循环?