Listbox (1 Viewer)

goldeliz

New member
Local time
Today, 04:57
Joined
Dec 24, 2020
Messages
11
I have a listbox which populates fine and allows me to select an item, but I can not get the text from that item.
Code:

Code:
For i = 0 To myListbox.ListCount - 1
           If myListbox.Selected(i) Then 
              'do something
           End If  
Next i

Debuging the code it next gets to the do something line i.e. myListbox.Selected(i) never seems to be true in the watch it seems to always have value 0. Any ideas why?
 
Last edited by a moderator:

goldeliz

New member
Local time
Today, 04:57
Joined
Dec 24, 2020
Messages
11
Select an item from the listbox so I can copy it to a textbox
 

moke123

AWF VIP
Local time
Today, 06:57
Joined
Jan 11, 2013
Messages
3,849
Multi select? Why iterate through the list?

If its just a regular listbox then
me.MyTextboxName = me.MyListbox
or
me.MyTextboxName = me.MyListbox.column(0) ' 0 is first column, 1 for the second, etc. as it is zero based.
 

goldeliz

New member
Local time
Today, 04:57
Joined
Dec 24, 2020
Messages
11
There is only 1 column, with around 5 items in the listbox, although there could be more. It is not multi select. Only one item can be selected. What I need to do is whichever item is selected needs to be copied to a textbox so I can copy it to the clipboard.
 

missinglinq

AWF VIP
Local time
Today, 06:57
Joined
Jun 20, 2003
Messages
6,423
Where TargetField is the name of your Textbox:

Code:
Private Sub MyListbox_Click()

Me.TargetField = Me.MyListbox
If Nz(Me.TargetField, "") <> "" Then
  TargetField.SetFocus
  TargetField.SelStart = 0
  TargetField.SelLength = Len(Me.TargetField)
  DoCmd.RunCommand acCmdCopy
End If

End Sub
Note that this will place the item on the Clipboard...no other user action/input needed except for clicking on the item in the ListBox.

Linq ;0)>
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:57
Joined
May 7, 2009
Messages
19,169
Why comparing to null string?
If you clicked on a listbox, automatically it will select an item.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:57
Joined
May 7, 2009
Messages
19,169
Why do you need a clipboard?
You can copy it to a Global variable or a Tempvar.
 

goldeliz

New member
Local time
Today, 04:57
Joined
Dec 24, 2020
Messages
11
That works thank you, although I'm not sure why, everything I've read suggests you need to get the selected item i.e. along the lines :
If myListbox.Selected(i) Then

I will take it. Thanks for the assist.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:57
Joined
Sep 21, 2011
Messages
14,038
That works thank you, although I'm not sure why, everything I've read suggests you need to get the selected item i.e. along the lines :
If myListbox.Selected(i) Then

I will take it. Thanks for the assist.
That is for multi select as already mentioned?
 

Jjaeger14

New member
Local time
Today, 13:57
Joined
Dec 26, 2020
Messages
15
That works thank you, although I'm not sure why, everything I've read suggests you need to get the selected item i.e. along the lines :
If myListbox.Selected(i) Then

I will take it. Thanks for the assist.
I think you are seeing a lot of google posts for both multi select list boxes and then multi column list boxes. For a simple list box, me.lstName works just fine. So you code would be txtTextbox = lstListbox, basically. you could improve clarity with me.txtTextbox = me.lstListbox. And if you want this to occur from another form or a general module then: forms!formname!txtTextbox = forms!formname!lstListbox

Jack
 

moke123

AWF VIP
Local time
Today, 06:57
Joined
Jan 11, 2013
Messages
3,849
As Jack points out, a simple listbox is equal to the bound column of the item selected.

In a multi- select listbox you need to iterate through the .ItemsSelected collection of the listbox.
Being a big fan of multiselect listboxes, I use a public function which returns an array of the selected items.

Code:
Public Function getLBX(Lbx As ListBox, Optional intColumn As Variant = 0, Optional Seperator As String = ",", _
                       Optional delim As Variant = Null) As String


'Iterates thru the multiselect listbox and constructs an array of the selected items
'Arguments:
'Lbx is Listbox Object ie.Me.MyListbox
'intColumn is the column # to be returned
'Seperator is the character seperating items in array returned
'Delim is optional delimiter to be return in array ie. #1/1/2001#,#12/25/2015#

    Dim strlist As String
    Dim VarSelected As Variant

    'On Error GoTo getLBX_Error

    If Lbx.ItemsSelected.count = 0 Then
        'MsgBox "Nothing selected"
    Else

        For Each VarSelected In Lbx.ItemsSelected

            If Nz(Lbx.Column(intColumn, (VarSelected)), "") <> "" Then

                strlist = strlist & delim & Lbx.Column(intColumn, (VarSelected)) & delim & Seperator

            Else

                strlist = strlist

            End If

        Next VarSelected

        If Nz(strlist, "") <> "" Then

            strlist = Left$(strlist, Len(strlist) - 1)  'remove trailing comma

        End If
    End If

    getLBX = strlist

    On Error GoTo 0
    Exit Function

getLBX_Error:

    MsgBox "Error " & Err.Number & " (" & Err.description & ") in procedure getLBX of Module modLBX"

End Function

Here's an example to play with
 

Attachments

  • MSListBX.accdb
    500 KB · Views: 124
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:57
Joined
Feb 19, 2002
Messages
42,970
List boxes and combos have TWO points of binding. The ControlSource which indicates which field in the form's RecordSource will store the selected value and the RowSource which includes the list that the combo/listbox will display.

Keep in mind that the visible value of the combo/Listbox is not necessarily the column that is actually being saved to the ControlSource. Is that the problem you are experiencing?
 

Users who are viewing this thread

Top Bottom