Use IF function to identify if identical values exist in another table? (1 Viewer)

gojets1721

Registered User.
Local time
Yesterday, 20:44
Joined
Jun 11, 2019
Messages
430
Terrible title, I'm sorry.

I have a form that looks at record one at a time. For whatever record I'm on, I want to use VBA via a command to check if that record's ComplaintNumber is listed at all in a completely separate table (tblEmployees). This table also has a ComplaintNumber field.

Here's the IF function I'm using:

Code:
    If ("SELECT ComplaintNumber FROM tblEmployees WHERE [ComplaintNumber] =" & [Forms]![frmComplaints]![ComplaintNumber]) = "" Then
        MsgBox "No Values present in tblEmployees"
    Else
        MsgBox "Values present in tblEmployees"
    End If

The issue is that it's always saying that values are present even though there aren't. Any suggestions? Let me know if an example DB is needed.
 

ebs17

Well-known member
Local time
Today, 05:44
Joined
Feb 7, 2020
Messages
1,949
Terrible statement.
Use DLookup.
Code:
If IsNull(DLookup("ComplaintNumber", "tblEmployees", "ComplaintNumber =" & Forms.frmComplaints.ComplaintNumber)) Then ...
 

plog

Banishment Pending
Local time
Yesterday, 22:44
Joined
May 11, 2011
Messages
11,647
I vote DCount:


No need to test for nulls because its guaranteed to return a value (even if value is 0)
 

plog

Banishment Pending
Local time
Yesterday, 22:44
Joined
May 11, 2011
Messages
11,647
First, I stand by DCount, but I am looking at your code more closely and want to point out a few things:

Code:
    If ("SELECT ComplaintNumber FROM tblEmployees WHERE [ComplaintNumber] =" & [Forms]![frmComplaints]![ComplaintNumber]) = ""

1. You can't just declare an SQL statement in VBA and have to return results. You must use DoCmd.RunSQL to execute your SQL. Two different languages requiring two different environments to run correctly. In VBA you get into the SQL environment by DoCmd.RunSQL:


Of course that's more for executing action queries (UPDATE/INSERT/DELETE, etc.) and not for SELECT queries. It just runs a query and doesn't do anything with the results.

2. Because of #1 your if statement is simply comparing strings to each other. "SELECT Complaint..." = "". Those two strings will never be equal because one has your SQL string in it and the other is empty. That if will always return false which means your code will always run the Else portion.[/CODE]
 

Solo712

Registered User.
Local time
Yesterday, 23:44
Joined
Oct 19, 2012
Messages
828
Terrible title, I'm sorry.

I have a form that looks at record one at a time. For whatever record I'm on, I want to use VBA via a command to check if that record's ComplaintNumber is listed at all in a completely separate table (tblEmployees). This table also has a ComplaintNumber field.

Here's the IF function I'm using:

Code:
    If ("SELECT ComplaintNumber FROM tblEmployees WHERE [ComplaintNumber] =" & [Forms]![frmComplaints]![ComplaintNumber]) = "" Then
        MsgBox "No Values present in tblEmployees"
    Else
        MsgBox "Values present in tblEmployees"
    End If

The issue is that it's always saying that values are present even though there aren't. Any suggestions? Let me know if an example DB is needed.
I think you always get the "values" message because the statement does not protect against null string.
Try
INI:
If Nz("Select".....
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 23:44
Joined
Feb 19, 2002
Messages
43,302
I'm with @plog dCount() is just simpler because it always returns a count. 0 means false. Any other value means true.
 

Josef P.

Well-known member
Local time
Today, 05:44
Joined
Feb 2, 2023
Messages
827
Or use DExists :)

Code:
Public Function DExists(Byval Domain As String, optional byval Criteria As Variant) As Boolean
   DExists = DCount("1", Domain, Criteria) > 0
   ' or
   'DExists = Nz(DLookup("True", Domain, Criteria), False)
   ' or
   ' ...
End Function
Then later, depending on the season or the phase of the moon, you can choose the one that suits you at the moment. ;)

Some variants that Colin tested:
 
Last edited:

gojets1721

Registered User.
Local time
Yesterday, 20:44
Joined
Jun 11, 2019
Messages
430
Terrible statement.
Use DLookup.
Code:
If IsNull(DLookup("ComplaintNumber", "tblEmployees", "ComplaintNumber =" & Forms.frmComplaints.ComplaintNumber)) Then ...
That worked! Thanks so much!
 

Users who are viewing this thread

Top Bottom