DLookUp as criteria within DLookup (1 Viewer)

Minty

AWF VIP
Local time
Today, 11:38
Joined
Jul 26, 2013
Messages
10,371
This is close - but you still need to concatenate the string into the dlookup

Code:
Dim MgrEmailX As String
MgrEmailX = DLookup("[MgrEmail]", "[Admin Table]")

MsgBox ("This from the Employee Form: " & DLookup("[Email]", "[Employee Table]", "[EMail] = [COLOR="Red"]'" & MgrEmailX & "'")[/COLOR])

However if you stored the managers employee ID in a field in the employee table , you could simply look up the manager email based on their emp id? This would allow you to have different managers per employee but no need to adjust your code.
 

chuckcoleman

Registered User.
Local time
Today, 05:38
Joined
Aug 20, 2010
Messages
363
Minty,

Thank you, I modified my code to match yours but I still get the message: "This from the Employee Form: " without anything after that. This is one of the scenarios I was dealing with before. Any ideas?

Chuck
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:38
Joined
Sep 21, 2011
Messages
14,268
PMFJI,
You are looking up for a [Email] supplied with a value ?

Is the Dlookup producing Null?
 

Mark_

Longboard on the internet
Local time
Today, 03:38
Joined
Sep 12, 2017
Messages
2,111
Debug 101.. Verify each step.
Code:
Dim MgrEmailX As String
MgrEmailX = DLookup("[MgrEmail]", "[Admin Table]")
msgbox "Manager EMail to look up is " & MgrEmailX ' Make sure you have one to begin with.

Dim EmailX As String
EmailX = DLookup("[Email]", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'")

MsgBox ("This from the Employee Form: " & EMailX )

If you get your managers EMail, but don't get the matching value from your employee table, then go in to your employee table and verify that the Email is actually there AND that they match.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:38
Joined
Sep 21, 2011
Messages
14,268
Well yes, but that statement is saying
Lookup the email address for the email address I am supplying, so no need to look it up.?

You probably want to say
Lookup the email address for the manager email address I am supplying.?

So it would be more like
Code:
MsgBox ("This from the Employee Form: " & DLookup("[Email]", "[Employee Table]", "[MgrEMail] = '" & MgrEmailX & "'"))

However that would pick up the first email address for an employee that the Manager manages.?

Without knowing your DB, it is a bit hard to say, but there is no point looking up an email address for the one you are supplying.?

A normal DLookup returns a particular field depending on the value of *another* field.?

HTH




 

chuckcoleman

Registered User.
Local time
Today, 05:38
Joined
Aug 20, 2010
Messages
363
Mark,

Before I asked for help, I tested via the DIM statement to make sure I could see the MgrEmailX in a MsgBox. That worked.

I also copied the matching email from the [Employee Table] to the [Admin Table] to make sure they were perfect matches. Still didn't work.

I then took your suggestion and added the second DIM statement EMailX = DLookup("", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'") and used EMailX in the message box AND IT WORKED!

Thank you. It took declaring it to make it work. I appreciate your help.

Chuck
 

Mark_

Longboard on the internet
Local time
Today, 03:38
Joined
Sep 12, 2017
Messages
2,111
Mark,

Before I asked for help, I tested via the DIM statement to make sure I could see the MgrEmailX in a MsgBox. That worked.

I also copied the matching email from the [Employee Table] to the [Admin Table] to make sure they were perfect matches. Still didn't work.

I then took your suggestion and added the second DIM statement EMailX = DLookup("", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'") and used EMailX in the message box AND IT WORKED!

Thank you. It took declaring it to make it work. I appreciate your help.

Chuck[/QUOTE]

Please note, that SHOULDN'T have fixed your issue, at least not in a logical manner. Then again I've found that occasionally ACCESS isn't all that logical in how things are presented.

My best guess would be that MsgBox is expecting nothing more than what is to be displayed to be passed to it, so it isn't actually firing off the DLookup. Due to how I code, I tend to avoid passing the return from a function as part of the parameters I pass to other functions. I normally have a firmly declared variable I hold the value in, just to make sure I'm not passing bad values to functions.

Maybe ACCESS is just upset you didn't sacrifice a chicken to it prior to coding?
 

chuckcoleman

Registered User.
Local time
Today, 05:38
Joined
Aug 20, 2010
Messages
363
Mark, I can only assume it is the chicken's fault.

Thanks again.

Chuck
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:38
Joined
Sep 21, 2011
Messages
14,268
It doesn't take much to confuse me I admit, but your previous syntax should have worked if the email address supplied is in that table?. At least as far as I can see, so if I am missing something, I'd like to be shown what I have missed.?

Is your intent just to see if the email address exists in the Employee table.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:38
Joined
Sep 21, 2011
Messages
14,268
Code:
? msgbox("Test : " & dlookup("Data","Lookups","Data = 'Teresa Hawkins'"))

works for me?
So I believe you never had the email address in the table in the first place.?

If I change mine to
Code:
? msgbox("Test : " & dlookup("Data","Lookups","Data = 'Teresa Hawkins' AGG"))

I get the results you were getting, just Test : in my case.
 

chuckcoleman

Registered User.
Local time
Today, 05:38
Joined
Aug 20, 2010
Messages
363
OK, new issue. The following code works if the [MgrEmail] from the [Admin Table] finds a matching from the [Employee Table]. In testing, if I made it so there weren't any matching [Email] addresses in the [Employee table], I get an "Invalid use of Null" error in the following line:

EmailX = DLookup("[Email]", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'")

The code stops at this point and doesn't allow me to do something like, If IsNull( EmailX = DLookup("[Email]", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'"))

Any ideas?

Chuck
 

chuckcoleman

Registered User.
Local time
Today, 05:38
Joined
Aug 20, 2010
Messages
363
I got it:

If IsNull(EmailX = DLookup("", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'")) Then
EmailX = "XYZ"
Else
EmailX = EmailX = DLookup("[Email]", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'")
End If
 

Mark_

Longboard on the internet
Local time
Today, 03:38
Joined
Sep 12, 2017
Messages
2,111
Code:
If IsNull(EmailX = DLookup("[Email]", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'")) Then
EmailX = "XYZ"
Else
EmailX = EmailX = DLookup("[Email]", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'")
End If

Can be replaced with
Code:
EmailX = NZ(DLookup("[Email]", "[Employee Table]", "[EMail] = '" & MgrEmailX & "'"),"XYZ")
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:38
Joined
Feb 28, 2001
Messages
27,172
My question is whether there is a relationship that you could have originally used to short-cut one of the two DLookups you had asked about. DLookup doesn't care whether your inquiry is based on a table or on a JOIN query; therefore, you could have provided a query to JOIN two tables and make a single lookup based on multiple criteria.

By the way, this syntax:

Code:
MgrEmailX = DLookup("[MgrEmail]", "[Admin Table]")

This always returns the FIRST entry in the named table (not necessarily the first entry by alphabetic sorting, howerver!) If that first thing is Null then you will get back a Null.
 

Users who are viewing this thread

Top Bottom