Display Selection of a List Box in a Text Box (1 Viewer)

But the code does not fit.

You insert the separator (line break) at the end, but cut off the 1st character at the beginning.
Either remove the line break at the end, or (better readable for me) add the line break at the beginning and then remove the line break at the beginning.

Code:
    With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = SelectedItemInfo & .Column(0, SelectedItem) & " | " & .Column(1, SelectedItem) & " | " & .Column(2, SelectedItem) & vbNewLine
              strFileNum = .Column(0) & strFileNum ' <-- without "|" or "," ?
        Next SelectedItem
    End With
  
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = left(SelectedItemInfo, len(SelectedItemInfo) - len(vbNewLine))
    End If
or
Code:
    With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = SelectedItemInfo & vbNewLine &  .Column(0, SelectedItem) & " | " & .Column(1, SelectedItem) & " | " & .Column(2, SelectedItem)
              strFileNum = .Column(0) & strFileNum ' <-- without "|" or "," ?
        Next SelectedItem
    End With
  
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = Mid(SelectedItemInfo, len(vbNewLine))
    End If
Thank you so much. I am going with your first code. It works. Thanks a lot
 
Hi,

How do I deselect the previously selected values from my list box?

I have created a query and it's based on the key words I texted in a text box. The row source of my list box is that query.

Like this:

1718293562009.png


Now, if I keyed another keyword in the Text box the previously selected values (Here, I have selected 3rd and firth value) are getting selected.

1718293688462.png


here in my previous example I have selected 3rd and fourth values and these exact positions are selecting when I am giving another keyword.

Once I am keying a new word in the Text field I want to deselect all the previous values which I have selected previously.

Could you please let me know how I do this?

Thanks
 
How do I deselect the previously selected values from my list box?

see: ListBox.Selected property (Access) | Microsoft Learn

Code:
Public Sub DeselectAllSelectedListboxItems(ByVal ListBoxRef As ListBox)

  Dim itm As Variant
 
  For Each itm In ListBoxRef.ItemsSelected
    ListBoxRef.Selected(itm) = False
  Next
 
End Sub
 
Hi,

I want to Insert Into the values of the selected items in my list box in to a table.

This is my code:


Code:
Private Sub Command74_Click()

        Dim db As Database
        Dim FlNum As String
        Dim FlNam As String
        Dim flReqBy As String
        Dim FlReqDate As Date
        Dim FlRetDate As Date
        Dim UserN As String
        Dim i As Variant
    
        Set db = CurrentDb
        FlNum = Me.TestListBox.Column(0)
        FlNam = Me.TestListBox.Column(2)
        UserN = Environ("UserName")
        FlReqDate = Date
        
        For Each i In Me.TestListBox.ItemsSelected
        
        db.Execute "Insert Into TestInsertTable (File_Number, File_Name, File_Requested_By, File_Requested_Date,File_Return_Date,File_Status_Notes) Values ('" & FlNum & "','" & FlNam & "','" & UserN & "','" & FlReqDate & "',#11-02-2024#,'Notes');"
        
        Next i
End Sub

The values which I am selecting from my list boxes are not inserting in to the Table properly.

This is my Selection

1718394088350.png


and this is what I am getting in my Table:


1718394348705.png


It should be like:

1718394442226.png

Could you please let me know how should I modify my Code?

I am attaching my database
 

Attachments

You set your variables outside of the loop
it would be something like this inside the loop
Code:
expression.Column (Index, Row)
FlNum = Me.TestListBox.Column(0,i)
FlNam = Me.TestListBox.Column(2,i)
 
You set your variables outside of the loop
it would be something like this inside the loop
Code:
expression.Column (Index, Row)
FlNum = Me.TestListBox.Column(0,i)
FlNam = Me.TestListBox.Column(2,i)
Thank you so much. It works
 

Users who are viewing this thread

Back
Top Bottom