Use IF function to identify if identical values exist in another table?

gojets1721

Registered User.
Local time
Yesterday, 21:23
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.
 
Terrible statement.
Use DLookup.
Code:
If IsNull(DLookup("ComplaintNumber", "tblEmployees", "ComplaintNumber =" & Forms.frmComplaints.ComplaintNumber)) Then ...
 
I vote DCount:


No need to test for nulls because its guaranteed to return a value (even if value is 0)
 
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]
 
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".....
 
I'm with @plog dCount() is just simpler because it always returns a count. 0 means false. Any other value means true.
 
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:
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

Back
Top Bottom