返回或設(shè)置 ADO Recordset 或 DAO Recordset 對象,代表指定窗體、報(bào)表、列表框控件或組合框控件的記錄源。可讀寫。
expression.Recordset
expression 必需。返回“應(yīng)用于”列表中的一個(gè)對象的表達(dá)式。
在 DAO 內(nèi),不能將該屬性用于 ODBCDirect 記錄集類型。
Recordset 屬性返回的記錄集對象提供窗體、報(bào)表、列表框控件或組合框控件中正被瀏覽的數(shù)據(jù)。例如,如果窗體是基于查詢產(chǎn)生的,引用 Recordset 屬性就相當(dāng)于用同樣的查詢復(fù)制 Recordset 對象。但是,與使用 RecordsetClone 屬性不同的是,如果更改由窗體的 Recordset 屬性返回的記錄集內(nèi)哪條記錄為當(dāng)前的這一設(shè)定,就會(huì)設(shè)置窗體的當(dāng)前記錄。
該屬性僅在使用 Visual Basic 時(shí)才可用。
Recordset 屬性的讀/寫行為取決于該屬性所標(biāo)識(shí)的記錄集內(nèi)所包含的記錄集類型(ADO 或 DAO)和數(shù)據(jù)類型(Jet 或 SQL)。
記錄集類型 |
基于 SQL 數(shù)據(jù) |
基于 Jet 數(shù)據(jù) |
ADO |
讀/寫 |
讀/寫 |
DAO |
N/A |
讀/寫 |
下面的示例將打開一個(gè)窗體,打開一個(gè)記錄集,然后通過將窗體的 Recordset 屬性設(shè)為新建 Recordset 對象,從而將窗體與記錄集綁定。
Global rstSuppliers As ADODB.Recordset
Sub MakeRW()
DoCmd.OpenForm "Suppliers"
Set rstSuppliers = New ADODB.Recordset
rstSuppliers.CursorLocation = adUseClient
rstSuppliers.Open "Select * From Suppliers", _
CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Set Forms("Suppliers").Recordset = rstSuppliers
End Sub
可以使用 Recordset 屬性進(jìn)行如下操作:
? | 將多個(gè)窗體綁定到公用數(shù)據(jù)集。這樣做允許多個(gè)窗體的同步。例如: |
Set Me.Recordset = Forms!Form1.Recordset
? | 使用窗體不直接支持的 Recordset 對象的方法。例如,您可以在用于查找記錄的自定義對話框中使用帶有 ADO Find 或 DAO Find 方法的 Recordset 屬性。 |
? | 圍繞一系列影響多個(gè)窗體的編輯將事務(wù)打包(可以回滾)。 |
更改窗體的 Recordset 屬性可能也將更改 RecordSource、RecordsetType 和 RecordLocks 屬性。同時(shí),一些與數(shù)據(jù)相關(guān)的屬性可能被覆蓋;例如:Filter、FilterOn、OrderBy 和 OrderByOn 屬性。
調(diào)用某窗體的記錄集的 Requery 方法(例如,Forms(0).Recordset.Requery)可能會(huì)導(dǎo)致該窗體變?yōu)槲唇壎?。若要刷新與記錄集綁定的窗體中的數(shù)據(jù),請將該窗體的 RecordSource 屬性設(shè)為它自身 (Forms(0).RecordSource = Forms(0).RecordSource)。
當(dāng)窗體與記錄集綁定時(shí),如果使用“按窗體篩選”命令就會(huì)出錯(cuò)。
下面的示例使用 Recordset 屬性從當(dāng)前窗體創(chuàng)建一個(gè)新的 Recordset 對象的副本,然后在“調(diào)試”窗口內(nèi)打印字段的名字。
Sub Print_Field_Names()
Dim rst As DAO.Recordset, intI As Integer
Dim fld As Field
Set rst = Me.Recordset
For Each fld in rst.Fields
' Print field names.
Debug.Print fld.Name
Next
End Sub
接下來的示例使用 Recordset 屬性和 Recordset 對象將一個(gè)記錄集與窗體的當(dāng)前記錄同步。當(dāng)從組合框選擇一個(gè)公司名稱時(shí),將使用 FindFirst 方法來為該公司定位記錄,并使窗體顯示找到的記錄。
Sub SupplierID_AfterUpdate()
Dim rst As DAO.Recordset
Dim strSearchName As String
Set rst = Me.Recordset
strSearchName = CStr(Me!SupplierID)
rst.FindFirst "SupplierID = " & strSearchName
If rst.NoMatch Then
MsgBox "Record not found"
End If
rst.Close
End Sub
下面的代碼幫助確定在不同情況下,Recordset 屬性返回什么類型的記錄集。
Sub CheckRSType()
Dim rs as Object
Set rs=Forms(0).Recordset
If TypeOf rs Is DAO.Recordset Then
MsgBox "DAO Recordset"
ElseIf TypeOf rs is ADODB.Recordset Then
MsgBox "ADO Recordset"
End If
End Sub