在表单访问vba中将多列值添加到列表框

时间:2013-09-18 11:07:05

标签: vba ms-access access-vba

我在访问列表框中向多列添加值时遇到问题。 我尝试过这样的解决方案:Adding items in a Listbox with multiple columns和此:vba listbox multicolumn add [duplicate],但它不起作用。在我的情况下,列表框没有属性“列表”。我有编译错误:

enter image description here

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

在这种情况下我该怎么办?

5 个答案:

答案 0 :(得分:5)

以下是如果行源是值列表,则将项添加到访问表单上的多列未绑定列表框的示例。您必须通过创建放置在值列表中的字符串来添加它。

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

答案 1 :(得分:0)

以下是如何将多个列添加到列表框的完整示例。

用于创建表的SQL:

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')";

按钮事件代码:

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

如果您想更改列表框内容,则需要吹掉并重建列表框的recordsource属性。根据我的经验,尝试在涉及多个列时修改单个行 - 从不起作用。

希望这会有所帮助。

答案 2 :(得分:0)

我不太了解Zaider的答案,但是拿出他的样本后我最终得到了这个对我的未绑定列表框有效:

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

答案 3 :(得分:0)

选择适当的

行来源类型=&gt;价值表

代码:

ListbName.ColumnCount = 2

ListbName.AddItem“value column1; value column2”

答案 4 :(得分:0)

首先为列表框

设置以下属性

行源类型:值列表 列数:2

假设列表框的名称是:listName 让我们假设你想在两个不同的列中添加两个不同的元素,并且在

中存储两个不同的字符串 然后

String1和String2遵循代码

代码:

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