Solved

Why a search form isn't working

Posted on 2015-01-22
11
120 Views
Last Modified: 2015-01-23
I have an unbound form that has 5 fields on it.  The purpose of the for is to allow the user to enter a string of characters in any of the fields and have a query then feed the information to a 2nd form that uses the query as its record source.  I though it was working yesterday but it isn't.  I have copy/pasted the SQL of the query designer below.  

The problem is that if for example, a record doesn't have anything in the "Company" field, the query won't return records if the user types "John" in the fist name field.

Here is the SQL:

SELECT Contacts.Lname, Contacts.Fname, Contacts.Company, Contacts.Phone1, Contacts.Email, Contacts.IDEE, Contacts.Date, Contacts.MI, Contacts.CType, Contacts.Address, Contacts.Suite, Contacts.City, Contacts.State, Contacts.Zip, Contacts.Phone2, Contacts.HomePhone, Contacts.Fax, Contacts.MailDate, Contacts.MailItem, Contacts.Comments, Contacts.AccountRep, Contacts.LastModUser, Contacts.LastModDate, Contacts.EmailList, Contacts.MailingLabel, Contacts.FaxList, Contacts.[BAWB Member], Contacts.[BAWB Membership Date], Contacts.[BAWB Expiration Date], Contacts.[BAWB Member Since]
FROM Contacts
WHERE (((Contacts.Lname) Like "*" & [Forms]![frmSearchContacts]![txtLastName] & "*") AND ((Contacts.Fname) Like "*" & [Forms]![frmSearchContacts]![txtFirstName] & "*") AND ((Contacts.Company) Like "*" & [Forms]![frmSearchContacts]![txtCompany] & "*") AND ((Contacts.Phone1) Like "*" & [Forms]![frmSearchContacts]![txtPhone1] & "*") AND ((Contacts.Email) Like "*" & [Forms]![frmSearchContacts]![txtEmail] & "*"))
ORDER BY Contacts.Lname, Contacts.Fname, Contacts.Company;
0
Comment
Question by:SteveL13
  • 5
  • 4
  • 2
11 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
try this

SELECT Contacts.Lname, Contacts.Fname, Contacts.Company, Contacts.Phone1, Contacts.Email, Contacts.IDEE, Contacts.Date, Contacts.MI, Contacts.CType, Contacts.Address, Contacts.Suite, Contacts.City, Contacts.State, Contacts.Zip, Contacts.Phone2, Contacts.HomePhone, Contacts.Fax, Contacts.MailDate, Contacts.MailItem, Contacts.Comments, Contacts.AccountRep, Contacts.LastModUser, Contacts.LastModDate, Contacts.EmailList, Contacts.MailingLabel, Contacts.FaxList, Contacts.[BAWB Member], Contacts.[BAWB Membership Date], Contacts.[BAWB Expiration Date], Contacts.[BAWB Member Since]
 FROM Contacts
 WHERE (((Contacts.Lname) Like "*" & [Forms]![frmSearchContacts]![txtLastName] & "*") AND ((Contacts.Fname) Like "*" & [Forms]![frmSearchContacts]![txtFirstName] & "*") AND ((Contacts.Company) Like "*" & [Forms]![frmSearchContacts]![txtCompany] & "*" OR (Contacts.Company) Is Null) AND ((Contacts.Phone1) Like "*" & [Forms]![frmSearchContacts]![txtPhone1] & "*") AND ((Contacts.Email) Like "*" & [Forms]![frmSearchContacts]![txtEmail] & "*"))
 ORDER BY Contacts.Lname, Contacts.Fname, Contacts.Company;
0
 

Author Comment

by:SteveL13
Comment Utility
Didn't work.  Please see attached test file.

On the form enter "doe" in the last name field.  The query will return a record as it should.

Then enter "meade" in the last name field.  The query will not return a record even though there is a record with "meade" in the Lname field.

????????
Test.accdb
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
Comment Utility
If I add a value to the Phone1 field for the Meade row, the query works.

IMO, you're creating a search form incorrectly. Instead of creating a query that references the form, instead build up the full SQL of the query, then show the results on a Datasheet form, or something of that nature.

So the "Run" button would do something like this:

Dim s As String

If Nz(Me.txtLastName, "") <> "" Then
  s = s & " LName LIKE *" & Me.txtLastName & "*"
End If

If Nz(Me.txtFirstName, "") <> "" Then
  If Len(s) > 0 Then
    s = s & " AND "
  End If
  s = s & " FName Like *" Me.txtFirstName & "*"
End If

<and so on for other controls>

s = "SELECT * FROM Contacts WHERE " & s

Me.DataSheetSubFormControlName.Form.Recorsource = s
0
 

Author Comment

by:SteveL13
Comment Utility
I'm getting a syntax error using: (see file attached)syntax error
Dim s As String

 If Nz(Me.txtLastName, "") <> "" Then
   s = s & " LName Like *" & Me.txtLastName & "*"
 End If
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
Comment Utility
If Nz(Me.txtLastName, "") <> "" Then
   s = s & " LName Like '*" & Me.txtLastName & "*'"
 End If
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

Author Comment

by:SteveL13
Comment Utility
That worked.   Now if I could just get the form to order by Lname
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
Comment Utility
s = "SELECT * FROM Contacts WHERE " & s & " Order by LName"
0
 

Author Comment

by:SteveL13
Comment Utility
Almost.  But if I don't put anything in the search form fields, which would return all records, I get another syntax error:

"Syntax Error in WHERE clause"
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
post the codes you are using.
0
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
Comment Utility
Do this:


If Len(s) > 0 Then
  s = "SELECT * FROM Contacts WHERE " & s
Else
  s = "SELECT * FROM Contacts"
End IF
0
 

Author Comment

by:SteveL13
Comment Utility
Thanks to both of you I was able to put this together and it works great!

Dim s As String

    If Nz(Me.txtLastName, "") <> "" Then
        's = s & " LName Like *" & Me.txtLastName & "*"
        s = s & " LName Like '*" & Me.txtLastName & "*'"
    End If

    If Nz(Me.txtFirstName, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " FName Like '*" & Me.txtFirstName & "*'"
    End If

    If Nz(Me.txtCompany, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " Company Like '*" & Me.txtCompany & "*'"
    End If

    If Nz(Me.txtEmail, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " Email Like '*" & Me.txtEmail & "*'"
    End If

    If Nz(Me.txtPhone1, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " Phone1 Like '*" & Me.txtPhone1 & "*'"
    End If
    
    If Len(s) > 0 Then
        s = "SELECT * FROM Contacts WHERE " & s & " Order by LName"
    Else
        s = "SELECT * FROM Contacts" & " Order by LName"
    End If
    

    DoCmd.OpenForm "frmContactsExisting", acNormal, , , acFormEdit, acWindowNormal

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now