billsinc said:
I have an update statement that uses the IN predicate the exclude a
list of values from another table. However, I would like the values
to have wild cards at the end rather than having to store every
variation that I'm trying to exclude. Any suggestions?
This is usually frowned upon in this newsgroup,
but if you saved following function to a code module,
I believe following
WHERE NOT fFldInList([textfield],"ab*, cdefg, bk*")
will *slowly* exclude records
where textfield starts with "ab" or "bk"
or textfield = "cdefg"
'*** start code ***
Public Function fFldInList(strFld As Variant, _
strList As Variant) As Boolean
On Error GoTo Err_fFldInList
Dim arrList As Variant
Dim strElement As String
Dim strWildCard As String
Dim i As Integer
'Returns TRUE only if
' 1) field string = an element of list
' 2) field string starts with an element (less "*")
' that ends with "*"
'
'?fFldInList("abcd","ad,ab*")
'True
'?fFldInList("abcd","a**d,bc*")
'False
'?fFldInList("abcd","ad,abcd")
'True
fFldInList = False
If Len(Trim(strFld & "")) > 0 Then
'continue
Else
'field value either Null or ZLS
Exit Function
End If
If Len(Trim(strList & "")) > 0 Then
'continue
Else
'list either Null or ZLS
Exit Function
End If
arrList = Split(strList, ",", -1, vbTextCompare)
For i = 0 To UBound(arrList)
strElement = arrList(i)
If Right(strElement, 1) = "*" Then
'element ends in wildcard,
'so test if strElement (less "*") starts strFld
'strip element of wildcard
strWildCard = Left(strElement, Len(strElement) - 1)
'cannot be true if wildcard is bigger than strFld
If Len(strWildCard) > Len(strFld) Then
Exit Function
Else
'does field string start with strWildCard?
If strWildCard = Left(strFld, Len(strWildCard)) Then
fFldInList = True
Exit Function
Else
'continue comparisons
End If
End If
Else
'no wildcard, so test equality
If strElement = strFld Then
fFldInList = True
Exit Function
Else
'continue comparisons
End If
End If
Next i
Exit_fFldInList:
Exit Function
Err_fFldInList:
MsgBox Err.Description
Resume Exit_fFldInList
End Function
'*** end code ***
If nothing else, I'm sure purists will object
to so many function exit points....
I have used similar function when I am "assembling
the list" in code into a string variable, then redefining
or running a query from code, i.e., something like...
strList = "ad, ab*"
strSQL = "SELECT f1, f2, f3 FROM tbl " _
& "WHERE fFldInList([f1],""" & strList & """);"
Debug.Print strSQL
CurrentDb.QueryDefs("someqry").SQL = strSQL
would give following in Immediate Window
SELECT f1, f2, f3 FROM tbl WHERE fFldInList([f1],"ad, ab*");
and "someqry" would then have that SQL...