Finding specific fields within large recordsets can be a
time consuming overhead and is a procedure commonly undertaken in many applications.
Consider determining if a record exists in a table, which in turn
determines if the record is edited or added as a new entry. One method
would be to step through each record in the recordset and check each one
against your search criteria. This is the common, but inefficient
approach. A better way is to use the search methods of the recordset
object, which can interrogate all the records quickly and move to the
matching record. The code below does exactly this:
'Find first record satisfying search criteria
rsExample.FindFirst "user_id = 18"
'check if search is successful
If rsExample.NoMatch Then 'no match found
rsExample.AddNew
'add AddNew record code here
rsExample.Update
Else 'match found
rsExample.Edit
'add Edit record code here
rsExample.Update
End If
If the search criteria does not uniquely identify individual records
then the FindNext, FindPrevious and FindLast method of the Recordset can
be employed to navigate multiple matching records.
|