5

我在访问中向列表框中的多个列添加值时遇到问题。我尝试过这样的解决方案:在具有多列的列表框中添加项目,并且:vba listbox multicolumn add [duplicate],但它不起作用。在我的情况下,列表框没有属性“列表”。我有编译错误:

在此处输入图像描述

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

在这种情况下我能做什么?

4

5 回答 5

6

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

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
于 2013-09-18T16:04:46.427 回答
2

首先为列表框设置以下属性

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

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

String1 和 String2 然后按照代码

代码:

Dim strName as string
strName=String1&";"&String2
Me.listName.addItem strName
于 2016-07-02T03:56:00.507 回答
1

我不太明白 Zaider 的回答,但是以他的样本我最终得到了这个对我未绑定的列表框有用的东西:

Me.listbox2.AddItem (Me.listbox1.Column(0) & ";" & Me.listbox1.Column(1))
于 2014-04-03T15:01:56.043 回答
1

选择财产

行源类型 => 值列表

代码 :

ListbName.ColumnCount=2

ListbName.AddItem "值列 1;值列 2"

于 2014-08-28T15:18:16.767 回答
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

如果您希望更改列表框的内容,您需要删除并重建列表框的记录源属性。以我的经验,当涉及多列时尝试修改单个行 - 永远行不通。

希望这会有所帮助。

于 2013-09-18T15:29:53.817 回答