Solved

Why a search form isn't working

Posted on 2015-01-22
11
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40564667
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
ID: 40564767
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
ID: 40566104
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:SteveL13
ID: 40566726
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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 40566797
If Nz(Me.txtLastName, "") <> "" Then
   s = s & " LName Like '*" & Me.txtLastName & "*'"
 End If
0
 

Author Comment

by:SteveL13
ID: 40566817
That worked.   Now if I could just get the form to order by Lname
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 40566856
s = "SELECT * FROM Contacts WHERE " & s & " Order by LName"
0
 

Author Comment

by:SteveL13
ID: 40566871
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40566935
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
ID: 40566946
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
ID: 40566983
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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