ComboBox.Recordset 屬性 (Access)ComboBox.Recordset property (Access)

會傳回或設定 ADO Recordset或 DAO Recordset物件,代表指定之物件的記錄來源。Returns or sets the ADO Recordset or DAO Recordset object that represents the record source for the specified object. 讀/寫 物件Read/write Object.

語法Syntax

運算式資料錄集expression.Recordset

_運算式_代表**ComboBox** 物件的變數。expression A variable that represents a ComboBox object.

註解Remarks

Recordset屬性會傳回Recordset物件會提供要瀏覽表單、 報表、 清單方塊控制項或下拉式方塊控制項中的資料。The Recordset property returns the Recordset object that provides the data being browsed in a form, report, list box control, or combo box control. 如果表單為基礎的查詢,例如,參照 Recordset 屬性相當於使用相同的查詢來複製 資料If a form is based on a query, for example, referring to the Recordset property is the equivalent of cloning a Recordset object by using the same query. 不過,不同於使用 RecordsetClone 屬性,變更為目前表單的 Recordset 屬性所傳回的 recordset 中記錄也會將目前表單的記錄。However, unlike using the RecordsetClone property, changing which record is current in the recordset returned by the form's Recordset property also sets the current record of the form.

Recordset 屬性的讀取/寫入行為取決於 recordset (ADO 或 DAO) 的類型和 recordset 屬性識別中包含的資料 (存取或 SQL) 類型。The read/write behavior of the Recordset property is determined by the type of recordset (ADO or DAO) and the type of data (Access or SQL) contained in the recordset identified by the property.

記錄集類型Recordset type 基於 SQL 資料Based on SQL data 基於 Access 資料庫引擎所儲存的資料Based on data stored by the Access database engine
ADOADO 可讀寫的。Read/write 可讀寫的。Read/write
DAODAO N/AN/A 可讀寫的。Read/write

下列範例會開啟表單、 開啟記錄集,並再將表單資料錄集繫結表單的 Recordset 屬性設定為新建立的 記錄集 物件。The following example opens a form, opens a recordset, and then binds the form to the recordset by setting the form's Recordset property to the newly created Recordset object.

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屬性:Use the Recordset property to:

  • 使用Recordset物件中 forms 上未直接支援方法。Use methods with the Recordset object that aren't directly supported on forms. 例如,您可以使用 Recordset 屬性 ADO 尋找 或 DAO 尋找 方法,在 [自訂] 對話方塊中的尋找記錄。For example, you can use the Recordset property with the ADO Find or DAO Find methods in a custom dialog for finding a record.

  • 環繞異動 (這可以回復) 一組會影響多表單的編輯四周。Wrap a transaction (which can be rolled back) around a set of edits that affect multiple forms.

    變更表單的 Recordset 屬性可能也會變更的 記錄來源記錄集類型 ,然後 RecordLocks 屬性。Changing a form's Recordset property may also change the RecordSource, RecordsetType, and RecordLocks properties. 此外,某些資料相關的屬性可能會覆寫,範例、 篩選FilterOn排序順序 ,和 OrderByOn 屬性。Also, some data-related properties may be overridden, for example, the Filter, FilterOn, OrderBy, and OrderByOn properties.

    呼叫表單的 recordset Requery方法 (例如Forms(0).Recordset.Requery) 可能會導致成為繫結表單。Calling the Requery method of a form's recordset (for example, Forms(0).Recordset.Requery) can cause the form to become unbound. 若要重新整理繫結至資料錄集表單中的資料、 設表單的RecordSource屬性本身: Forms(0).RecordSource = Forms(0).RecordSourceTo refresh the data in a form bound to a recordset, set the RecordSource property of the form to itself: Forms(0).RecordSource = Forms(0).RecordSource.

  • 將多個表單繫結至一組通用的資料。Bind multiple forms to a common data set. 這種情況允許多表單的同步處理。This allows synchronization of multiple forms. 例如:For example:

      Set Me.Recordset = Forms!Form1.Recordset
    

注意

當表單繫結至資料錄集時,如果您使用 [依表單篩選] 命令,會發生錯誤。When a form is bound to a recordset, an error occurs if you use the Filter by Form command.

範例Example

下列範例會使用 Recordset 屬性,若要從目前的表單中建立新複本的 Recordset 物件,然後列印 [偵錯] 視窗中的欄位名稱。The following example uses the Recordset property to create a new copy of the Recordset object from the current form and then prints the names of the fields in the Debug window.

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物件至與表單的目前記錄同步處理資料錄集。The following example uses the Recordset property and the Recordset object to synchronize a recordset with the form's current record. 選取公司名稱時,從下拉式方塊, FindFirst 方法用來尋找記錄的公司,使表單顯示所找到的記錄。When a company name is selected from a combo box, the FindFirst method is used to locate the record for that company, causing the form to display the found record.

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 屬性。The following code helps to determine what type of recordset is returned by the Recordset property under different conditions.

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

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.