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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18870600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:38:29  来源:igfitidea点击:

Adding values multiple columns to listbox in form access vba

vbams-accessaccess-vba

提问by Beacze

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 columnsand this: vba listbox multicolumn add [duplicate], but it doesn't work. Listbox in my case hasn't a property "List". I have compile error:

我在访问列表框中向多列添加值时遇到问题。我尝试过这样的解决方案:在具有多列的列表框中添加项目和这个: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

What can I do in this situation?

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

回答by Zaider

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

回答by Kristian

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

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

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

回答by KONG

select propety

选择属性

Row Source Type => Value List

行源类型 => 值列表

Code :

代码 :

ListbName.ColumnCount=2

ListbName.ColumnCount=2

ListbName.AddItem "value column1;value column2"

ListbName.AddItem "value column1;value column2"

回答by Sudeep kumar naru

Firstly set the following properties for list box

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

Row source type: value list Column count: 2

行源类型:值列表列数: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

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

String1 and String2 then follow the code

String1 和 String2 然后按照代码

Code:

代码:

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

回答by DHW

Here's a full example of how you can add multiple columns to a listbox.

这是一个完整的示例,说明如何将多列添加到列表框。

SQL to create the table:

创建表的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')";

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.

希望这会有所帮助。