如何迭代返回field2
类型字段的记录集?
有没有办法告诉field2
类型中有多少个对象?
让我描述一下表格的相关方面:
表fields
包含field NMR
,其中包含用户可在另一个表中选择的可能选项列表。在Experiments
表中,字段NMR是一个组合框,用于填充另一个表中的选项。
我这样做是在Experiments
表格设计中,我用这种方式设置了字段:
现在在我的一个表单中,我需要读取Experiments!NMR
中的值,该值可以是多个选择允许的组合框。记录集rs!NMR的类型为Field2
。
要获取值,请使用整数进行迭代(即rs!NMR(0)
将返回第一个选定的选项)。问题是我不知道如何获得字段计数并调用!NMR(i)
,其中i大于元素数将调用Run time error '3265', Object doesn't exist in this collection
。
它们存在size
方法只返回字段宽度大小(4?),文档说明这是field2
对象中数据类型的大小。
使用count
调用field2
似乎没有与!NMR.Count
相关联的runtime error 438, Object doesn't support this method
方法。
代码:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qry As String
qry = "SELECT * FROM Experiments"
Set db = CurrentDb
Set rs = db.OpenRecordset(qry, dbOpenSnapshot)
With rs
Do While Not .EOF
Dim i As Integer
For i = 0 to !NMR.Count ' or SOMETHING - this is the problem
' this is irrelevant, I need to know how to iterate the list
Next i
.MoveNext
Loop
End With
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
我也尝试过逻辑控制,例如
Do While(!NMR(i) <> vbNullString)
因为各个组件都是字符串,但没有运气。这会发出相同的3265: Item isn't found
错误。对于具有此检查Do While Not IsNull(!NMR(i))
有没有办法告诉Field中有多少个对象?核磁共振?
答案 0 :(得分:2)
您需要将复杂Field2分配给Recordset2
对象并循环播放。
Sub Whatever()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsComplex As DAO.Recordset2
Dim qry As String
qry = "SELECT * FROM Experiments"
Set db = CurrentDb
Set rs = db.OpenRecordset(qry, dbOpenSnapshot)
Do While Not rs.EOF
Set rsComplex = rs("NMR").Value
rsComplex.MoveLast
rsComplex.MoveFirst
Dim i As Integer
For i = 0 To rsComplex.RecordCount - 1 ' Asker modified
Debug.Print rsComplex(0)
rsComplex.MoveNext
Next i
rsComplex.Close
Set rsComplex = Nothing
rs.MoveNext
Loop
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub