Results 1 to 9 of 9
  1. #1
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171

    Basic Coding Question - Novice


    I am trying to create a report that allows the user to select multiple contractors. I found this code and it works except for the last line. I know this is pretty simple but I don't know how to fix it. The code in RED is what is not working.

    Code:
    Private Sub cmdPerfIssuesContractorRpt_Click()
      On Error GoTo Err_cmdPerfIssuesContractorRpt_Click
      Dim strWhere      As String
      Dim ctl           As Control
      Dim varItem       As Variant
      'make sure a selection has been made
      If Me.cboContractorSelection.ItemsSelected.Count = 0 Then
        MsgBox "Must select at least 1 employee"
        Exit Sub
      End If
      'add selected values to string
      Set ctl = Me.cboContractorSelection
      For Each varItem In ctl.ItemsSelected
        strWhere = strWhere & ctl.ItemData(varItem) & ","
      Next varItem
      'trim trailing comma
      strWhere = Left(strWhere, Len(strWhere) - 1)
      'open the report, restricted to the selected items
      DoCmd.OpenReport "rptPerfIssuesbyContractor", acPreview, , "Contractor IN(" & strWhere & ")"
    Exit_cmdPerfIssuesContractorRpt_Click:
      Exit Sub
    Err_cmdPerfIssuesContractorRpt_Click:
      MsgBox Err.Description
      Resume Exit_cmdPerfIssuesContractorRpt_Click
    End Sub

  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    How about using the like command

    Code:
    DoCmd.OpenReport "rptPerfIssuesbyContractor", acPreview, , "Contractor like ('*" & strWhere & "'*)"

  3. #3
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    It still says "Missing operator in query expression"

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I don't think Like is appropriate in this case. What's the data type of the Contractor field? If it's text, you need delimiters. Note the second commented out strWhere:

    http://www.baldyweb.com/multiselect.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    If this is a multiselect listbox or combobox, you need the IN operator
    There needs to be a space after IN as shown below

    If it still fails, suggest you add the line in BLUE to check the output

    Code:
    'trim trailing comma  
    strWhere = Left(strWhere, Len(strWhere) - 1)
    Debug.Print strWhere
    
     'open the report, restricted to the selected items   
    DoCmd.OpenReport "rptPerfIssuesbyContractor", acPreview, , "Contractor IN (" & strWhere & ")"
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    Paul
    I am not getting any errors but I am also not getting any data in the report. This report was originally setup for a single Contractor selection and I know the report worked correctly. Now that I am adding a multiple selection to the report no data is coming up, only the headings.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Are you sure the bound column of the listbox returns the values expected in the where clause? Can you attach the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    If your cboContractorSelection is actually a combobox, I don't think this will apply, as I believe a multiselect combobox has to do with multi-valued fields, which I do not use.
    The below code is for a listbox. Added code to replaces the right side comma with a close parens.
    You may have to adjust the red index to get the correct column in the row.

    Code:
    Public Sub genWhere()
    Dim sq As String
    Dim varItem As Variant
    Dim strWhere As String
    sq = Chr$(39)      'single quote
    
    
    strWhere = "Contractor In("
    For Each varItem In cboContractorSelection.ItemsSelected
        strWhere = strWhere & sq & Me.cboContractorSelection.Column(1, varItem) & sq & ","
    Next varItem
    strWhere = Left(strWhere, Len(strWhere) - 1) & ")"
    Debug.Print strWhere
    DoCmd.OpenReport "rptPerfIssuesbyContractor", acPreview, , strWhere
    End Sub
    Last edited by davegri; 03-22-2018 at 09:29 PM. Reason: added right side code to change comma to rt parens

  9. #9
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    @davegri - good point
    Mutliselect combo boxes are indeed based on the dreaded multivalue fields so if you need part of them you will need to treat as arrays and use the split function

    As for listboxes, the code I provided in post 3 should be correct & it trims off the trailing comma
    Your code appears to be just a variation on that
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. novice question....
    By fazer909 in forum Access
    Replies: 2
    Last Post: 10-27-2014, 01:34 AM
  2. Really novice question regarding queries
    By ggdoggie in forum Access
    Replies: 9
    Last Post: 06-11-2014, 01:39 PM
  3. Data sum novice question
    By DougTheSquid in forum Access
    Replies: 1
    Last Post: 10-13-2012, 12:45 PM
  4. Really Novice Question
    By AccessNoob1 in forum Forms
    Replies: 1
    Last Post: 04-07-2011, 09:44 AM
  5. Novice question about using established forms
    By runabout6 in forum Access
    Replies: 2
    Last Post: 08-23-2010, 09:00 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