Applies To Dynamic-Type Recordset object, Dynaset-Type Recordset object, Forward-Only–Type Recordset object, Recordset object, Snapshot-Type Recordset object, Table-Type Recordset object.
Description
Copies the current record from an updatable Recordset object to the copy buffer for subsequent editing.
Syntax recordset.Edit The recordset placeholder represents an open, updatable Recordset object that contains the record you want to edit. 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.See Also AddNew method, Delete method, LockEdits property.
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
Example (Microsoft Access)
The following example opens a Recordset object and locates each record satisfying the search criteria for the Title field. The procedure then uses the Edit method to prepare the record for subsequent editing, changes the job title, and saves the change.
Sub ChangeTitle()
Dim dbs As Database, rst As Recordset
Dim strCriteria As String, strNewTitle As String
' Return reference to current database.
Set dbs = CurrentDb
' Set search criteria.
strCriteria = "Title = 'Sales Representative'"
strNewTitle = "Account Executive"
' Create dynaset-type Recordset object.
Set rst = dbs.OpenRecordset("Employees", dbOpenDynaset)
' Find first occurrence.
rst.FindFirst strCriteria
' Loop until no matching records.
Do Until rst.NoMatch
With rst
.Edit ' Enable editing.
!Title = strNewTitle ' Change title.
.Update ' Save changes.
.FindNext strCriteria ' Find next occurrence.
End With
Loop
rst.Close
Set dbs = Nothing
End Sub
Tip Using an update query to alter data is more efficient. For example, you can use the following code to achieve the same results:
Sub ChangeTitleSQL()
Dim dbs As Database
Dim strSQL As String
' Return reference to current database.
Set dbs = CurrentDb
' Create SQL string.
strSQL = "UPDATE Employees SET Title = 'Account Executive' " _
& "WHERE Title = 'Sales Representative' "
' Run action query.
dbs.Execute strSQL
' Return number of records updated.
Debug.Print dbs.RecordsAffected
Set dbs = Nothing
End Sub