multi-select list box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MSD

    multi-select list box

    I am running a report that uses a query as its record source and opens a
    form collecting beginning and ending item numbers to feed to the query. This
    works, but now I'm trying to use the result of a multi-select list box on
    the form to feed multiple item numbers to the query.

    In other words - if you pick item #100,105,110 from the list box, the query
    should display information for those 3 items.

    If I type 'IN (100,105,110)' in the criteria cell of the 'itemNo' field in
    my query, it works - I get information for those 3 items. However, I would
    like the list of items to be variable, coming from the items selected in the
    multi-select list box on my form.

    How do I loop through the items in the list box and feed them to the query?
    What should I put in the criteria cell for my query?

    Thanks very much,

    Emma


  • Douglas J. Steele

    #2
    Re: multi-select list box

    Take a look at http://www.mvps.org/access/forms/frm0007.htm at "The Access
    Web"

    --
    Doug Steele, Microsoft Access MVP
    will.i.am created a car for Mercedes-AMG that will fund inner city school engineering programs to prepare our youth for a technological tomorrow

    (No private e-mails, please)



    "MSD" <msdanielson@ho tmail.com> wrote in message
    news:jweUb.4034 01$ts4.58439@pd 7tw3no...[color=blue]
    > I am running a report that uses a query as its record source and opens a
    > form collecting beginning and ending item numbers to feed to the query.[/color]
    This[color=blue]
    > works, but now I'm trying to use the result of a multi-select list box on
    > the form to feed multiple item numbers to the query.
    >
    > In other words - if you pick item #100,105,110 from the list box, the[/color]
    query[color=blue]
    > should display information for those 3 items.
    >
    > If I type 'IN (100,105,110)' in the criteria cell of the 'itemNo' field in
    > my query, it works - I get information for those 3 items. However, I would
    > like the list of items to be variable, coming from the items selected in[/color]
    the[color=blue]
    > multi-select list box on my form.
    >
    > How do I loop through the items in the list box and feed them to the[/color]
    query?[color=blue]
    > What should I put in the criteria cell for my query?
    >
    > Thanks very much,
    >
    > Emma
    >
    >[/color]


    Comment

    • Rich P

      #3
      Re: multi-select list box

      Well, if you don't mind writing a little bit of code in your report you
      could do something like this in the Report Open Event and Detail Event:

      *************** *************** *************** *********
      Option Compare Database
      Option Explicit

      Dim RS As Recordset

      Private Sub Detail_Format(C ancel As Integer, FormatCount As Integer)
      Text0 = RS(0)
      RS.MoveNext
      End Sub

      Private Sub Report_Open(Can cel As Integer)
      Dim frm As Form, ctl As Control,
      Dim v As Variant, str1 As String, strSql As String
      Set frm = Forms!Form1
      Set ctl = frm!List16
      For Each v In ctl.ItemsSelect ed
      str1 = str1 & "'" & ctl.ItemData(v) & "',"
      Next
      str1 = Left(str1, Len(str1) - 1) 'get rid of last comma
      Me.RecordSource = "Select fld1 From Table1 " _
      & "Where fld1 In (" & str1 & ")"
      strSql = "Select fld1 From Table1 " _
      & "Where fld1 In (" & str1 & ")"
      Set RS = CurrentDb.OpenR ecordset(strSql )
      End Sub
      *************** *************** *************** *********

      In this example, the report is not directly bound to a recordsource.
      You set the recordsource in the Report_Open event. I am only using one
      field from a table, but you could select multiple fields and you filter
      this recordset from the selections on the listbox on your form (which is
      list16 in my example). You can use the

      DoCmd.OpenRepor t "Report1" statement

      and when Report1 open it will automatically read the selections from the
      listbox. Then say you have 5 fields in the detail section of your
      report, they could be txt0, txt1, txt2, txt3, txt4 or maybe you named
      them

      txtID, txtRegion, txtName, txtAddress, txtPhone

      In the Detail section you would do this:

      Private Sub Detail_Format(C ancel As Integer, FormatCount As Integer)
      txtID = RS(0) 'or RS!ID
      txtRegion = RS(1) 'or RS!Region
      txtName = RS(2) 'or RS!Name
      txtAddress = RS(3) 'or RS!Address
      txtPhone = RS(4) 'or RS!Phone
      RS.MoveNext
      End Sub

      And, of course, your sql statement in the report_open event would select
      the same fields for the RecordSource and for the Recordset var (RS).

      Rich

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Emma Danielson

        #4
        Re: multi-select list box

        Thanks so much for your help. I think I'm close to having it work now.

        I modified your code a bit - I create a string that contains the values
        from the list box that I need for the parameter query. It looks like
        this, e.g. 201,203,205.

        In my parameter query, in the criteria cell for itemNo, I have :
        In ([Forms]![frmSelectItems]![txtSelect])
        where txtSelect is the name of the hidden control containing the string
        '201,203,205' and frmSelectItems is the form with the list box and
        hidden text box.

        If I type:
        In (201,203,205)
        directly into the criteria cell, it works, however it doesn't work the
        way I am doing it - even though the value of
        [Forms]![frmSelectItems]![txtSelect] is 201,203,205.

        Any ideas?

        Thanks again,

        Emma



        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Shane Pea

          #5
          Re: multi-select list box

          Hi Emma,

          This is the code I use to print from a multi-select box:

          InSQL = CreateINClauseF romLB("lbListbo x", "frmSelectItems ",
          ColWhereIDIsObt ainedFrom, NoPosted)
          SQLString = "[FieldName] IN (" & InSQL & ")"
          DoCmd.OpenRepor t ReportName, , , Filter
          Msgbox NoPosted & "Item(s) printed"




          Public Function CreateINClauseF romLB(LB As String, FormName As String, Col
          As Long, ByRef Count As Long) As String
          'Create an comma seperated string from the selected items in a list box
          'Returns the no of items in the list in Count

          Dim ItemIndex As Variant
          Dim Tempst As String
          If TestMode = False Then On Error GoTo ErrorHand

          Count = 0

          If (Forms(FormName )(LB).ItemsSele cted.Count < 1) Then
          ' If there are no items in the list, close form and exit procedure.
          CreateINClauseF romLB = ""
          Exit Function
          End If

          ItemIndex = 0

          'Determine which listbox items are selected.
          For Each ItemIndex In Forms(FormName) (LB).ItemsSelec ted
          If Nz(Forms(FormNa me)(LB).Column( Col, ItemIndex), -1) > 0 Then
          Tempst = Tempst & Format(Forms(Fo rmName)(LB).Col umn(Col, ItemIndex))
          & ","
          Count = Count + 1
          End If
          Next ItemIndex

          'Ensure that temp string has some characters first, then remove last comma
          If Tempst <> "" Then Tempst = Left(Tempst, Len(Tempst) - 1)

          CreateINClauseF romLB = Tempst
          Exit Function
          ErrorHand:
          EHD , MODULE_NAME
          End Function





          "Emma Danielson" <msdanielson@ho tmail.com> wrote in message
          news:4022820c$1 $70303$75868355 @news.frii.net. ..[color=blue]
          > Thanks so much for your help. I think I'm close to having it work now.
          >
          > I modified your code a bit - I create a string that contains the values
          > from the list box that I need for the parameter query. It looks like
          > this, e.g. 201,203,205.
          >
          > In my parameter query, in the criteria cell for itemNo, I have :
          > In ([Forms]![frmSelectItems]![txtSelect])
          > where txtSelect is the name of the hidden control containing the string
          > '201,203,205' and frmSelectItems is the form with the list box and
          > hidden text box.
          >
          > If I type:
          > In (201,203,205)
          > directly into the criteria cell, it works, however it doesn't work the
          > way I am doing it - even though the value of
          > [Forms]![frmSelectItems]![txtSelect] is 201,203,205.
          >
          > Any ideas?
          >
          > Thanks again,
          >
          > Emma
          >
          >
          >
          > *** Sent via Developersdex http://www.developersdex.com ***
          > Don't just participate in USENET...get rewarded for it![/color]


          Comment

          Working...