Not a programmer by any means but I got this far and it works for the most part. I hope I'm not asking for too much but I've gone about as far as I can go on my own. Any help would be greatly apreciated.
1. I want to flash an error message in case the user fails to enter search criteria and am getting syntax error in where clause.
2. Would like to have boolean search.
example: type "bogus" and retrieve "bogus company inc."
any tutorials or help would be nice.
3. Would like to auto clear the search form after a search is performed.
Code:
Private Sub Search_Click()
On Error GoTo Err_search_button_Click
Dim sSQL As String, stLen As Integer, stCaption As String
sSQL = "SELECT Customers.* FROM Customers WHERE"
stCaption = "Search Results. "
If Not IsNull(Me.txtCustomerID) Then
' Search By Customer I.D.
sSQL = sSQL & " (((Customers.CustomerID)=[Forms]![frmSearch]![txtCustomerID])) AND"
stCaption = stCaption & "Customer I.D. = " & Me.txtCustomerID & " "
End If
If Not IsNull(Me.txtCompany) Then
' Search By Company Name
sSQL = sSQL & " (((Customers.CompanyName)=[Forms]![frmSearch]![txtCompany])) AND"
stCaption = stCaption & "Company Name = " & Me.txtCompany & " "
End If
If Not IsNull(Me.txtFirstName) Then
' Search By Contact First Name
sSQL = sSQL & " (((Customers.ContactFirstName)=[Forms]![frmSearch]![txtFirstName])) AND"
stCaption = stCaption & "Contact First Name = " & Me.txtFirstName & " "
End If
If Not IsNull(Me.txtLastName) Then
' Search By Contact Last Name
sSQL = sSQL & " (((Customers.ContactLastName)=[Forms]![frmSearch]![txtLastName])) AND"
stCaption = stCaption & "Contact Last Name = " & Me.txtLastName & " "
End If
If Not IsNull(Me.txtCity) Then
' Search By City
sSQL = sSQL & " (((Customers.City)=[Forms]![frmSearch]![txtCity])) AND"
stCaption = stCaption & "City = " & Me.txtCity & " "
End If
If Not IsNull(Me.txtState) Then
' Search By State
sSQL = sSQL & " (((Customers.StateOrProvince)=[Forms]![frmSearch]![txtState])) AND"
stCaption = stCaption & "State = " & Me.txtState & " "
End If
If Not IsNull(Me.txtZip) Then
' Search By Zip
sSQL = sSQL & " (((Customers.PostalCode)=[Forms]![frmSearch]![txtZip])) AND"
stCaption = stCaption & "Zip = " & Me.txtZip & " "
End If
' The following "IF...then" statement detects for "AND" at the end of the SQL statements
' and removes it if it exists.
If Right(sSQL, 3) = "AND" Then
stLen = Len(sSQL)
sSQL = Left(sSQL, stLen - 4)
End If
' Prompt user if they clicked SEARCH but didn't enter search criteria
If sSQL = sSQLOriginal Then
MsgBox "You need to enter search criteria", vbCritical, "Search Failed"
End
End If
DoCmd.OpenForm "Customers"
Forms![Customers].Form.RecordSource = sSQL
Forms![Customers].Form.Caption = stCaption
Exit_search_button_Click:
Exit Sub
Err_search_button_Click:
' This routine handles any errors with a custom message
MsgBox Err.Description, vbCritical, "Report this Error to the database designer"
End Sub
1. I want to flash an error message in case the user fails to enter search criteria and am getting syntax error in where clause.
2. Would like to have boolean search.
example: type "bogus" and retrieve "bogus company inc."
any tutorials or help would be nice.
3. Would like to auto clear the search form after a search is performed.
Code:
Private Sub Search_Click()
On Error GoTo Err_search_button_Click
Dim sSQL As String, stLen As Integer, stCaption As String
sSQL = "SELECT Customers.* FROM Customers WHERE"
stCaption = "Search Results. "
If Not IsNull(Me.txtCustomerID) Then
' Search By Customer I.D.
sSQL = sSQL & " (((Customers.CustomerID)=[Forms]![frmSearch]![txtCustomerID])) AND"
stCaption = stCaption & "Customer I.D. = " & Me.txtCustomerID & " "
End If
If Not IsNull(Me.txtCompany) Then
' Search By Company Name
sSQL = sSQL & " (((Customers.CompanyName)=[Forms]![frmSearch]![txtCompany])) AND"
stCaption = stCaption & "Company Name = " & Me.txtCompany & " "
End If
If Not IsNull(Me.txtFirstName) Then
' Search By Contact First Name
sSQL = sSQL & " (((Customers.ContactFirstName)=[Forms]![frmSearch]![txtFirstName])) AND"
stCaption = stCaption & "Contact First Name = " & Me.txtFirstName & " "
End If
If Not IsNull(Me.txtLastName) Then
' Search By Contact Last Name
sSQL = sSQL & " (((Customers.ContactLastName)=[Forms]![frmSearch]![txtLastName])) AND"
stCaption = stCaption & "Contact Last Name = " & Me.txtLastName & " "
End If
If Not IsNull(Me.txtCity) Then
' Search By City
sSQL = sSQL & " (((Customers.City)=[Forms]![frmSearch]![txtCity])) AND"
stCaption = stCaption & "City = " & Me.txtCity & " "
End If
If Not IsNull(Me.txtState) Then
' Search By State
sSQL = sSQL & " (((Customers.StateOrProvince)=[Forms]![frmSearch]![txtState])) AND"
stCaption = stCaption & "State = " & Me.txtState & " "
End If
If Not IsNull(Me.txtZip) Then
' Search By Zip
sSQL = sSQL & " (((Customers.PostalCode)=[Forms]![frmSearch]![txtZip])) AND"
stCaption = stCaption & "Zip = " & Me.txtZip & " "
End If
' The following "IF...then" statement detects for "AND" at the end of the SQL statements
' and removes it if it exists.
If Right(sSQL, 3) = "AND" Then
stLen = Len(sSQL)
sSQL = Left(sSQL, stLen - 4)
End If
' Prompt user if they clicked SEARCH but didn't enter search criteria
If sSQL = sSQLOriginal Then
MsgBox "You need to enter search criteria", vbCritical, "Search Failed"
End
End If
DoCmd.OpenForm "Customers"
Forms![Customers].Form.RecordSource = sSQL
Forms![Customers].Form.Caption = stCaption
Exit_search_button_Click:
Exit Sub
Err_search_button_Click:
' This routine handles any errors with a custom message
MsgBox Err.Description, vbCritical, "Report this Error to the database designer"
End Sub