Solved

Why a search form isn't working

Posted on 2015-01-22
11
123 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
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
 

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 119

Assisted Solution

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

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:SteveL13
ID: 40566817
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
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 119

Expert Comment

by:Rey Obrero
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
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…

867 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

19 Experts available now in Live!

Get 1:1 Help Now