Adding values multiple columns to listbox in form access vba

前端 未结 5 1686
谎友^
谎友^ 2021-01-13 02:07

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 li

相关标签:
5条回答
  • 2021-01-13 02:18

    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.

    0 讨论(0)
  • 2021-01-13 02:20

    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))
    
    0 讨论(0)
  • 2021-01-13 02:28

    select propety

    Row Source Type => Value List

    Code :

    ListbName.ColumnCount=2

    ListbName.AddItem "value column1;value column2"

    0 讨论(0)
  • 2021-01-13 02:33

    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
    
    0 讨论(0)
  • 2021-01-13 02:34

    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
    
    0 讨论(0)
提交回复
热议问题