I'm trying to set up a search form with 4 txtboxes for search criteria, each text value passed needs to be compaired 5 fields of my database. If I use only one field for the search everything works OK, but as soon as I add 'OR' to search multiple fields it will return ANY records with ANYTHING in the searched fields. Here's the code that I'm using right now:
Thanks in advance for any help offered
.
Code:
Option Compare Database
Option Explicit
Const strSELECT As String = "SELECT * "
Const strFROM As String = "FROM tblCompany_Information "
Private Sub findit_Click()
Call DoDispatchSearch
Me.OrderBy = "carrier_name"
End Sub
Sub DoDispatchSearch()
Dim ok As Boolean, sql As String
ok = False
sql = " WHERE "
If Not IsNull(Me.txtSrchSCity) Then
If sql <> " WHERE " Then
sql = sql & " And "
End If
ok = True
sql = sql & "[po_c1] OR [po_c2] OR [po_c3] OR [po_c4] OR [po_c5] LIKE '*" & Me.txtSrchSCity & "*'"
End If
If Not IsNull(Me.txtSrchSState) Then
If sql <> " WHERE " Then
sql = sql & " And "
End If
ok = True
sql = sql & "[po_s1] OR [po_s2] OR [po_s3] OR [po_s4] OR [po_s5] '" & Me.txtSrchSState & "'"
End If
If Not IsNull(Me.txtSrchCcity) Then
If sql <> " WHERE " Then
sql = sql & " And "
End If
ok = True
sql = sql & "[pd_c1] OR [pd_c2] OR [pd_c3] OR [pd_c4] OR [pd_c5] LIKE '*" & Me.txtSrchCcity & "*'"
End If
If Not IsNull(Me.txtSrchCState) Then
If sql <> " WHERE " Then
sql = sql & " And "
End If
ok = True
sql = sql & "[pd_s1] OR [pd_s2] OR [pd_s3] OR [pd_s4] OR [pd_s5] LIKE '" & Me.txtSrchCState & "'"
End If
If ok Then
MsgBox "The SQL String: " & sql & ""
Me.RecordSource = strSELECT & strFROM & sql
Me.Requery
Me.Detail.Visible = True
Me.FormFooter.Visible = True
DoCmd.MoveSize , , 8500, 6000
End If
fitexit:
Exit Sub
fiterr:
MsgBox Error$
Resume fitexit
End Sub
Thanks in advance for any help offered