Referencing a listbox in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jesse07
    New Member
    • Sep 2007
    • 18

    Referencing a listbox in an array

    I am trying to change a value in an array when the user selects a name a from a list or combo box. Also, I want the highlighted item to be displayed in a Label for the current selection. After all the names are selected from the boxes than I going to write those to a file or table. It sounds real simple and it probably is, but I am having a brain fart.

    lblName - label
    lstName - name of listbox
    arrName (1, lstName) - array to hold item for list box

    Thank you.
  • Ack Programming
    New Member
    • Sep 2007
    • 8

    #2
    I would suggest uding a combo box with its style property set to "Simple Combo giving you a combo box with a listbox look and in the coding of the combo box on its click event use the following

    Label1.Caption = combobox(index) .text

    Index Refering to the array number of the Combo box

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      Originally posted by jesse07
      I am trying to change a value in an array when the user selects a name a from a list or combo box. Also, I want the highlighted item to be displayed in a Label for the current selection. After all the names are selected from the boxes than I going to write those to a file or table. It sounds real simple and it probably is, but I am having a brain fart.

      lblName - label
      lstName - name of listbox
      arrName (1, lstName) - array to hold item for list box

      Thank you.
      Declare an integer (dim i as integer) and an array of strings (dim Arr1() as string) in the general declarations.

      In the Change event of the listbox there should be something like this:

      [CODE=vb]i=i+1
      redim preserve arr1(1 to i)
      arr1(i) = listbox1.list(l istbox1.listind ex)
      label1.caption = listbox1.list(l istbox1.listind ex)[/CODE]

      now you have all the selected items in the array
      HTH

      Comment

      • jesse07
        New Member
        • Sep 2007
        • 18

        #4
        lblCurrentPlaye r.Caption = cmbPlayer(Index ).Text

        I still get an error. I have tried that syntax before, but I still don't think I am referencing the right part of the combo box. Does the cmbPlayer(Index ).Text refer to the selected item from the combo box?

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          add comment

          Originally posted by jesse07
          lblCurrentPlaye r.Caption = cmbPlayer(Index ).Text

          I still get an error. I have tried that syntax before, but I still don't think I am referencing the right part of the combo box. Does the cmbPlayer(Index ).Text refer to the selected item from the combo box?
          indexes are from 0 to the elements in the list box - 1

          listbox1.listin dex will return the hightlighted index, so if you use

          lblcurrentplaye r.caption = cmbplayer.list( cmbplayer.listi ndex)

          will do
          also works for combo boxes

          Comment

          • jesse07
            New Member
            • Sep 2007
            • 18

            #6
            I've walked away from this, and I am trying this again. I still can only display the number from the combobox of the listindex.

            Ex. Combo box has (mike, jim, alex, and pete). When I select Alex it is displayed by the listindex of 2. I want to show the name alex, and then store this in an array.


            I cannot refereence cmbbox.list.(cm bbox.listindex) because my version of VBA doesn't have a .list property. Access 2000

            Comment

            • kadghar
              Recognized Expert Top Contributor
              • Apr 2007
              • 1302

              #7
              Originally posted by jesse07
              I've walked away from this, and I am trying this again. I still can only display the number from the combobox of the listindex.

              Ex. Combo box has (mike, jim, alex, and pete). When I select Alex it is displayed by the listindex of 2. I want to show the name alex, and then store this in an array.


              I cannot refereence cmbbox.list.(cm bbox.listindex) because my version of VBA doesn't have a .list property. Access 2000
              I dont know why is this, i dont have access here to check it out, but in the while:

              Since you're using a combobox, when you select something its showed as the combobox text. so you can store that by writing in the change procedure something like:

              [CODE=vb]i=i+1
              redim preserve myArry(1 to i)
              myarr(i) = combobox1.text
              msgbox combobox1.text[/CODE]

              Just make sure that the combobox matchrequired property is true.
              HTH

              Comment

              • jesse07
                New Member
                • Sep 2007
                • 18

                #8
                Thanks everyone for your input. I figured it out. I went to the control source feature in Access and put in this code to reference it to a text box. As soon as I had the value linked to the textbox everything else is easy.

                =[cmbPlayer].[column](1)

                I haven't done much programming the last 5 years, it is amazing how you forget little steps. Thanks again.

                Comment

                Working...