Computing.Net > Forums > Programming > How to do a FindFirst in VBA-Access

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to do a FindFirst in VBA-Access

Reply to Message Icon

Name: Marc-André Gagnon
Date: August 31, 2002 at 20:12:26 Pacific
Comment:

I'm trying to do a FindFirst in VBA-Access to verify the MATCH or NOMATCH. Can anyone help me please ?
Thanx !



Sponsored Link
Ads by Google

Response Number 1
Name: JasonSLC
Date: September 3, 2002 at 09:04:07 Pacific
Reply:

Here's an example from the VBA help-file:

Dim dbsNorthwind As Database
Dim rstCustomers As Recordset
Dim strCountry As String
Dim varBookmark As Variant
Dim strMessage As String
Dim intCommand As Integer

Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set rstCustomers = dbsNorthwind.OpenRecordset( _
"SELECT CompanyName, City, Country " & _
"FROM Customers ORDER BY CompanyName", _
dbOpenSnapshot)

Do While True
' Get user input and build search string.

strCountry = _
Trim(InputBox("Enter country for search."))
If strCountry = "" Then Exit Do
strCountry = "Country = '" & strCountry & "'"

With rstCustomers
' Populate recordset.
.MoveLast
' Find first record satisfying search string. Exit
' loop if no such record exists.
.FindFirst strCountry
If .NoMatch Then
MsgBox "No records found with " & _
strCountry & "."
Exit Do

End If

Do While True
' Store bookmark of current record.
varBookmark = .Bookmark
' Get user choice of which method to use.
strMessage = "Company: " & !CompanyName & _
vbCr & "Location: " & !City & ", " & _
!Country & vbCr & vbCr & _
strCountry & vbCr & vbCr & _
"[1 - FindFirst, 2 - FindLast, " & _
vbCr & "3 - FindNext, " & _
"4 - FindPrevious]"
intCommand = Val(Left(InputBox(strMessage), 1))

If intCommand 4 Then Exit Do

' Use selected Find method. If the Find fails,
' return to the last current record.
If FindAny(intCommand, rstCustomers, _
strCountry) = False Then
.Bookmark = varBookmark
MsgBox "No match--returning to " & _
"current record."
End If

Loop

End With

Exit Do
Loop

rstCustomers.Close
dbsNorthwind.Close

Function FindAny(intChoice As Integer, _
rstTemp As Recordset, _
strFind As String) As Boolean

' Use Find method based on user input.
Select Case intChoice
Case 1
rstTemp.FindFirst strFind
Case 2
rstTemp.FindLast strFind
Case 3
rstTemp.FindNext strFind
Case 4
rstTemp.FindPrevious strFind
End Select

' Set return value based on NoMatch property.
FindAny = IIf(rstTemp.NoMatch, False, True)
End Function


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How to do a FindFirst in VBA-Access

How to put a password in a folder? www.computing.net/answers/programming/how-to-put-a-password-in-a-folder/7021.html

Help to create a function in Access www.computing.net/answers/programming/help-to-create-a-function-in-access/16210.html

How to clear a textbox in asp.net www.computing.net/answers/programming/how-to-clear-a-textbox-in-aspnet-/8234.html