我在 Access 2013 中使用 VBA。
在常规模块中有 2 个过程,RunProc()
并且PopulateCollection()
执行时RunProc
,它调用PopulateCollection
其中传递的参数是一个名为的集合实例MyCol
。
PopulateCollection
添加 3 个项目,然后RunProc
通过迭代集合继续。
我的问题/问题是这样的:
我希望参数不被MyCol
. 实现此目的的正确方法是什么?RunProc
PopulateCollection
为什么PopulateCollection
同时填充参数和参数?
' --------Module1------------------
Option Compare Database
Option Explicit
Dim i As Integer
Dim MyCol As VBA.Collection
Sub RunProc()
Set MyCol = New VBA.Collection
PopulateCollection MyCol
For i = 1 To MyCol.Count
Debug.Print MyCol.Item(i)
Next i
End Sub
Function PopulateCollection(ByRef pMyCol As VBA.Collection)
For i = 1 To 3
pMyCol.Add "Item" & i
Next i
End Function
这是问我问题的另一种方式:
Option Compare Database
Option Explicit
Sub Proc1()
Dim myInt As Integer
myInt = 1
Proc2 myInt
Debug.Print myInt
myInt = 1
Proc3 myInt
Debug.Print myInt
End Sub
Sub Proc2(ByVal pmyInt)
pmyInt = pmyInt + 1
Debug.Print pmyInt
End Sub
Sub Proc3(ByRef pmyInt)
pmyInt = pmyInt + 1
Debug.Print pmyInt
End Sub
'Consider the 3 procedures: Proc1, Proc2, Proc3
'Proc1 calls Proc2 and Proc3
'The only difference between Proc2 and Proc3 is that
'the parameter pmyInt is called differently: ByVal vs ByRef
'Proc2 does not change the argument myInt
'Proc3 does change the argument myInt
'The root of my question is why the same behavior is
'not exhibited with an Object (VBA.Collection)
'Assuming I wanted to not have the original Collection altered
'how would I proceed?