Results 1 to 9 of 9
  1. #1
    jbates is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2013
    Posts
    8

    Multiselect Combo/List Box to generate a report

    I’m a novice with Access so would be grateful for some guidance please.

    I’m currently designing a database and have the following issue:

    I have an ‘Unbound’ Combo Box and an ‘Unbound’ List Box which I’m using to create a report. Basically the user can select numerous ‘part numbers’ from the Combo Box (which is sourced from a table) which can then be (when selected by the user) added to a List Box. The user can then select the ‘part numbers’ that appear in the list box to generate a report. This works well with a code I got from ‘PBaldy’, however the list box shows the ‘ID’ number. What I want to show in the list box is the ‘Part Number’ not its ‘ID’ number and for it to generate a report from there. I’ve managed to get the ‘part number’ into the list box however the report doesn’t recognise my command and appears blank. A majority of the part numbers are numeric however there are some that contain letters too.
    The code I have on the ‘Open Report’ command button is:

    Private Sub Command25_Click()
    On Error GoTo Err_cmdOpenReport_Click
    Dim strWhere As String
    Dim ctl As Control
    Dim varItem As Variant

    'make sure a selection has been made


    If Me.MyList.ItemsSelected.Count = 0 Then
    Exit Sub
    End If

    'add selected values to string
    Set ctl = Me.MyList
    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 "rptSearch", acViewReport, , "ID IN(" & strWhere & ")"

    Exit_cmdOpenReport_Click:
    Exit Sub

    Err_cmdOpenReport_Click:
    MsgBox Err.Description
    Resume Exit_cmdOpenReport_Click
    End Sub

    Help with the above would be most appreciated.

    Thank you.

  2. #2
    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'm unclear as to what the problem is. I'd point out that you still refer to the ID field here:

    DoCmd.OpenReport "rptSearch", acViewReport, , "ID IN(" & strWhere & ")"

    Also, if you look at my link where you got that, there's a different line to use inside the loop for a text value, which yours must be if it can have letters in it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jbates is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2013
    Posts
    8
    Many thanks for your prompt reply.

    The DoCmd.OpenReport "rptSearch", acViewReport, , "ID IN(" & strWhere & ")" works when the 'ID' of the part number is shown in the List Box but I want to show the actual 'part number' in the list box and then run a report. I did try doing the following DoCmd.OpenReport "rptSearch", acViewReport, , "PartNumber IN(" & strWhere & ")" however when I opened the report a box would appear prompting me to enter the part number.

    *Also, if you look at my link where you got that, there's a different line to use inside the loop for a text value, which yours must be if it can have letters in it.

    By incorporating that line will it pick up both text and numerical fields? i.e I may have a part number that says 91256AO2. I get the impression that you can only use one or the other.

    Thanks again.

  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
    If you got prompted for part number, it must not be in the report's source query. That prompt is Access telling you it can't find something.

    The method depends on the data type of the field, not what it contains. Yours is text, but the method should work even if the value is "12345".
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    jbates is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2013
    Posts
    8
    I've checked the report design, and in the 'Data' tab the record source is pointing to my query which has the part number but in the Filter it shows 'ID IN(7)' (which is the ID number of the 'part number', on the 'Filter On Load' it's set to 'No', in the 'Order By' it says [tblECCNLog].[Part Number], 'Order By On Load' is set to 'Yes' and 'Allow Filters' is set to 'Yes'. I changed the query on the 'Open Report command button' on my form to the following:

    Set ctl = Me.MyList
    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 "rptSearch", acViewReport, , "Part Number IN(" & strWhere & ")"

    Which then gives me an error message saying 'Syntax error (missing operator) in query expression 'Part Number IN ('91*********')'.

    Sorry to be incompetent with this but can you advise where the mistake is please.

    Thanks again.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Because of the inadvisable space, the field name must be bracketed.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    jbates is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2013
    Posts
    8
    Your an absolute star. Thank you so much.

    With your help I've managed to resolve the problem.

    Thank you again, your help has been most appreciated.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help, and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    jbates is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2013
    Posts
    8
    Thank you. I would highly recommend this site to Access users

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

Similar Threads

  1. Generate Report that list current training course only
    By TheHarleygirl2005 in forum Reports
    Replies: 6
    Last Post: 09-27-2013, 12:19 PM
  2. Replies: 3
    Last Post: 04-24-2013, 02:41 PM
  3. Replies: 1
    Last Post: 11-23-2012, 10:26 PM
  4. Get combo box value to generate report
    By chuyee17 in forum Access
    Replies: 3
    Last Post: 10-28-2011, 05:24 AM
  5. Replies: 9
    Last Post: 08-30-2011, 04:08 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