Results 1 to 3 of 3
  1. #1
    AJM2013 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    5

    Trying to query a table using multiple values in a textbox on a form - Not Working


    I have a database in Microsoft access 2010. The database has a table that stores prospective customer records, and a form that is used to input a search criteria(s) via textboxes, which then queries prospective customers table and returns the records that contain the inputted search criteria(s).

    An Example of Textboxes values on the search criteria form:
    Textbox - name: bob
    Textbox - address: Left blank
    Textbox - category: car,boat,truck

    I Have tried creating a query with the following

    field: name
    criteria: like “*” & name & “*”
    field: address
    criteria: like “*” & address & “*”
    field: category
    criteria: like “*” & category & “*”

    SQL code:
    SELECT customerName ,address,category
    FROM prospectiveCustomers
    WHERE customerName LIKE “*” & name & “*” AND address LIKE “*” & address& “*” AND category LIKE “*” & category& “*”;

    That works, but only for one value in a textbox. Once there is more than one value in a textbox (e.g name: bob,smith), the query returns no records.

    I have also tried splitting the values using the comma as a delimmter, then inserting the values into a new table. That is fine until one of the search criteria textboxes has been left blank. So the query I created will run, but returns no records.

    SQL CODE:
    SELECT prospectiveCustomers.name, prospectiveCustomers.address,prospectiveCustomers. category
    FROM prospectiveCustomers, [SearchCriteria-name], [SearchCriteria-address],[SearchCriteria-category]
    WHERE prospectiveCustomers.name Like [SearchCriteria-name].name AND prospectiveCustomers.address LIKE [SearchCriteria-address].address AND prospectiveCustomers.category LIKE [SearchCriteria-address].category;

    I have rattled my brain, anyone got any ideas? Hopefully I have explained clearly. :-(

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    Is it possible for records to not have data in any of those fields? Need to handle the nulls.

    If you want to use parameterized query, here is example but it doesn't deal with nulls: http://datapigtechnologies.com/flash...tomfilter.html

    Two ways to deal with the nulls

    1. ([name] Like Forms![SearchCriteria-name].[name] & "*" Or [name] Is Null) AND (address LIKE Forms![SearchCriteria-address].address & "*" Or address Is Null) AND (category LIKE Forms![SearchCriteria-address].category & "*" Or category Is Null)

    2. expressions in query to convert null to string and apply wildcard filter criteria under those constructed fields
    Nz([name],"")
    or
    Nz([name],"none")
    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
    AJM2013 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    5
    Thanks for your reply. Your advice worked . Sometimes you need someone else perspective. I ended up using NZ([name],"*") to insert the wildcard *. So when the search criteria in the text box was left blank (null) on the form, it will insert the * wildcard into the search criteria table, giving me the results I was looking for. I have posted my code below used in access 2010.

    VBA CODE:

    Public Function suppliedSearchCriteria()

    DoCmd.RunSQL ("DELETE * FROM [searchCriteria-name] ")
    Call insertSearchCriteraiaValues(Nz(Me.name.Value, "*"), "NAME", "SearchCriteria-name")

    DoCmd.RunSQL ("DELETE * FROM [searchCriteria-address] ")
    Call insertSearchCriteraiaValues(Nz(Me.address.Value, "*"), "ADDRESS", "SearchCriteria-address")

    DoCmd.RunSQL ("DELETE * FROM [searchCriteria-category] ")
    Call insertSearchCriteraiaValues(Nz(Me.category.Value, "*"), "CATEGORY", "SearchCriteria-category")

    End Function

    Public Function insertSearchCriteraiaValues(searchCriteriaValues As String, columnName As String, tableName As String)

    Dim searchValue As String
    Dim temp() As String

    temp = Split(searchCriteriaValues, ",")
    DoCmd.RunSQL ("DELETE * FROM [" & tableName & "] ")

    For i = LBound(temp) To UBound(temp)
    searchValue = "'" + Trim(temp(i)) + "'"
    DoCmd.RunSQL ("INSERT INTO [" & tableName & "] (" & columnName & ")VALUES (" & searchValue & ")")
    Next i

    End Function

    SQL QUERY CODE:

    SELECT CUSTOMERS.ID, CUSTOMERS.[NAME], CUSTOMERS.ADDRESS, CUSTOMERS.CATEGORY
    FROM CUSTOMERS, [SearchCriteria-name], [SearchCriteria-address], [SearchCriteria-category]
    WHERE (((CUSTOMERS.[NAME]) Like "*" & [SearchCriteria-name]![NAME] & "*") AND ((CUSTOMERS.ADDRESS) Like "*" & [SearchCriteria-address]![ADDRESS] & "*") AND ((CUSTOMERS.CATEGORY) Like "*" & [SearchCriteria-category]![CATEGORY] & "*")
    ORDER BY CUSTOMERS.[NAME];

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Multiple Values in textbox
    By frksdf in forum Forms
    Replies: 10
    Last Post: 07-14-2014, 11:41 AM
  2. Replies: 21
    Last Post: 10-05-2012, 11:36 AM
  3. Replies: 2
    Last Post: 02-04-2012, 01:48 PM
  4. Insert Multiple Checkbox Values to one Textbox
    By dshillington in forum Programming
    Replies: 1
    Last Post: 12-28-2011, 10:10 AM
  5. Not sure why textbox not working for some values
    By jtkjames in forum Programming
    Replies: 1
    Last Post: 07-21-2010, 04:26 AM

Tags for this Thread

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