VBA compile errors that make no sense

Anonymous
2024-05-10T23:02:00+00:00

Can someone explain to me why the VBA compiler gets upset in these two situations? The fixes look like they should be completely unnecessary.

With parentheses, it expects an expression, which is not applicable here since the default message box doesn't return a value. It's unintuitive since not using/needing parenthesis around function arguments is contrary to coding norms.

The compile error goes away if I put MsgBox("Select Employee") on a newline. It's not an issue with the initial If statement, which runs fine on its own. And I thought VBA doesn't have meaningful indentation, so why does a newline affect the following Elseif?

Microsoft 365 and Office | Access | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

17 answers

Sort by: Most helpful
  1. Duane Hookom 26,575 Reputation points Volunteer Moderator
    2024-05-11T00:20:22+00:00

    I never put anything other than comments after “Then”. Consider breaking up to multiple lines for better readability.

    0 comments No comments
  2. Tom van Stiphout 40,091 Reputation points MVP Volunteer Moderator
    2024-05-11T01:32:58+00:00

    Wow, you dont make it easy for someone to help you. How about posting the VBA text of the procedure in its entirety, and indicating the line where compile errors occur?

    0 comments No comments
  3. Anonymous
    2024-05-11T08:02:01+00:00

    Hi WCZH,

    Image

    If you have declared a Function, but you don't need the function result, you can call that function in the "Sub"- way, that is without the parentheses.

    I make quite a lot use of it.

    Image

    You can write If-statements in different ways.

    The classical example is:

    If (condition) Then
    
        aaa
    
        bbb
    
        ccc
    
    EndIf
    

    or if you wish with an ElseIf in between.

    But you can write this also in "shorthand":

    If (condition) Then aaa: bbb: ccc
    

    In this case VBA interprets the If-statement as completed (no EndIf necessary).

    However, you can continue with that If-statement like:

    If (condition) Then aaa: bbb: ccc
    
    Else: ddd: eee: fff
    

    or even

    If (condition) Then aaa: bbb: ccc
    
    Else: If (condition2) Then ddd: eee: fff Else: ggg: hhh: iii
    

    Personally I like the one-liners very much.

    Imb.

    0 comments No comments
  4. Anonymous
    2024-05-11T08:38:59+00:00

    The full procedure is irrelevant. All that matters is using parentheses breaks the MsgBox function only when a title argument is given (or maybe it's any optional argument; I didn't test the others). Why?

    In the second error, "ElseIf IsNull(TaskDate)" was highlighted by the debugger, which was invisible when I used the screen snip tool. I thought it was obvious enough given where I positioned the error message that says "Else without If." I also explained the issue pretty clearly and how adding a newline between "Then" and "MsgBox("Select Employee")" fixes the issue without adding or removing any code.

    0 comments No comments
  5. Anonymous
    2024-05-11T08:44:09+00:00

    It's not that I can call the function without parentheses. It's that I must call it without parentheses when I add a title argument. Why? That doesn't make sense to me. Trying to understand why VBA has this nuance or if it's a bug in the compiler/language itself.

    Also, with the If-Else block, I had multiple ElseIf statements, but only the first one caused a compile error until I separated the first If statement onto 2 lines. The remaining ElseIf statements are all on one line and don't cause any errors. Again, very strange behavior.

    0 comments No comments