Results 1 to 9 of 9
  1. #1
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74

    Generate a query of Project ID based on a comma separated list in a text box.

    Hi All,



    I'm trying to generate an OR query using a user inputted comma separated list. I've replaced the comma's with Or statements, but I'm getting an error when I enter more than one value into the textbox. I'm assuming this is because the value is treated as text rather than two numbers separated by an Or.

    Click image for larger version. 

Name:	Form.png 
Views:	10 
Size:	7.9 KB 
ID:	12737

    The first box is the textbox that the user enters Project numbers into (tboxProjects). The second box is the Query (tboxQuery) generated from the user's entry using a simple Replace() call. The second textbox will be hidden once the bugs are ironed out. When the user clicks generate query three things happen.

    1. Check if the box is null, if it's null give a message saying you have to enter at least one value.
    2. If tboxProjects has a value, generate a query: Me.tboxQuery = Replace(Me.tboxProjects.Value, ",", " Or")
    3. Run a report using tboxQuery as the input of project numbers.

    If I run this with only one number, it works as expected. If I have any more than one number it fails with the following error:
    Click image for larger version. 

Name:	Error.png 
Views:	10 
Size:	17.6 KB 
ID:	12738

    I'm assuming that as the numbers are combined with Or, MS Access is converting the entire value to a string. So when the [Forms]![frmProjectID].[Form]![tboxQuery] is replaced with "25 Or 29", Access can't figure out why a string would be used to search on an integer field.

    Any suggestions or work-arounds would be greatly appreciated.

  2. #2
    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,725
    What is the value of tboxquery

    in this
    Me.tboxQuery = Replace(Me.tboxProjects.Value, ",", " Or")
    ?

    I would recommend a space in Replace(Me.tboxProjects.Value, ",", " Or ") --between Or and "

    Where is the code to generate the query?
    Also, be aware that users often make typos.

  3. #3
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74
    Your advice on anticipating user's will make errors is well taken. This is mostly a first crack to see if this technique will work. I fully intend on going back to fix errors my users might make in an iterative process.

    Is white space ignored in VBA? By adding a space after Or in the replace call would it avoid errors for users that try to input something like: 52,53,54 ?

    Here's my code for the button:

    Code:
    Private Sub btnGenQuery_Click()
        If IsNull(Me.tboxProjects) Then
            MsgBox "You must enter at least one project.", vbInformation
        Else
            Me.tboxQuery = Replace(Me.tboxProjects.Value, ",", " Or ")
            Debug.Print Me.tboxQuery
            
            DoCmd.OpenReport "DOItempPrjNums", acViewReport
        End If
    End Sub

  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,725
    And what was the value of me.tboxquery?
    The space should not be an issue. I thought you might get replacements such as 45 or68, and that would be a problem(at least in my view).

    So hidden somewhere in your processing is the "query". It will be the recordsource of your report.

    Can you show us what the report recordsource is? Both the original and rendered SQL with the constraint?

  5. #5
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74
    When the value of tboxProjects is 54, 56
    The value of tboxQuery is 54 Or 56

    For the form RecordSource, I simply put [Forms]![frmProjectID].[form]![tboxQuery] in the criteria field for ProjectID in the Query Builder.

    Click image for larger version. 

Name:	Query.png 
Views:	8 
Size:	4.9 KB 
ID:	12740

    Can I pass a query value in the DoCmd.OpenReport call as the optional [WhereCondition] instead of referencing the value on the form?

  6. #6
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74
    I've tried sending the query as the WhereCondition of the OpenReport command. My query is showing up in the "Filter" portion of the Report's property sheet, but it's generating the report for ALL records instead of the records included in the WhereClause.

    Code:
    Private Sub btnGenQuery_Click()
        Dim query As String
        
        If IsNull(Me.tboxProjects) Then
            MsgBox "You must enter at least one project.", vbInformation
        Else
            Me.tboxQuery = Replace(Me.tboxProjects.Value, ",", " Or")
                    
            query = "ProjectID = " & Me.tboxQuery
            Debug.Print query
            DoCmd.OpenReport "DOItempPrjNums", acViewReport, , query
        End If
    End Sub
    Click image for larger version. 

Name:	Filter.png 
Views:	7 
Size:	8.9 KB 
ID:	12741

  7. #7
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74
    FACE PALM MOMENT.

    A Query can't read ProjectID = 52 Or 53 it must read ProjectID = 52 Or ProjectID = 53

    Edited the Replace function include "ProjectID = " after the Or.

    Code:
    Private Sub btnGenQuery_Click()
        Dim query As String
        
        If IsNull(Me.tboxProjects) Then
            MsgBox "You must enter at least one project.", vbInformation
        Else
            Me.tboxQuery = Replace(Me.tboxProjects.Value, ",", " Or ProjectID = ")
                    
            query = "ProjectID = " & Me.tboxQuery
            Debug.Print query
            DoCmd.OpenReport "DOItempPrjNums", acViewReport, , query
        End If
    End Sub

  8. #8
    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,725
    Ok, that is true. However, there is another construct that you could use by adjusting your code a bit.

    Instead of
    WHERE ProjectID = 52 Or ProjectID = 53

    you can use
    WHERE ProjectID In ( 52 , 53 ) ... as long as ProjectId is numeric

  9. #9
    BlueIshDan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    8

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

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 04:45 PM
  2. Sample DB : Search Multiple Data Separated by Comma
    By spideynok in forum Sample Databases
    Replies: 3
    Last Post: 03-29-2012, 10:52 PM
  3. Replies: 6
    Last Post: 06-26-2011, 12:15 AM
  4. Replies: 2
    Last Post: 04-07-2011, 10:15 AM
  5. Replies: 1
    Last Post: 07-31-2009, 03:57 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