Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116

    Optimize a select query

    Hi,
    I have a search button and when the button is clicked, a query is executed. This is how the query looks like:



    SELECT *
    FROM Contacts AS t
    WHERE (t.ContactName Like IIf(IsNull(FORMS!F_Products_Search!Text34),"*",(FO RMS!F_Products_Search!Text34)) Or (IIf(IsNull(FORMS!F_Products_Search!Text34),t.Prod uctName is null))) And
    (t.FPItem Like IIf(IsNull(FORMS!F_Products_Search!Text48),"*",(FO RMS!F_Products_Search!Text48)) Or (IIf(IsNull(FORMS!F_Products_Search!Text48),t.FPIt em is null)))...


    The problem with this query is that, it doesn't search for partial names. As an example, if I need to search for "Mathew", but I type it only "Mat", nothing is retreived. I need to explicitly state Mat* to search for partial names.

    Also, even if i specify "thew" and hit search, it should bring up mathew.

    Any suggestions???

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,954
    This tutorial shows how to set up a search form and how to set the parameters of a query based on the form. http://www.datapigtechnologies.com/f...earchform.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116
    Hi,
    Thank you for the tutorial. It worked! I don't have to explicitly type * anymore. But there are still some issues with it .

    All the fields need to have some data in it for the search to retrieve a particular column. For example, I have a field called "Middle name" and if the entry is blank then that column is not retrieved in the search.

    Also, the query in the tutorial does not search for patterns in a word. Like, if I would like to search for "microsoft" and I type in "soft", I would like it to retrieve "microsoft". For now, it only retrieves "microsoft", if I type in the first few words.

    I am not the world's greatest explainer. Not sure if I am making myself clear here, but yeah..there you go.

    I apologize for all the questions. I am a brand new access user and all these problems are very frustrating.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,738
    Please post the query sql.

  5. #5
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116
    SELECT Contacts.ContactName, Contacts.ItemNo, Contacts.NDC
    FROM Contacts
    WHERE (((Contacts.ContactName) Like [Forms]![F_Contacts_Search].[txtContactName] & "*") AND ((Contacts.ItemNo) Like [Forms]![F_Contacts_Search].[txtItemNo] & "*") AND ((Contacts.NDC) Like [Forms]![F_Contacts_Search].[txtNDC] & "*");

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,738
    When you have a Like as follows:
    suppose Somefield has value "QUE"
    Like Somefield & "*" you are limiting responses to "QUE "+ zero or more characters

    You could find Question questions query etc, but
    you would not find requestion request requery since the first few characters do not match "Somefield" [que]

    To find these fields you need to adjust your Like to this format (using the same example)

    Like "*" & Somefield & "*"

    Now you will find all of the examples

    Question questions query requestion request requery
    because you are requesting any string that contains "somefield" (que) anywhere in the string

    Good luck.

  7. #7
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116
    @orange: thanks a ton! It worked!

    Now what do I do about the blank fields? My query is executed only when all the fields for a column have values in them

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,954
    You want to return record even if no value? Assuming you don't permit empty strings in fields, add Or Is Null to the criteria for each field.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116
    @June7:

    Imagine I have 3 fields called FirstName, MiddleName and LastName. For the FirstName and LastName columns I have entered the names "John" and "Kennedy" respectively. I have left the MiddleName field blank. Now, in the search criteria if I enter "John" in the FirstName textbox and hit enter, "John Kennedy" is not retrieved because MiddleName field is blank.

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,738
    I fyou are using the base query you showed earlier

    Where A like *"s1"* and B like *"s2"* and C like *"s3"*, then perhaps you should use

    as june1 said

    Where (A like *"s1"* or A is null) and (B like *"s2"* or b Is Null) and (C like *"s3"* or C is null)

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,954
    Did you try the Or Is Null? Seems that was the solution for another poster I helped. I actually don't have any search forms set up like this. I use VBA code to build a filter string with specific values selected from comboboxes, no Like operators involved, just haven't encountered the need in my projects.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116
    maybe what I have done is totally wrong, but based on your inputs this is the query I came up with:

    SELECT Contacts.ContactName, Contacts.ItemNo, Contacts.NDC
    FROM Contacts
    WHERE (((Contacts.ContactName) Like (([Contacts].[ContactName])="*" & [Forms]![F_Contacts_Search].[txtContactName] & "*" Or [Forms]![F_Contacts_Search].[txtContactName] Is Null)) AND ((Contacts.ItemNo) Like (([Contacts].[ItemNo])="*" & [Forms]![F_Contacts_Search].[txtItemNo] & "*" Or [Forms]![F_Contacts_Search].[txtItemNo] Is Null)) AND ((Contacts.NDC) Like (([Contacts].[NDC])="*" & [Forms]![F_Contacts_Search].[txtNDC] & "*" Or [Forms]![F_Contacts_Search].[txtNDC] Is Null)));


    Something must be wrong in the above query. Cos now I get no results when I query this!

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,738
    Quote Originally Posted by accessnewb View Post
    maybe what I have done is totally wrong, but based on your inputs this is the query I came up with:

    SELECT Contacts.ContactName, Contacts.ItemNo, Contacts.NDC
    FROM Contacts
    WHERE (((Contacts.ContactName) Like (([Contacts].[ContactName])="*" & [Forms]![F_Contacts_Search].[txtContactName] & "*" Or [Forms]![F_Contacts_Search].[txtContactName] Is Null)) AND ((Contacts.ItemNo) Like (([Contacts].[ItemNo])="*" & [Forms]![F_Contacts_Search].[txtItemNo] & "*" Or [Forms]![F_Contacts_Search].[txtItemNo] Is Null)) AND ((Contacts.NDC) Like (([Contacts].[NDC])="*" & [Forms]![F_Contacts_Search].[txtNDC] & "*" Or [Forms]![F_Contacts_Search].[txtNDC] Is Null)));


    Something must be wrong in the above query. Cos now I get no results when I query this!

    The problem is the =. It isn't equal , it's LIKE

    I have adjusted your Where, but please check the bracketing,

    WHERE ((Contacts.ContactName) Like "*" & [Forms]![F_Contacts_Search].[txtContactName] & "*" Or ([Forms]![F_Contacts_Search].[txtContactName] Is Null)) AND
    ((Contacts.ItemNo) Like "*" &
    Forms]![F_Contacts_Search].[txtItemNo] & "*" Or ([Forms]![F_Contacts_Search].[txtItemNo] Is Null)) AND
    ((Contacts.NDC) Like "*" & [Forms]![F_Contacts_Search].[txtNDC] & "*" Or [Forms]![F_Contacts_Search].[txtNDC] Is Null));

  14. #14
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116
    yayy! it works!! thanks :-) phew!

  15. #15
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116
    alright! now I have only tiny last question. i already asked this in the other forum but I think I didn't make myself clear. The query works super now, and for the onClick event of the search button, this is the code I have written:

    Private Sub searchTable_Click()
    DoCmd.OpenQuery "Q_Contacts", acViewNormal

    End Sub

    The problem is I have to close the results window every time to search for new criteria. For example, I type "John" under first name and a results table opens with 5 results. I have to close the results table if I need to search for "Mathew", else the results table with "John" is shown.


    This technically doesn't fall under the "query" category. Should I delete it here and re-post in the appropriate forum??

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Select Query > Update Query > Table Field
    By tuggleport in forum Queries
    Replies: 2
    Last Post: 08-09-2012, 07:04 AM
  2. Replies: 2
    Last Post: 03-23-2012, 09:20 AM
  3. Replies: 1
    Last Post: 04-14-2011, 07:19 AM
  4. Replies: 2
    Last Post: 01-31-2011, 08:31 AM
  5. select sum query
    By sparkles in forum Queries
    Replies: 1
    Last Post: 10-22-2010, 07:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums