>
Dim strCriteria As String, strNewTitle As String strCriteria = "Title = 'My Right Hand'" ' Create the criteria. strNewTitle = "My Right Foot" ' Create a new title. rstTitles.FindFirst strCriteria ' Make record current. Do While Not rstTitles.NoMatch rstTitles.Edit ' Open record. rstTitles.Fields("Title") = strNewTitle ' Enter new title. rstTitles.Update ' Save changes. rstTitles.FindNext strCriteria LoopThe following example achieves the same effect without using the Edit method.
' Create the criteria string. strSQLQuery = "UPDATE Titles SET Title = 'My Right Foot'" & _ " WHERE Title = 'My Right Hand';" dbsPublishers.Execute strSQLQuery ' Execute the query.This example opens a Recordset object and locates each record whose Title field satisfies the search criteria and copies it to the copy buffer. The example then prepares the record for subsequent editing, changes the job title, and saves the change using the Update method.
Dim dbsNorthwind As Database, rstEmployees As Recordset Dim strCriteria As String, strNewTitle As String ' Set search criteria. strCriteria = "Title = 'Sales Representative'" strNewTitle = "Account Executive" ' Set new job title. Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb") ' Create dynaset. Set rstEmployees = dbsNorthwind.OpenRecordset("Employees", _ dbOpenDynaset) rstEmployees.FindFirst strCriteria ' Find first occurrence. ' Loop until no matching records. Do Until rstEmployees.NoMatch With rstEmployees .Edit ' Enable editing. !Title = strNewTitle ' Change title. .Update ' Save changes. .FindNext strCriteria ' Find next occurrence. End With Loop ' End of loop.Tip Using an update query to change job titles might be more efficient. For example, you could use the following code to achieve the same results:
strSQL = "Update Employees Set Title = 'Account Executive' " & _ "WHERE Title = 'Sales Representative' " dbsNorthwind.Execute strSQLExample (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 Database variable pointing 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 End SubTip Using an update query to alter data is more efficient. For example, you can use the following code to achieve the same results.
strSQL = "Update Employees Set Title = 'Account Executive' " & _ "WHERE Title = 'Sales Representative' " dbs.Execute strSQL