Solved DLookup with criteria based on a text field on a form (1 Viewer)

chuckcoleman

Registered User.
Local time
Today, 14:17
Joined
Aug 20, 2010
Messages
363
Hi,
I've been going nuts trying to get this to work.
In my form, in the After Update for a control, [LotNbrX], I want the value in that control to be used to establish the value of another control on the form called [EMailX]. Note: [LotNbrX] is a text field.

If I use a MsgBox to test this, MsgBox ("The value of LotNbr is: " & Me.LotNbrX), before the code below runs, and I enter 19 in the [LotNbrX] control, the MsgBox returns "The value of LotNbr is: 19", which is correct. The next step in the code is to set the value of EMailLU as shown below. It returns, "An Invalid Use of Null" when it runs.

Code:
Dim EmailLU as String
EMailLU = DLookup("[Email]", "[Email for Receipt]", "[LotNbr] =  ' " & [Forms]![Revenue Input Form].[LotNbrX] & " ' ")
Me.EmailX = EmailLU

What am I missing/doing wrong?

Thank you for your help - Chuck
 

plog

Banishment Pending
Local time
Today, 14:17
Joined
May 11, 2011
Messages
11,646
Divide, isolate and conquer.

1. Remove the criteria from your Dlookup entirely. If it returns a value, you know the criteria is the issue. If not, then the problem is either the field or the table name.

2. Make your criteria a string and msgbox that out. Then you can see exactly what the criteria is. And to be fair, the thing you messaged box out isn't exactly the same as what is in your Dlookup criteria (Me.LotNbrX & [Forms]![Revenue Input Form].[LotNbrX])

3. Make sure that field and value of the criteria is actually in [Email for Receipt].
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:17
Joined
Oct 29, 2018
Messages
21,474
DLookup() returns a Null if it doesn't find a match. Try wrapping it inside an Nz() function.
 

chuckcoleman

Registered User.
Local time
Today, 14:17
Joined
Aug 20, 2010
Messages
363
Thanks guys. I just have to believe the problem is that the criteria and the field in the query are text. It's hard to tell in a MsgBox when you see the value if it's right-aligned, (number), or left-aligned, (text). The data is in the query so there absolutely should be a match. Here are a couple of things I've tried.

Code:
   Dim EmailLU As String
   Dim EmailLU2 As String
  
   MsgBox ("The value of LotNbr is: " & Me.LotNbrX)
   MsgBox ("The value of the criteria is: " & [Forms]![Revenue Input Form].[LotNbrX])
   EmailLU = Nz([Forms]![Revenue Input Form].[LotNbrX])
   MsgBox ("The value of EmailLU is: " & EmailLU)
   EmailLU2 = DLookup("[Email]", "[Email for Receipt]", "[LotNbr] = ' " & Nz(EmailLU) & " ' ")
   MsgBox ("22-The value of EmailLU2 is: " & EmailLU2)
   If IsNull(Me.LotNbrX) Then

Both with and without Nx. I still get the Invalid Use of Null immediately before the MsgBox that starts with "22-....."
 

Josef P.

Well-known member
Local time
Today, 21:17
Joined
Feb 2, 2023
Messages
826
Code:
EmailLU2 = DLookup("[Email]", "[Email for Receipt]", "[LotNbr] = ' " & Nz(EmailLU) & " ' ")
Test:
Code:
dim CriteriaString as String
CriteriaString = "[LotNbr] = ' " & Nz(EmailLU) & " ' "
msgbox "CriteriaString: " & CriteriaString
EmailLU2 = DLookup("[Email]", "[Email for Receipt]", CriteriaString) '<-- will return Null, if no record found
Is the critera correct?
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:17
Joined
Sep 21, 2011
Messages
14,310
Why the spaces in the criteria?
 

chuckcoleman

Registered User.
Local time
Today, 14:17
Joined
Aug 20, 2010
Messages
363
Hi Josef P. It returns, [LotNbr] ' '. Null. What I don't understand is why all of the "test" msgbox's ahead of this result return 19 which is the number I've entered into [LotNbr].
 

chuckcoleman

Registered User.
Local time
Today, 14:17
Joined
Aug 20, 2010
Messages
363
Josef P. I had the CriteriaString before I established EmailLU. After moving it I do get: [LotNbr] = 19. After that msgbox I still get Invalid Use of Null immediately before the msgbox that starts "22-....".

Code:
Dim EmailLU As String
   Dim EmailLU2 As String
   Dim CriteriaString As String
  
 
   MsgBox ("The value of LotNbr is: " & Me.LotNbrX)
   MsgBox ("The value of the criteria is: " & [Forms]![Revenue Input Form].[LotNbrX])
   EmailLU = Nz([Forms]![Revenue Input Form].[LotNbrX])
   CriteriaString = "[LotNbr] = ' " & Nz(EmailLU) & " ' "
   MsgBox ("The value of EmailLU is: " & EmailLU)
   MsgBox ("11-CriteriaString is: " & CriteriaString)
   EmailLU2 = DLookup("[Email]", "[Email for Receipt]", CriteriaString) '<-- will return Null, if no record found
   MsgBox ("22-The value of EmailLU2 is: " & EmailLU2)
 

chuckcoleman

Registered User.
Local time
Today, 14:17
Joined
Aug 20, 2010
Messages
363
This never results in [LotNbr] = 19 but [LotNbr] = ' 19 ' (if EmailLU = 19)
[SOLVED] Sometimes you can learn something every day. For readability I had in my DLookUp criteria, " space ' space ". When I removed the spaces, the criteria worked. Gasman pointed this out early on and I just didn't pay enough attention to it because I didn't think it mattered. It does.

Thank you all for helping! You all make a difference.
 

Users who are viewing this thread

Top Bottom