4

Can anyone explain to me why, in VBA code for a button on a form in MS-Access 2007, this gives the error "Compile error: Type mismatch: array or user-defined type expected"

Private Sub Button_Click()
    Dim Arr() As Integer
    Foo (Arr())
End Sub

Private Sub Foo(Arr() As Integer)
    Me.Field.Value = "Foo"
End Sub

but this compiles fine?

Private Sub Button_Click()
    Dim Dummy
    Dim Arr() As Integer
    Dummy = Bar (Arr())
End Sub

Private Function Bar(Arr() As Integer)
    Me.Field.Value = "Bar"
End Function

The function/subroutine I'm writing doesn't return anything, but I can't get it to compile unless I assign the return value of my function call to a dummy variable, like in the Bar function above.

1 Answer 1

5

If you wish to use brackets around the parameter, you must use Call, so skip the brackets:

Private Sub Button_Click()
    Dim Arr() As Integer
    ''No brackets
    Foo Arr()
    ''Or 
    ''Call Foo(Arr())
End Sub

Private Sub Foo(Arr() As Integer)
    Me.Field.Value = "Foo"
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Seems I need to read up a little more on the ways to call subroutines. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.