Query Exclusion List (1 Viewer)

Zippersabrat

Blah Blah Blah :o)
Local time
Today, 01:57
Joined
May 5, 2010
Messages
31
Hi all! I have an Access Database that I am using to track the difference in product codes between two different external databases. I export the codes from both and import to their own table. I then run two queries, one to show what is missing from one and another to show what is missing from the other. I have a ton of exceptions that I have built out in the Expression Builder as seen below. I am wondering if I can create a table to track these so I can make it more user-friendly to allow others on my team to edit. Ideally, I could have a table and the a form for them to add to the list. Not sure if this is even possible so thought I would reach out to the experts before I even try to come up with something. Thanks in advance!

1623169989542.png
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:57
Joined
Oct 29, 2018
Messages
21,496
Is that the expression in your query criteria?

I think you can store them in a table and join that table in your query to automatically filter the data.
 

plog

Banishment Pending
Local time
Today, 03:57
Joined
May 11, 2011
Messages
11,657
Yes, you can put them in a table (CodeTable) and then LEFT JOIN from that table to the one you have (MainTable) to achieve the results you want.

Preferably you would breakout all the codes in MainTable to their own field. Perhaps all those prefixes are followed by a dash and then other data (e.g. VX-12378, VX-9877, etc). That way you can put the VX in one field and the 12378 and 9877 in another. Then its a striaght LEFT JOIN from CodeTable to identify matches.


...LEFT JOIN CodeTable ON CodeTable.Code = MainTable.Code...

If not, you can use a non-equi join. 90% of the time a LEFT JOIN looks like the above, but it doesn't have to be an equals sign. If you can't break the code out into its own field you can do this:

...LEFT JOIN CodeTable ON MainTable.WholeCode LIKE CodeTable.Code & "*"...

I am sure that syntax is incorrect with respect to the wildcard character. But it demonstrates that you don't have to use an equals sign in your LEFT JOIN.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:57
Joined
May 21, 2018
Messages
8,555
Maybe a table like this.
tblExclusions tblExclusions

ExclusionIDExclusionTextExclusionType
1​
IDRBeginning
2​
PKGAnywhere
3​
MRKAnywhere
4​
TR_0017Match
5​
TSTEnding
Code:
Public Function GetExclusions(YourFieldName As String) As String
  Dim strEx As String
  Dim rs As DAO.Recordset
  Set rs = CurrentDb.OpenRecordset("tblExclusions")
  Do While Not rs.EOF
    Select Case rs!exclusionType
      Case "Anywhere"
        strEx = "'*" & rs!ExclusionText & "*'"
      Case "Beginning"
        strEx = "'" & rs!ExclusionText & "*'"
      Case "Ending"
        strEx = "'*" & rs!ExclusionText & "'"

      Case "Match"
        strEx = "'" & rs!ExclusionText & "'"
     End Select
     If GetExclusions = "" Then
       GetExclusions = YourFieldName & " NOT LIKE " & strEx
     Else
       GetExclusions = GetExclusions & " AND " & YourFieldName & " NOT LIKE " & strEx
     End If
    rs.MoveNext
  Loop

End Function

Results passing in Fld1 as the field name
Code:
fldone NOT LIKE 'IDR*' AND fldone NOT LIKE '*PKG*' AND fldone NOT LIKE '*MRK*' AND fldone NOT LIKE 'TR_0017' AND fldone NOT LIKE '*TST'

Then have a form with a button that gets the string above and updates and opens the query.

Probably should have use the terms "Starts with", "Ends With" instead of beginning and ending.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:57
Joined
May 7, 2009
Messages
19,247
you can create a table with those exclusion list (first letters )
exclusion_table.png

then create Function that will check this Table:
Code:
Public Function IsExcluded(ByVal pFieldValue As Variant) As Boolean
    Dim iX As Integer
    Dim rs As DAO.Recordset
    pFieldValue = Trim$(pFieldValue & "")
    iX = Len(pFieldValue)
    If iX < 1 Then
        Exit Function
    End If
    With CurrentDb.OpenRecordset("tblExlusion", dbOpenSnapshot, dbReadOnly)
        .MoveFirst
        Do Until .EOF
            IsExcluded = (pFieldValue Like ![Excluded] & "*")
            If IsExcluded Then Exit Do
            .MoveNext
        Loop
    End With
End Function

your Criteria in Query:

Where IsExcluded([theFieldToCheck]) = False
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:57
Joined
Feb 28, 2001
Messages
27,229
It may be easier from a logic standpoint to reverse your logic. Add flag fields to show EXCLUDED (perhaps as a Boolean or Yes/No field).
Then put those things in a table, call it EXCL.

EXCLUDED: KeyValue
where the KeyValue is things like "IDR*" or "VX*" or "PKG*"

Then do an update:

Code:
UPDATE MainTable SET EXCLUDED = 0'
UPDATE MainTable INNER JOIN EXCL ON MainTable.MyField LIKE EXCL.KeyValue SET MainTable.EXCLUDED = 1;

Now use the EXCLUDED flag in your other query's WHERE clause to exclude the records that were marked as such. This approach might actually be faster than that ugly exclusion list, and has the further advantage that you can add other stuff to the exclusions list because now it is just a table.
 

Users who are viewing this thread

Top Bottom