다음을 통해 공유


Recordset.Edit Method (DAO)

Copies the current record from an updatable Recordset object to the copy buffer for subsequent editing.

Syntax

expression .Edit

expression A variable that represents a Recordset object.

Remarks

Once you use the Edit method, changes made to the current record's fields are copied to the copy buffer. After you make the desired changes to the record, use the Update method to save your changes.

The current record remains current after you use Edit.

Note

If you edit a record and then perform any operation that moves to another record, but without first using Update, your changes are lost without warning. In addition, if you close recordset or end the procedure which declares the Recordset or the parent Database or Connection object, your edited record is discarded without warning.

Using Edit produces an error if:

  • There is no current record.

  • The Connection, Database, or Recordset object was opened as read-only.

  • No fields in the record are updatable.

  • The Database or Recordset was opened for exclusive use by another user (Microsoft Access workspace).

  • Another user has locked the page containing your record (Microsoft Access workspace).

In a Microsoft Access workspace, when the Recordset object's LockEdits property setting is True (pessimistically locked) in a multiuser environment, the record remains locked from the time Edit is used until the update is complete. If the LockEdits property setting is False (optimistically locked), the record is locked and compared with the pre-edited record just before it's updated in the database. If the record has changed since you used the Edit method, the Update operation fails with a run-time error if you use OpenRecordset without specifying dbSeeChanges. By default, Microsoft Access database engine-connected ODBC and installable ISAM databases always use optimistic locking.

Note

To add, edit, or delete a record, there must be a unique index on the record in the underlying data source. If not, a "Permission denied" error will occur on the AddNew, Delete, or Edit method call in a Microsoft Access workspace.

Example

This example uses the Edit method to replace the current data with the specified name. The EditName procedure is required for this procedure to run.

Sub EditX() 
 
 Dim dbsNorthwind As Database 
 Dim rstEmployees As Recordset 
 Dim strOldFirst As String 
 Dim strOldLast As String 
 Dim strFirstName As String 
 Dim strLastName As String 
 
 Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
 Set rstEmployees = _ 
 dbsNorthwind.OpenRecordset("Employees", _ 
 dbOpenDynaset) 
 
 ' Store original data. 
 strOldFirst = rstEmployees!FirstName 
 strOldLast = rstEmployees!LastName 
 
 ' Get new data for record. 
 strFirstName = Trim(InputBox( _ 
 "Enter first name:")) 
 strLastName = Trim(InputBox( _ 
 "Enter last name:")) 
 
 ' Proceed if the user entered something for both fields. 
 If strFirstName <> "" and strLastName <> "" Then 
 ' Update record with new data. 
 EditName rstEmployees, strFirstName, strLastName 
 
 With rstEmployees 
 ' Show old and new data. 
 Debug.Print "Old data: " & strOldFirst & _ 
 " " & strOldLast 
 Debug.Print "New data: " & !FirstName & _ 
 " " & !LastName 
 ' Restore original data because this is a 
 ' demonstration. 
 .Edit 
 !FirstName = strOldFirst 
 !LastName = strOldLast 
 .Update 
 End With 
 
 Else 
 Debug.Print _ 
 "You must input a string for first and last name!" 
 End If 
 
 rstEmployees.Close 
 dbsNorthwind.Close 
 
End Sub 
 
Sub EditName(rstTemp As Recordset, _ 
 strFirst As String, strLast As String) 
 
 ' Make changes to record and set the bookmark to keep 
 ' the same record current. 
 With rstTemp 
 .Edit 
 !FirstName = strFirst 
 !LastName = strLast 
 .Update 
 .Bookmark = .LastModified 
 End With 
 
End Sub