>
Dim strCriteria As String, varSaveHere As Variant Dim dbsNorthwind As Database, rstEmployees As Recordset strCriteria = "Title = 'Sales Representative'" ' Define search
' criteria. Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb") ' Create Recordset. Set rstEmployees = dbsNorthwind.OpenRecordset("Employees", dbOpenDynaset) varSaveHere = rstEmployees.Bookmark ' Save Bookmark. rstEmployees.FindFirst strCriteria ' Locate first occurrence. Do Until rstEmployees.NoMatch ' Loop until no matching
' records.
With rstEmployees .Edit ' Enable editing. !Title = "Account Executive" ' Change title. .Update ' Save changes. .FindNext strCriteria ' Locate next record. End With Loop ' End of loop. rstEmployees.Bookmark = varSaveHereTip In some situations, using an update query is faster. Example (Microsoft Access) The following example uses the NoMatch property to determine whether a FindFirst method process has been successful.
Function FindCountry() As Integer Dim intI As Integer, varRecord() As Variant Dim dbs As Database, rstOrders As Recordset Dim strCountry As String ' Return Database variable that points to current database. Set dbs = CurrentDb ' Create Dynaset-Type Recordset object. Set rstOrders = dbs.OpenRecordset("Orders", dbOpenDynaset) strCountry = InputBox("Please enter country name.") intI = 0 rstOrders.FindFirst "[ShipCountry] = '" & strCountry & "'" If rstOrders.NoMatch Then FindCountry = False Exit Function Else Debug.Print rst!OrderID End If FindCountry = True End FunctionExample (Microsoft Excel) This example adds all the names of contacts for the state of Washington to a list box on a new dialog sheet and then runs the dialog box. The data is drawn from the Customer recordset in the NWINDEX.MDB database. To create the NWINDEX.MDB database, run the Microsoft Excel example for the CreateDatabase method.
Dim db As Database Dim rs As Recordset Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB") Set rs = db.OpenRecordset("SELECT * FROM Customer") criteria = "[REGION] = 'WA'" Set customDialog = DialogSheets.Add
Set list1 = customDialog.ListBoxes.Add(78, 42, 84, 80) rs.FindFirst criteria Do Until rs.NoMatch list1.AddItem (rs.fields("CONTACT").Value) rs.FindNext criteria Loop customDialog.Show rs.Close db.Close