Find Record in Access using .FindFirst with Multiple Criteria

dloskot

New Member
Joined
Oct 18, 2015
Messages
43
I have spent several hours trying to debug this code. I have not been able to find the answer anywhere.
I have an Access DataBase I am running Access 2010. I want to search the DB to see if a record with several criteria already exists.
I am able to make it work with a single criteria but as soon as I put in an AND or OR I get one or more errors. In the below code the line with the And in it does not work but if I run either of the two lines below it, it works fine.
I hope you can help me .
Thanks Doug

VBA Code:
With KAH_Record
'The first line DOES NOT work, the next two lines do work.  I would like to be able to do
'about 4 different criteria at one.'
    .FindLast "[Payment Method]= '" & PayMethod & "'" And "[EMail]= '" & EMail & "'"
    .FindLast "[Payment Method]= '" & PayMethod & "'"
    .FindLast "[EMail]= '" & EMail & "'"
    
        
        If .NoMatch = False Then
            MsgBox "Found Match"
            'Get_Record_Request = False
            'Exit Function
        Else
        MsgBox "Not Found"
        End If
 
How about formatting the date

VBA Code:
.FindLast "[Payment Method]='" & PayMethod & "' AND [EMail]='" & EMail & "' AND [Amount Donated]=" & Amount_Donated & " AND [Event Date]=#" & Format(Event_Date, "yyyy-mm-dd") & "#"
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Formated the date but still having Type Mismatch error. The first line gets the Type Mismatch error, but the second line works fine.

VBA Code:
 .FindLast "[Payment Method]='" & PayMethod & "' AND [EMail]='" & EMail _
       & "' AND [Amount Donated]=" & Amount_Donated And "[Event Date]= #" & Format(Event_Date, "mm-dd-yyyy") & "#"
    
    .FindLast "[Event Date]= #" & Format(Event_Date, "mm-dd-yyyy") & "#"
 
Upvote 0
You are missing an ampersand. Was that line not shown in red?
1695304713456.png

That is an automatic sign that there is something wrong with the concatenation or syntax.
You are passing Amount_Donated And as your value/reference. Try
VBA Code:
strFind = "[Payment Method]='" & PayMethod & "' AND [EMail]='" & Email _
       & "' AND [Amount Donated]=" & Amount_Donated & " And [Event Date]= #" & Format(Event_Date, "mm-dd-yyyy") & "#"
 
Upvote 0
Put the criteria into a string variable, then debug.print it until you get it correct.
 
Upvote 0
That would not work here because it would err out when trying to process the line that would pass the string to the variable. Note that it is red.
Clarification: I mean that the same error message would be raised when trying to pass the string to a variable, not that it would also be red when doing that. So same error message, thus no farther ahead.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top