benb wrote:
I have form that looks a lot like a search bar for the user to search
for records matching specified criteria (e.g. first names containing
"ben"). For robust results, an intermediary form displays all records
matching the criteria (hmm...sound like a popular web site I know).
Here is the immediate problem: my code (below) works fine on my
computer, but on some of those belonging to my work colleagues (who
will be the end users of the db) it does not seem to work. This is
particularly hard to debug because I have to use other PC's to
duplicate the error. Other controls with similar event code DO work.
The only difference between working controls/event code and nonworking
ones seems to be in the "whencondition" of the DoCmd.OpenForm. Do I
need to add something to the code so that it will work on all PC's?
(Aside: Security is not enable so all users should be "Admin"; the
problem would appear to be related to the PC, not the user.) HELP
PLEASE!
**********code that works************
Private Sub cmdEdit_Click()
DoCmd.OpenForm FormName:="frm_Search", view:=acNormal
Forms![frm_Search].Tag = 1
Forms![frm_Search].txtSubject = ""
End Sub
**********code that doesn't always work**************
Private Sub cmdSearch_Click()
Dim category As String
Dim subject As Variant
subject = Forms!frm_Search!txtSubject.Text
category = Forms!frm_Search!cmbCategory.Text
DoCmd.OpenForm FormName:="frm_Browse", view:=acNormal,
wherecondition:= category & " Like " & Chr$(34) & "*" & subject &
"*" &
Chr$(34)
End Sub
Here's something to try as a first step:
Does it only work when cmbCategory.Value matches the name of a field
contained in the RowSource of the form? Your wherecondition should
probably start with the literal name of the field you are searching on.
Something like:
wherecondition := "Category Like " & Chr(34) & "*" & cmbCategory.Value
& "*" & Chr(34)
or
wherecondition := "Subject Like " & Chr(34) & "*" & txtSubject.Value &
"*" & Chr(34)
or
wherecondition := "(Category Like " & Chr(34) & "*" & cmbCategory.Value
& "*" & Chr(34) & ") AND (" & "Subject Like " & Chr(34) & "*" &
txtSubject.Value & "*" & Chr(34) & ")"
(Note that Like "**" is equivalent to Like "*") Like "*" doesn't get
Null values so I often do something like:
If Not IsNull(cbxCity.Value) Then
strWhere = strWhere & " AND ([City] LIKE '" & cbxCity.Value & "')"
Else
strWhere = strWhere & " AND (([City] LIKE '*') OR ([City] Is Null))"
End If
when building a SQL string if I want a Null in a search textbox to
return all records. I have seen mysterious situations where the SQL
created with "((MyField Like '*') Or (MyField IS NULL))" for multiple
fields failed to return all the records so the most robust approach
when possible is more like:
If Not IsNull(cbxCity.Value) Then
strWhere = strWhere & " AND ([City] LIKE '" & cbxCity.Value & "')"
End If
so that there are no restrictions of any kind when the value in a
search control is Null. You should be able to do something like:
wherecondition := strWhere
For debugging you can put your strWhere in a SQL WHERE clause to see if
it's returning the correct records.
Hope this helps,
James A. Fortune