2015-11-06 51 views
1

我试图在使用VBA进行访问时将多列项添加到列表框中。不能为我的生活制定出如何去做。在互联网上似乎有多种方式,他们都没有为我工作。访问VBA - 添加具有多列的行

下面是一个例子:

Sub AddMultipleColumn() 
'Add multiple Columns to a listbox 
ListBox1.Clear 'Make sure the Listbox is empty 
ListBox1.ColumnCount = 3 'Set the column Amount 
'Fill the Listbox 
ListBox1.AddItem "Row Number 1" 'Additem creates a new row 
ListBox1.List(0, 1) = "Column Number 2" 'List(x,y) X is the row number, Y the column number 
ListBox1.List(0, 2) = "Column Number 3" 
ListBox1.List(0, 3) = "Column Number 4" 
End Sub 

http://visiblevisual.com/jupgrade/index.php/general-net/77-multiple-columns

这里是我的代码(已配置有三列):

lst_TemplateEditorList.AddItem "1" 
lst_TemplateEditorList.List(0, 0) = "1" 
lst_TemplateEditorList.List(0, 1) = "2" 
lst_TemplateEditorList.List(0, 2) = "3" 

但我得到的编译错误“方法或数据成员找不到“on”lst_TemplateEditorList.List“,因为没有List方法/属性。

我发现的东西,用“列”,而不是“名单”,但没有工作,和一些没有大吵“的AddItem”,这将导致错误太:

lst_TemplateEditorList.AddItem 
lst_TemplateEditorList.Column(0, 0) = "1" 
lst_TemplateEditorList.Column(0, 1) = "2" 
lst_TemplateEditorList.Column(0, 2) = "3" 

但最似乎表明第一应该工作,如:

Adding items in a Listbox with multiple columns

vba listbox multicolumn add

现在看来似乎应该是这么简单......但我很难过。谁能告诉我我做错了什么?

感谢

在Access

回答

1

列表框有一组不同的属性/方法比其他Office程序列表框(在那里,他们属于“Microsoft窗体”对象模型)

https://msdn.microsoft.com/en-us/library/office/ff195480.aspx

在访问您只需添加多列数据,就像您在RowSource属性中为RowSourceType = Value List所做的那样,以分号分隔。

With Me.lst_TemplateEditorList 
    .AddItem "1;2;3" ' first row 
    .AddItem "4;5;6" ' second row 
End With 

编辑

在Access 2010开发人员帮助增加了混乱:如果搜索List,你得到的形式 -Listbox和它的属性,包括.List。你必须非常仔细地注意到你已经离开了Access领域并且进入了Microsoft Forms领域。德国访问,但你明白了。

enter image description here

+0

谢谢!这就是为什么我得到这么多其他结果。 – Joe

+0

@Joe:确实令人困惑,请参阅编辑。 :) – Andre