Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why a search form isn't working

Posted on 2015-01-22
11
Medium Priority
?
143 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 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1000 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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 1000 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 1000 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 85

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

704 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