Results 1 to 9 of 9
  1. #1
    ajolson1964 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    10

    How do I send multiple values to a query?

    I have a form that has a text box where I want the user to be able to put in multiple values and pass that to a query. The query parameter is set to [Forms]![Ad_Hoc_Report_frm].[TxtEnter_PID_Filter] When I put in more than one value (int values) I get an error.



    Example

    If I put in one value 1234 it returns record 1234 as expected.

    If I put in 1234,2934, or any thing like that in (1234,2934) I get this error message: The expression is typed incorrectly, or is too complicated to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning part of the expressions to a variable.
    I am new to access not sure how to assign to a variable.
    Any help would be greatly appreciated.
    Thanks

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    That is because you are including a comma within your expression. It seems you are dealing with number data types in your SQL and as soon as you type the comma, SQL interprets this differently.

    What you are creating, when the user is typing multiple numbers separated by a comma, is a string array. I am not aware of an Array function in Access. I think there is one function that you can use to parse the elements. I have used it before but I would have to search for it.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    The BEST way is to have a list. The user picks items, or enters them in a table. This table is joined to your query table.
    It pulls the only those in the list.
    just like saying where [code] = 65,or 76 or 32 or 124

  4. #4
    ajolson1964 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    10
    Thanks what is the setting for them to select more than one item on the list?

  5. #5
    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,870
    If you insert into a textbox tbxExample 81,32, 45,67
    You can use a Split function to separate the values, then you can use those individual values
    as you wish.

    Here's a small example that may help

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : tbxExample_AfterUpdate
    ' Author    : mellon
    ' Date      : 18/05/2015
    ' Purpose   : Sample form with textbox to show use of split() to
    '             get individual values from a string of "csv" values.
    '             Once you have individual values you can use them/manipulate them as required.
    '
    'Revised: To show use of specific value in the textbox and also
    '         how to get individual values between the commas.
    '---------------------------------------------------------------------------------------
    '
    Private Sub tbxExample_AfterUpdate()
              Dim x As Variant
              Dim i As Integer
              Dim t1 As String
              Dim t2 As String
    10        t1 = "This is a mockedup SQL -"
    20        t2 = "SELECT * FROM MyTable WHERE someIntfield In("
    30        On Error GoTo tbxExample_AfterUpdate_Error
              
          'use the string value in the textbox
    40        Debug.Print "Sample using the text box string " & vbCrLf & vbCrLf & t1 & t2 & Me.tbxExample & ")" & vbCrLf & vbCrLf _
              & "OR  individual values beteeen commas " & vbCrLf
              
                       'OR
          'separate the individual integers for individual processing/manpulation
    
    50        If InStr(Me.tbxExample, ",") > 0 Then    'there is at least a comma in the txtbox
    60            x = Split(Me.tbxExample, ",")
    70            For i = LBound(x) To UBound(x)
    80                Debug.Print i; x(i) & "  " & x(i) ^ 3 & " is " & x(i) & " cubed"
    90            Next i
    100       Else
    110           MsgBox "no values in textbox", vbOKOnly
    120       End If
    130      On Error GoTo 0
    140      Exit Sub
    
    tbxExample_AfterUpdate_Error:
    
    150       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure tbxExample_AfterUpdate of VBA Document Form_FormSampleTextBoxWithCSV"
    
    End Sub
    Output:
    Sample using the text box string

    This is a mockedup SQL -SELECT * FROM MyTable WHERE someIntfield In(81,32,45,67)

    OR individual values between commas

    0 81 531441 is 81 cubed
    1 32 32768 is 32 cubed
    2 45 91125 is 45 cubed
    3 67 300763 is 67 cubed

    NOTE:
    This was revised based on June7's comment and to show processing the individual values between the commas.
    Last edited by orange; 05-18-2015 at 02:39 PM.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    AFAIK, parsing the CSV string to an array won't help with a dynamic parameterized query. The query filter criteria needs the IN operator in conjunction with CSVs and this won't work with a dynamic parameter. However, can use the CSV string to programmatically set the filter property of form or report.
    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.

  7. #7
    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,870
    I was just showing an example of how to enter comma separated values into a textbox.
    Process them, and adjust some recordsource sql with an IN clause, and separating out each integer value.

    Select fields from MyTable where YourIntField In ( a,b,c..)

    If the Op has users entering 2,4,5,6,7 in a textbox and wants to select certain records, I think this example can do it.

    Note: The code and post #5 have been revised to clarify
    a) using the entire string of comma separated numbers en bloc to create an IN(...) clause, and
    b) separating and using individual values (integers) in subsequent process(es).

    Thanks June for the comment.

    Note: If these were text values between commas, you would have to enter "value1","value2"....
    or programmatically process the textbox string to make individual strings for an IN clause.

    The essence of my responses was to show how you could have a comma separated list in the text box and process it. Some programming behind the scenes is required.
    Last edited by orange; 05-18-2015 at 02:48 PM. Reason: clarification

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I just tested code to set form Filter property without first parsing string through an array. It works.

    I NEVER use dynamic parameterized queries.
    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
    ajolson1964 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    10
    Thanks all. I took a simpler approach. Built a table where the user can paste their values in then runs a inner join query. Works like a charm, And part of the requirement was to allow the user to enter up to 5000 records at a time. So this solved all problems and potential problems I might have had. Thanks for the help.

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

Similar Threads

  1. Send email to multiple recipients based on query
    By nablmm in forum Programming
    Replies: 3
    Last Post: 09-11-2014, 05:36 PM
  2. Cross Tab Query with multiple Values
    By chemicalbrother in forum Queries
    Replies: 1
    Last Post: 06-03-2014, 06:06 AM
  3. Loop Through Query, Send Multiple Emails
    By tdoolittle in forum Reports
    Replies: 6
    Last Post: 05-12-2014, 09:33 PM
  4. Multiple Query Values In A Report
    By dr4ke in forum Reports
    Replies: 10
    Last Post: 01-29-2013, 07:32 AM
  5. Send multiple e-mails through Outlook based on query
    By dataphile in forum Programming
    Replies: 3
    Last Post: 12-30-2009, 12:04 AM

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