2017-06-20 82 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