Solved

Getting DLookup to return a Null value in Access - VB6

Posted on 2006-07-18
7
438 Views
Last Modified: 2012-06-22
Hello experts,

This should be pretty easy but I cannot figure it out.  I am new to VB and having problems.

I am trying to make a form in Access that will allow the user to input their employee number into a textbox.  Once they click the button, it will look for thier name in a table and retreive their info or return a Null value if the number is not found.  I have had not problems having it find records that are there, but I cannot figure out how to make it return a Null if there is no record found.

Here is my code:

' NumberInputTxt , FirstNameTxt , and ValidTxt  are all textboxes
' identipass is the table I am looking in

' the problem is I cannot get varX to = Null.  I have also tried if varX="" which did not work


Private Sub numberinputtxt_Click()

Dim CardNumber As String
Dim varX As Variant

CardNumber = NumberInputTxt.Text

varX = DLookup("[First]", "Identipass", "[Number]= " & CardNumber)

FirstNameTxt = varX

If varX = Null Then
ValidTxt = "Null"

Else

ValidTxt = "Found"
End If

End Sub

----------------------------------------------------------

Thanks in advance!
0
Comment
Question by:paries
  • 4
  • 2
7 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 17133256
If varX Is Null Then
ValidTxt = "Null"

Dave
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 17133291
DLOOKUP always returns null if no record foind

Flavo has given u the right syntax. The other way  is    If IsNull(varx) = True then



Alternativly, u can use NZ to return a string instead

varX = NZ(DLookup("[First]", "Identipass", "[Number]= " & CardNumber),"")
if varX = "" then
    msgbox "Not found"


0
 

Author Comment

by:paries
ID: 17133436
If varX Is Null then    gave me a runtime error 424 for some reason.  The other suggestion worked great.  Thanks
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 34

Expert Comment

by:flavo
ID: 17133475
what?
0
 
LVL 34

Expert Comment

by:flavo
ID: 17133500
Well I'll be...I doesn't work :-(

Dave
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17133615
Dave, thinking about it, I think may be it will work for object references only
0
 
LVL 34

Expert Comment

by:flavo
ID: 17134297
Yeap :-(
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now