thread705-74658
In 2001 I said here I had 2 functions I made to ease the creation of SQL strings. Then forgot it and now I come back -12 years later!- and find some of you wanted the functions. So I better post them before forgetting it and returning in 2025, ok?
MakeSQL("Red/Green/Blue", "Color") returns
[Color] Like '*Red*' OR [Color] Like '*Green*' OR [Color] Like '*Blue*'
I used this function with a text box in a form to find some records based on the words an analyst remebered the record contained. Then I added this to the WHERE part of the main SQL string.
I have a twin function named MakeNOTSQL() that only changes this part:
It's been 12 years but here it is! Hope it's useful for anyone.
To boldly code, where no programmer has compiled before!
In 2001 I said here I had 2 functions I made to ease the creation of SQL strings. Then forgot it and now I come back -12 years later!- and find some of you wanted the functions. So I better post them before forgetting it and returning in 2025, ok?
Code:
'***** Find separator symbols in a string. Sort of a customizable ispunct() *****
Public Function IsSeparator(Caracter As Variant) As Boolean
Caracter = UCase(Caracter)
Select Case Caracter
Case ",", ".", "-", "(", ")", ":", ";", "#", "¿", "?", "¡", "!" '** Add " " (space) if needed
IsSeparator = True
Case "[", "{", "}", "]", "\", "/", "'", "_", Chr(34) '** Chr(34) is double quotes ("),
IsSeparator = True
Case Else
IsSeparator = False
End Select
End Function
'***** Make SQL string from a collection of words *****
Public Function MakeSQL(InputPhrase, Field2Use As Variant) As Variant
Dim NewSpace, LastOne, LastSpace, n As Integer
Dim CurrCar, FirstChar, LastChar, Word As String
Dim OutputPhrase As String
'*** Variable Cleanup
NewSpace = -1: LastSpace = -1: LastOne = Len(InputPhrase)
Word = "": FirstChar = ""
'*** Advance thru the chars of the phrase, checking for separators
For n = 1 To LastOne
CurrCar = Mid$(InputPhrase, n, 1)
'*** If current char is separator or the last in a string
'*** set the word boundaries
If IsSeparator(CurrCar) Or (n = LastOne) Then
LastSpace = NewSpace + 1
If n = LastOne Then
NewSpace = n
Else
NewSpace = n - 1
End If
'Find first char of word
FirstChar = Mid$(InputPhrase, (LastSpace + 1), 1)
'If first character is not alpha, then it's not written right or it's a digit
'or has a symbol attached, like parentheses or square brackets.
'Advance one character to ignore the symbol.
If IsSeparator(FirstChar) Then GoTo SkipHere
'If not, keep checking if word has attached another symbol,
'comma, period, etc., and remove it if necessary.
LastChar = Mid$(InputPhrase, (NewSpace), 1)
If IsSeparator(LastChar) Then
Word = Mid$(InputPhrase, LastSpace + 1, (NewSpace - LastSpace) - 1)
Else
Word = Mid$(InputPhrase, LastSpace + 1, (NewSpace - LastSpace))
End If
'Generate OR string
If OutputPhrase <> "" Then
OutputPhrase = OutputPhrase & " OR [" & Field2Use & "] Like '*" & Word & "*'"
Else
OutputPhrase = "[" & Field2Use & "] Like '*" & Word & "*'"
End If
End If
SkipHere:
Next n
MakeSQL = OutputPhrase
End Function
MakeSQL("Red/Green/Blue", "Color") returns
[Color] Like '*Red*' OR [Color] Like '*Green*' OR [Color] Like '*Blue*'
I used this function with a text box in a form to find some records based on the words an analyst remebered the record contained. Then I added this to the WHERE part of the main SQL string.
I have a twin function named MakeNOTSQL() that only changes this part:
Code:
'Generate AND NOT string
If OutputPhrase <> "" Then
OutputPhrase = OutputPhrase & " AND [" & Field2Use & "] Not Like '*" & Word & "*'"
Else
OutputPhrase = "[" & Field2Use & "] Not Like '*" & Word & "*'"
End If
End If
It's been 12 years but here it is! Hope it's useful for anyone.
To boldly code, where no programmer has compiled before!