我是Access VBA的新手,我陷入了“语言限制”的困境。我有一个项目集合,我想根据条件将其中的一些项目复制到新集合中,然后处理该新集合。但问题是,如果我从新集合中更改或删除任何内容,它也会在之前的集合中更改。但我不希望这种情况发生,因为它将在下一次迭代中再次使用。
我用来制作新系列的代码是:
Private Function ReturnSubCollection(TotalCollection As Collection, workIDs As String) As Collection
Dim collWorkIDs As Collection
Dim itemCount As Integer
Dim obj As Object
For itemCount = 1 To TotalCollection.count
If InStr(1, workIDs, TotalCollection.item(itemCount).Work_ID) > 0 Then
Set obj = TotalCollection.item(itemCount)
If collWorkIDs Is Nothing Then Set collWorkIDs = New Collection
collWorkIDs.Add obj
End If
Next
Set ReturnSubCollection = collWorkIDs
End Function
答案 0 :(得分:0)
这是VB的限制。优雅的解决方案是在this great answer中提到的项目对象中创建“memento”类。
一个简单的解决方法可能是:
假设您的项目类以三个值Work_ID,Work_Name,Work_Date开头。修改您的代码如下:
With TotalCollection.item(itemCount)
If InStr(1, workIDs, .Work_ID) > 0 Then
Set obj = New itemClass
obj.Work_ID = .Work_ID
obj.Work_Name = .Work_Name
obj.Work_Date = .Work_Date
'And so on, for any additional fields.
If collWorkIDs Is Nothing Then Set collWorkIDs = New Collection
collWorkIDs.Add obj
End If
End With
当然是原油。希望有效。