Access 2013 - if no match after find first record do stuff

boboivan

Board Regular
Joined
Feb 18, 2013
Messages
68
Hello there!

I run this code under a command button on my Main Menu to find the first record based on criteria in the form that I want to open:

Code:
Private Sub Command0_Click()
If TempVars("CurrentUser") = "admin" Then
DoCmd.OpenForm "Events"
Else
DoCmd.OpenForm "Events"
Forms("Events").Recordset.FindFirst "Event =" & DLookup("IDevent", "tblEvents", _
"IDevent=" & "IDevent" & " And [UserName] = '" & TempVars("CurrentUser") & "'")
[COLOR=#ff0000][B][I]'something to add in here[/I][/B][/COLOR]
End If
End Sub

The code works fine if it finds the current user in the records. If it doesn't find anything, I get an error.
How can I fix this error?
I've tried something with NoMatch syntax, but I couldn't achieve much.
Please help!
Thx!
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi,
What is the error message?
What is the NoMatch code that you tried?
 
Upvote 0
Hi xenou!

I fixed it:
The working code is:
Code:
Private Sub Command0_Click()
If TempVars("CurrentUser") = "admin" Then
DoCmd.OpenForm "Events"
Else
On Error GoTo Handler
DoCmd.OpenForm "Events"
Forms("Events").Recordset.FindFirst "Event =" & DLookup("IDevent", "tblEvents", _
"IDevent=" & "IDevent" & " And [UserName] = '" & TempVars("CurrentUser") & "'")
Handler:
End If
End Sub

Thx anyway!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,826
Messages
6,162,192
Members
451,752
Latest member
majbizzaki

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