Here's a full example of how you can add multiple columns to a listbox.
SQL to create the table:
DROP TABLE Test;
CREATE TABLE Test (TestID AUTOINCREMENT(1, 1),
TestName TEXT,
TestDescription TEXT,
CONSTRAINT TestPKey PRIMARY KEY (TestID));
INSERT INTO Test (TestName, TestDescription)
VALUES ('Test A', 'Testing Record A')";
INSERT INTO Test (TestName, TestDescription)
VALUES ('Test B', 'Testing Record B')";
INSERT INTO Test (TestName, TestDescription)
VALUES ('Test C', 'Testing Record C')";
Button Event Code:
Private Sub TestButton_Click()
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim lb As ListBox
Dim rowStr As String
Set rst = CurrentDb.OpenRecordset("Test")
Set lb = Me.TestListBox
' Set the number of listbox columns to reflect recordset fields
lb.ColumnCount = rst.Fields.Count
' Set the row source type to Value List
lb.RowSourceType = "Value List"
' Erase the listbox data so we can populate it
lb.RowSource = ""
' If ColumnHeads property is enabled, then first record is field
' names. Lets populate those.
If lb.ColumnHeads Then
rowStr = ""
' Build a string for each record
For Each fld In rst.Fields
rowStr = rowStr & replace(fld.Name,",","") & ","
Next
' Strip final comma
rowStr = Left(rowStr, Len(rowStr) - 1)
' Add each record (all fields) at once.
lb.AddItem rowStr
End If
' Loop through each record
Do Until rst.EOF
' Build a record string and add it
rowStr = ""
For Each fld In rst.Fields
rowStr = rowStr & replace(fld.Value,",","") & ","
Next
rowStr = Left(rowStr, Len(rowStr) - 1)
lb.AddItem rowStr
rst.MoveNext
Loop
' Close and release objects
rst.Close
Set fld = Nothing
Set rst = Nothing
Set lb = Nothing
End Sub
If you wish to change the listbox contents you will need to blow away and rebuild the recordsource property of the listbox. In my experience, trying to modify individual rows when multiple columns are involved - never works.
Hopefully that helps out.
.List(.ListCount - 1, 1) = TextBox2.Value
and make changes to suit your case? – ConantMe.lstAddPositions.AddItem Me.txtAddPos.Value
, I'm clicking a button and nothing, listbox still is empty: img – Mohair