DLookup with If Else and error messages (1 Viewer)

Tiger6115

Registered User.
Local time
Today, 02:19
Joined
Jun 25, 2012
Messages
24
Looking for help with DLookup function VBA using If Else conditions. I have a form for adding new carriers to an existing carrier table. Because I know these users :banghead:, I would like for it to:

1. If the ID No. field is left blank - show an error message in an unbound text box.
2. If the ID entered in the ID No. Field already exists in the table - display an error message in an unbound text box and display the the carrier ID and Name associated with it.

I have it set on the got focus of the field immediately after the ID No. Field. I've searched and search and tried a number of ways, i know it's probably a syntax issue. Any help is much appreciated.

Here is what I have so far:

If IsNull(Me.AddCarrierNo) Then
Me.ErrMesgBoxTxt = "Please enter a Carrier/SI No."

Else

If DLookup("[CarrierNo]", "ExamCarrier", "[CarrierNo] = " & Me.AddCarrierNo) = True Then

Me.ErrMesgBoxTxt = "Carrier/SI Number already exists. Please verify Carrier information."
Me.ErrMesgBoxTxt.Requery
Me.ErrMesgBoxID = DLookup("[CarrierNo]", "[ExamCarrier]", "[CarrierNo] = " & Me.AddCarrierNo)
Me.ErrMesgBoxID.Requery
Me.ErrMsgBoxName = DLookup("[CarrierName]", "[ExamCarrier]", "[CarrierNo] = " & Me.AddCarrierNo)
Me.ErrMsgBoxName.Requery


End If
End If
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:19
Joined
Oct 29, 2018
Messages
21,454
Hi. Not sure if you really need an If/Then statement for this. You should be able to display everything in one Textbox. For example:
Code:
=Nz(DLookup("'Carrier already exists: ' & [CarrierName]","ExamCarrier","[CarrierNo]=" & Nz(Me.AddCarrierNo,0)),"Please enter a Carrier No")
(untested)
Hope it helps...
 

Tiger6115

Registered User.
Local time
Today, 02:19
Joined
Jun 25, 2012
Messages
24
Hey theDBguy, thank you for your help again. It appears when I try to enter information for a new carrier (one not currently in the table) a new Carrier ID No. is giving me the "Please enter Carrier No" error. I wanted that error to only show if the user left the filed blank.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:19
Joined
Oct 29, 2018
Messages
21,454
Hey theDBguy, thank you for your help again. It appears when I try to enter information for a new carrier (one not currently in the table) a new Carrier ID No. is giving me the "Please enter Carrier No" error. I wanted that error to only show if the user left the filed blank.
Hi. For that case, you should be able to add an IIf() statement to check if it's blank or not first.
 

Tiger6115

Registered User.
Local time
Today, 02:19
Joined
Jun 25, 2012
Messages
24
I'm sorry, I can't wrap my brain around where the IIF statement would go. I've tried adding it around the code you provided me, i've tried it around the Nz(Me.AddCarrierNo), 0) and I get errors either way.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:19
Joined
Oct 29, 2018
Messages
21,454
I'm sorry, I can't wrap my brain around where the IIF statement would go. I've tried adding it around the code you provided me, i've tried it around the Nz(Me.AddCarrierNo), 0) and I get errors either way.
Let's see. Try this and let us know what happens.

=Nz(DLookup("'Carrier already exists: ' & [CarrierName]","ExamCarrier","[CarrierNo]=" & Nz(Me.AddCarrierNo,0)),IIf(IsNull([CarrierNo]),"Please enter a Carrier No",""))
 

Tiger6115

Registered User.
Local time
Today, 02:19
Joined
Jun 25, 2012
Messages
24
That worked perfectly. As always your help/guidance is so very much appreciated. Thank you!!
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:19
Joined
Oct 29, 2018
Messages
21,454
That worked perfectly. As always your help/guidance is so very much appreciated. Thank you!!
Hi. You're welcome. Glad to hear it worked for you. Good luck with your project.
 

Users who are viewing this thread

Top Bottom