2017-06-20 46 views
0

如何迭代返回类型为field2的字段的记录集? 是否有方法可以确定field2类型中有多少个对象?针对Combobox字段返回的VBA访问对象类型

让我描述我的表的相关方面:

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),其中我大于元素的数量将调用Run time error '3265', Object doesn't exist in this collection

它们存在size方法只返回字段宽度大小(4?),并且文档状态表明这是field2对象内数据类型的大小。

似乎没有与field2关联的count方法,因为使用!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中有多少个物体!NMR?

+0

http://sourcedaddy.com/ms-access/navigating-recordsets-with-multi-value-lookup-fields.html这将告诉你如何做到这一点。实际上很容易找到。 –

+0

过度反应?你的高清读数太多了lol –

+0

@DougCoats Myb然后。我搜索了一段时间 - 我以为你说我没有试图自己找到它。 – Chemistpp

回答

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 
+0

@Chemistpp谢谢,我忽略了这一点。我总是从1循环记录集以避免.RecordCount -1。 –

+0

我也忽略了它。 MS VBA调试器不适合我们。 – Chemistpp