访问VBA集合无法正常显示

时间:2015-11-06 17:06:06

标签: vba collections access-vba

这个想法很简单,填充并显示具有不同值的集合。但是它只显示最后一个值。我不确定问题是在填充子或显示部分。使用访问VBA

public sub populate(list as Collection)

dim i as integer
for i = 0 to 100
   dim e as new Numbers
   e.addition = i+i
   e.multiply = i*i
   list.add e
next
end sub

称呼它:

    Dim test As Numbers
    Dim list as new Collection
    Call populate(list)
    For Each test In list

        Debug.Print test.addition & ", " & test.multiply      

    Next test

这仅显示列表集合中的LAST Numbers-Object 100次。

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

据我所知,Dim e as new Numbers每次都不创建新元素,定义是函数范围的。所以你每次只编辑相同的Numbers e。将Dim e as Numbers移出循环并在循环中使用Set e as new Numbers应该可以解决问题:

(未经测试)

public sub populate(list as Collection)

dim i as integer
dim e as Numbers
for i = 0 to 100
   Set e = new Numbers
   e.addition = i+i
   e.multiply = i*i
   list.add e
next
end sub