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. Anonymous
    2024-05-11T09:22:18+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.

    Hi WCZH.

    If you call a Function with parentheses you MUST specify a variable (or control) to assign the result to.

    For the second part it is important to see all the lines in the If-Else block, becuase the exact punctuation becomes important.

    Perhaps I am - after some 40 years - used to how to prevent compile errors in If-Else constructions, but I don't experience it as strange behaviour.

    The important thing is to write the instructions so that the compiler can unambiguous interpret the lines.

    Imb.

    0 comments No comments
  2. Anonymous
    2024-05-13T01:22:07+00:00

    As Tom pointed out, show the full code.

    I would also like to point out these 2 lines. (since we don't know what's the beginning of the code)

    I can't figure out why VBA didn't throw any error here

    If IsNull(EmployeeID) Then MsgBox ("Select Employee")
    

    Correct example is

    If IsNull(EmployeeID) Then MsgBox "Select Employee" 'you are using Default message box here

    This code end here. Not sure why the the next line "ElseIf" comes from.

    No idea why the line

    ElseIf IsNull(TaskDate) Then MsgBox "Select Date", ,
    

    Since the above line is a one command line, the "ElseIf" wouldn't work here. So what I can see is that you'll need to a lot of code syntax corrections.

    0 comments No comments
  3. George Hepworth 22,220 Reputation points Volunteer Moderator
    2024-05-13T13:15:01+00:00

    Hm, while you insist on getting help, you also resist requests for more information that would help someone provide that help?

    How does that improve the odds of getting usable help?

    0 comments No comments
  4. Anonymous
    2024-05-13T15:30:56+00:00

    If you call a Function with parentheses you MUST specify a variable (or control) to assign the result to.

    That's just weird, but explains the problem. Is there any other programming language that's like this? Parentheses are not normally optional (or rather exhibit different behavior) in function calls

    0 comments No comments
  5. Anonymous
    2024-05-13T16:05:11+00:00

    The code looks like this:

    Private Sub SubmitBtn_Click()

    If IsNull([field1]) Then

    MsgBox "[message]", , "[title]"

    ElseIf IsNull([field2]) Then MsgBox "[message]", , "[title]"

    [several more ElseIf statements with the exact same structure]

    ElseIf MsgBox("Enter record?", vbYesNo, "Submit") = vbYes Then

    DoCmd.RunSQL([SQL insert statement with all the above fields concatenated])

    End If

    End Sub

    I get a compile error ('Else without If') if I put the first If-Then statement on one line, but not if I have no ElseIf statements. Why does the newline matter? Why does the compiler not recognize the preceding If statement if it's on one line?

    You guys keep fretting over the code but it's just those few lines that matter. Do you see now?

    0 comments No comments