2017-11-24 4 views
0

컨텍스트 : Microsoft Access에서 동적으로 Application.Run을 사용하여 함수를 호출하고 매개 변수를 전달하려고합니다. 내 코드는 다음과 엑셀 2013에서 작동하지만 Access에서 작동하지 않습니다 2013Access VBA 컴파일 오류 잘못된 ParamArray 사용

코드 :

Public Sub Test() 
    Call MethodDynamically("MethodToBeCalled1", "This", "works") 
    Call MethodDynamically("MethodToBeCalled2", "This", "works", "too") 
    Call MethodDynamically("MethodToBeCalled3", "This", "works", "too", "as well") 
End Sub 

Public Sub MethodDynamically(MethodName As String, ParamArray Params() as Variant) 
    Application.Run MethodName, Params 
End Sub 
Public Sub MethodToBeCalled1(Params As Variant) 
    Debug.Print Params(0) & " " & Params(1) 
End Sub 
Public Sub MethodToBeCalled2(Params As Variant) 
    Debug.Print Params(0) & " " & Params(1) & " " & Params(2) 
End Sub 
Public Sub MethodToBeCalled3(Params As Variant) 
    Debug.Print Params(0) & " " & Params(1) & " " & Params(2) & " " & Params(3) 
End Sub 

반환 출력 :

This works 
This works too 
This works too as well 

오류 : Access에서 그러나의 동일한 코드는 오류 : Compile error: Invalid ParamArray use을 제공합니다.

Access VBA에이 코드를 조정하는 방법이 있습니까?

답변

1

ParamArray를 다른 함수에 직접 전달할 수 없습니다.

는 다음과 같은 시도 :

Public Sub MethodDynamically(MethodName As String, ParamArray Params() as Variant) 
    Dim MyArray As Variant 
    MyArray = Params 
    Application.Run MethodName, MyArray 
End Sub 
+0

큰 감사 에릭. 호기심에서 벗어난 이유는 Excel에서 작동하고 Access에서는 작동하지 않는 이유는 무엇입니까? –

+0

Excel에서'Application.Run "MyFunction()"''MyFunction'을 두 번 실행하면 (Access에서 런타임 오류가 발생 함) Excel에서'Application.Run'에 대해 여러 가지 이상한 버그가있는 것을 발견했습니다. 내가 말할 수있는 것은 afaik는 작동하지 않아야한다는 것입니다. –