knuckles
03-17-2000, 07:22 AM
I have a library database with a search form that I'm working on that will allow users to search for multiple terms in a certain field (example: the user will be able to type in "marijuana OR pot" in the "Title" box of the search form and all records that have titles with either word in them will be returned).
This is turning out to be a LOT harder than I thought. However, I think I'm getting closer. My problem now is trying to link together the ideas I've collected into something that actually works.
As an example of what I've got that ISN'T working, here is the code that is supposed to process the Author information entered by the user in the search form. I'm trying to get it to take what was entered in txtAuthor on the search form and then see if there are any "and" or "or" restrictors in that entered phrase. If there are, I want the words between them separated out and the entire search string rephrased to go from this:
s.Author Like "Smith and Jones"
to this:
s.Author Like "Smith" and s.Author Like "Jones"
But my code isn't working. Does anything obvious stand out in this section?
If Not IsNull(txtAuthor) Then
answer = ""
Do
a = InStr(SearchStr, " and ")
o = InStr(SearchStr, " or ")
If a = 0 And o = 0 Then 'no ands or ors so 'just go on as usual
strWHERE = strWHERE & " AND s.Author Like '* " & txtAuthor & " *'"
Exit Function
End If
If a = 0 Or (o <> 0 And o < a) Then 'looking
'for ORs
answer = answer & "s.Author" & "=" & Chr$(34) & Left$(txtAuthor, o - 2) & Chr$(34) & " OR "
txtAuthor = Mid$(txtAuthor, o + 3)
Else 'look for ANDs
answer = answer & "s.Author" & "=" & Chr$(34) & Left$(txtAuthor, a - 2) & Chr$(34) & " AND "
txtAuthor = Mid$(txtAuthor, a + 4)
End If
Loop
strWHERE = strWHERE & " AND " & answer
End If
The ultimate goal is to come up with a long strWHERE statement that has the parameters from the Author, Title, and Source sections all linked together with ANDs (if the user has entered terms into all three fields).
The original function, which I am now doctoring up with the AND and OR stuff, looked like this:
Function BuildSQLString(strSQL As String) As Boolean
Dim strSELECT As String
Dim strFROM As String
Dim strWHERE As String
Dim strINTO As String
strSELECT = "s.* "
strFROM = "tblArticles s "
If Not IsNull(txtAuthor) Then
strWHERE = strWHERE & " AND s.Author Like '* " & txtAuthor & " *'"
End If
If Not IsNull(txtTitle) Then
strWHERE = strWHERE & " AND s.Title Like '* " & txtTitle & " *'"
End If
If Not IsNull(txtSource) Then
strWHERE = strWHERE & " AND s.Source Like '*" & txtSource & "*'"
End If
If Not IsNull(txtYear) Then
strWHERE = strWHERE & " AND s.Year " & cboYear & "'" & txtYear & "'"
End If
If Not IsNull(txtYear2) Then
strWHERE = strWHERE & " and " & "'" & txtYear2 & "'"
End If
strSQL = "SELECT " & strSELECT
If strWHERE <> "" Then strSQL = strSQL & "WHERE " & Mid$(strWHERE, 6)
BuildSQLString = True
End Function
Do I need to start over from scratch or can I alter this function the way I was trying to in the first example above?
This project has been making me crazy for about 8 months now. I need resolution or I'm going to lose it!
Thanks!
Meg Wood
knuckles@drizzle.com
This is turning out to be a LOT harder than I thought. However, I think I'm getting closer. My problem now is trying to link together the ideas I've collected into something that actually works.
As an example of what I've got that ISN'T working, here is the code that is supposed to process the Author information entered by the user in the search form. I'm trying to get it to take what was entered in txtAuthor on the search form and then see if there are any "and" or "or" restrictors in that entered phrase. If there are, I want the words between them separated out and the entire search string rephrased to go from this:
s.Author Like "Smith and Jones"
to this:
s.Author Like "Smith" and s.Author Like "Jones"
But my code isn't working. Does anything obvious stand out in this section?
If Not IsNull(txtAuthor) Then
answer = ""
Do
a = InStr(SearchStr, " and ")
o = InStr(SearchStr, " or ")
If a = 0 And o = 0 Then 'no ands or ors so 'just go on as usual
strWHERE = strWHERE & " AND s.Author Like '* " & txtAuthor & " *'"
Exit Function
End If
If a = 0 Or (o <> 0 And o < a) Then 'looking
'for ORs
answer = answer & "s.Author" & "=" & Chr$(34) & Left$(txtAuthor, o - 2) & Chr$(34) & " OR "
txtAuthor = Mid$(txtAuthor, o + 3)
Else 'look for ANDs
answer = answer & "s.Author" & "=" & Chr$(34) & Left$(txtAuthor, a - 2) & Chr$(34) & " AND "
txtAuthor = Mid$(txtAuthor, a + 4)
End If
Loop
strWHERE = strWHERE & " AND " & answer
End If
The ultimate goal is to come up with a long strWHERE statement that has the parameters from the Author, Title, and Source sections all linked together with ANDs (if the user has entered terms into all three fields).
The original function, which I am now doctoring up with the AND and OR stuff, looked like this:
Function BuildSQLString(strSQL As String) As Boolean
Dim strSELECT As String
Dim strFROM As String
Dim strWHERE As String
Dim strINTO As String
strSELECT = "s.* "
strFROM = "tblArticles s "
If Not IsNull(txtAuthor) Then
strWHERE = strWHERE & " AND s.Author Like '* " & txtAuthor & " *'"
End If
If Not IsNull(txtTitle) Then
strWHERE = strWHERE & " AND s.Title Like '* " & txtTitle & " *'"
End If
If Not IsNull(txtSource) Then
strWHERE = strWHERE & " AND s.Source Like '*" & txtSource & "*'"
End If
If Not IsNull(txtYear) Then
strWHERE = strWHERE & " AND s.Year " & cboYear & "'" & txtYear & "'"
End If
If Not IsNull(txtYear2) Then
strWHERE = strWHERE & " and " & "'" & txtYear2 & "'"
End If
strSQL = "SELECT " & strSELECT
If strWHERE <> "" Then strSQL = strSQL & "WHERE " & Mid$(strWHERE, 6)
BuildSQLString = True
End Function
Do I need to start over from scratch or can I alter this function the way I was trying to in the first example above?
This project has been making me crazy for about 8 months now. I need resolution or I'm going to lose it!
Thanks!
Meg Wood
knuckles@drizzle.com