RecordsetClone Property

Applies To

Form.

Description

You can use the RecordsetClone property to refer to a form’s Recordset object specified by the form’s RecordSource property.

Setting

The RecordsetClone property setting is a copy of the underlying query or table specified by the form’s RecordSource property. If a form is based on a query, for example, referring to the RecordsetClone property is the equivalent of cloning a Recordset object using the same query. If you then apply a filter to the form, the Recordset object reflects the filtering.

The RecordsetClone property is available only in Visual Basic and is read-only in all views.

Remarks

You use the RecordsetClone property to navigate or operate on a form’s records independent of the form itself. For example, you can use the RecordsetClone property when you want to use a method, such as the FindFirst method, that can’t be used with forms. The RecordsetClone property provides access to all the methods and properties that apply to a Recordset object.

When a new Recordset object is opened, its first record is the current record. If you use one of the Find or Move methods to make any record in the Recordset object current, you must synchronize the current record in the Recordset object with the form’s current record by assigning the value of the form’s Bookmark property to a string variable, and then assign the string variable to the data access Bookmark property of the Recordset object.

You can use the RecordCount property to count the number of records in a Recordset object. The following example shows how you can combine the RecordCount property and the RecordsetClone property to count the records in a form.


Forms!Orders.RecordsetClone.MoveLast= "My form contains " & Forms!Orders.RecordsetClone.RecordCount= Msg & " records.", vbInformation, "Record Count"Msg

Note If you close the form or if you change the form’s RecordSource property, the Recordset object is no longer valid. If you subsequently refer to the Recordset object or to previously saved bookmarks in the form or the Recordset object, an error will occur.

See Also

FindFirst, FindLast, FindNext, FindPrevious Methods; MoveFirst, MoveLast, MoveNext, MovePrevious Methods; Recordset Object.

Example

The following example uses the RecordsetClone property to create a Recordset object from the Orders form and then prints the names of the fields in the Debug window.


Sub Print_Field_Names()
    Dim rs As Recordset, intI As Integer
    Set rs = Me.RecordsetClone
    For intI = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(intI).Name        ' Print field names.
    Next intISub

The next example uses the RecordsetClone property and the Recordset object to synchronize a recordset’s record with the form’s current record. When a company name is selected from a combo box, the FindFirst method is used to locate the record for that company and the Recordset object’s Bookmark property is assigned to the form’s Bookmark property causing the form to display the found record.


Sub CompanyName_AfterUpdate()
    Dim rs As Recordset
    Dim strSearchName As String
    Set rs = Me.RecordsetClone
    strSearchName = Me!CompanyName
    rs.FindFirst "[CompanyName] = '" & strSearchName & "'"
        If rs.NoMatch Then
            MsgBox "Record not found"
        Else
            Me.Bookmark = rs.Bookmark
        End If
    rs.CloseSub