Data Control

Finding Records

When using a Database, a crucial function is to be able to find specific records. You can do this using the Find and Seek functions of the Data control.

Data1.Recordset.FindFirst criteria
Data1.Recordset.FindLast criteria
Data1.Recordset.FindNext criteria
Data1.Recordset.FindPrevious criteria
Data1.Recordset.Seek comparison, key1, key2...key13

Find method

Begins searching at Search direction

FindFirst 

Beginning of recordset End of recordset 

FindLast 

End of recordset Beginning of recordset 

FindNext 

Current record End of recordset 

FindPrevious 

Current record Beginning of recordset 

The criteria property of the Find methods is quite simple, and takes the following format:

FieldName = Value

If FieldName contains a space, it must be contained by ":

Data1.Recordset.FindFirst "ISBN = '0-0131985-2-1'"

To see if either of these method has found an item check the NoMatch property:

If Data1.Recordset.NoMatch = True Then
    Msgbox "No match found"
End If

As the FindFirst method will move you to the beginning of the recordset if there is no match, you should bookmark the position, and return to it if no match is found.

Dim varBookmark As Variant
varBookmark = datTitles.Recordset.Bookmark
datTitles.Recordset.FindFirst "ISBN = '0-0131985-2-12'"
If datTitles.Recordset.NoMatch = True Then
    datTitles.Recordset.Bookmark = varBookmark
    MsgBox "No match found"
End If

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Java is to JavaScript what Car is to Carpet.” - Chris Heilmann