为Combobox字段返回的VBA访问对象类型

时间:2017-06-20 21:50:24

标签: vba ms-access access-vba

如何迭代返回field2类型字段的记录集? 有没有办法告诉field2类型中有多少个对象?

让我描述一下表格的相关方面:

enter image description here

fields包含field NMR,其中包含用户可在另一个表中选择的可能选项列表。在Experiments表中,字段NMR是一个组合框,用于填充另一个表中的选项。

enter image description here

我这样做是在Experiments表格设计中,我用这种方式设置了字段:

enter image description here

现在在我的一个表单中,我需要读取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中有多少个对象?核磁共振?

1 个答案:

答案 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