Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tell when a record has not been found in DLookup

Status
Not open for further replies.

paulbradley

Programmer
Oct 9, 2002
158
GB

I have a form in MSAccess that you type a code into and on enter it looks this up in a table and displays the information ascciated with this in a textbox.

I would like a MsgBox to appear if the record does not exist - how can I do this?

Thanks.
 
use another aggreate function to test first

if dcount("filedname","table","criteria) = 0 then
msgbox "no records match that
else
'dlookup code to do other stuff here
end if

 

Adraid that dosent work, I'm using DLookup and if something isnt found the variable that is to be set by the incoming data does not go to 0.
 
Hi paulbradley,

Try this syntax, next time maybe name your tables, controls etc, can give more exact examples then:

Dim x
x = DLookup("Field1", "YourTableName", "Field2 ='1234'")
If IsNull(x) Then
MsgBox "Null"
Else
MsgBox x
End If

Bill
 
Hi,

One of the better way to avoid null error at the first place could be to write this

If Nz(Dlookup("Field","Table","Criteria"),"NA")="NA" Then
Msgbox("Not found")
Else
'Your Code
End if



Cheers!
ÙÇãá
 
Paul,

The DLookup will return Null if the value is not found.

Try:

Dim varIsFound As Variant

varIsFound = DLookup(...)

If IsNull(varIsFound) Then
Msgbox("Not found.")
End If

Wayne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top