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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dlookup function is not working 1

Status
Not open for further replies.

ChrisHaynes

Technical User
Mar 9, 2006
80
GB
Hi. I have a register form, which is unbound. When I click the command button to create a new user, it is supposed to use the Dlookup function to check that there aren't any records including the first name and surname in the User table.

However, this doesn't work and the form still add's the record to the table. I have set the field 'username' to the primary key and no duplicates but the form still add the record but just doesn' add a duplicate to the table.

The code I am using is shown below:

Code:
If DLookup("[First Name]", "tblUsers") = Me![First Name] And DLookup("[Surname]", "tblUsers") = Me![Surname] Then
MsgBox "You Are Already A User", vbOKOnly & vbCritical, "User Already Exists"
DoCmd.Close


Can anyone help?

Thanks,
Chris.
 
DLookup function are of the form:
DLookup(expr, domain[, criteria])
If you don't specify criteria they will return random values of expr in the domain.

You should be using the criteria to look for a specific field value. If no matching records are found in the domain (tblUsers) is not found a DLookUp function will return Null. So, the code should be:

If Not IsNull(DLookup("[First Name]", "tblUsers","[First Name]='" & Me![First Name] & "'")) And Not IsNull(DLookup("[Surname]", "tblUsers", "[Surname]='" & Me![Surname] & "'")) Then
MsgBox "You Are Already A User", vbOKOnly & vbCritical, "User Already Exists"
DoCmd.Close
 
you may try this:[tt]
If DCount("*", "tblUsers", "[First Name]='" & Me![First Name] & "' AND Surname='" & Me!Surname & "'") > 0 Then[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for your help IanAble. That works perfect!

Cheers,
Chris.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top