Adding values multiple columns to listbox in form

2019-02-17 11:52发布

问题:

I have problem with adding values to multiple columns in listbox in access. I have tried solution like this: Adding items in a Listbox with multiple columns and this: vba listbox multicolumn add [duplicate], but it doesn't work. Listbox in my case hasn't a property "List". I have compile error:

Private Sub cmdAddPosition_Click()

Dim i As Integer

Me.lstAddPositions.ColumnCount = 7

If Me.txtAddPos.Value = i And i > 0 And i < 50 Then
     Me.lstAddPositions.AddItem (Me.txtAddPos.Value)
    'Me.lstAddPositions.AddItem(Me.txtAddPos.Value,(i))
     Me.lstAddPositions.List(0, i) = Me.txtAddPos.Value
    'Me.lstAddPositions.Column(0, i) = Me.txtAddPos.Value 'adding number of position
    'Me.lstAddPositions.Column(2, i) = Me.lstAddHidden.Column(0, 0) 'adding titel
End If

Me.lstAddPositions.Requery

End Sub

What can I do in this situation?

回答1:

Here is an example of adding items to a multi-column unbound list box on an access form if the row source is a value list. You have to add it by creating a string that you place in a value list.

Private Sub cmdAddPosition_Click()
    Dim i As Integer

    Me.lstAddPositions.ColumnCount = 7

    If Me.txtAddPos.Value = i And i > 0 And i < 50 Then
         Me.lstAddPositions.AddItem "Col1" & "," & "col2" & "," & "Col3" & "," & _
         "Col4" & "," & "Col5" & "," & "col6" & "," & "col7"  &";"     
    End If

    Me.lstAddPositions.Requery
End Sub


回答2:

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.



回答3:

select propety

Row Source Type => Value List

Code :

ListbName.ColumnCount=2

ListbName.AddItem "value column1;value column2"



回答4:

I didn't quite understand Zaider's answer, but taking his sample I ended up with this which worked for my unbound listbox:

Me.listbox2.AddItem (Me.listbox1.Column(0) & ";" & Me.listbox1.Column(1))


回答5:

Firstly set the following properties for list box

Row source type: value list Column count: 2

Suppose name of list box is : listName Let us assume that u want to add two different elements in two different columns and two different strings are stored in

String1 and String2 then follow the code

Code:

Dim strName as string
strName=String1&";"&String2
Me.listName.addItem strName