Else without if

Wiz47

Learning by inches ...
Local time
Today, 03:03
Joined
Nov 30, 2006
Messages
274
It seems to me that my syntax is correct. Why the error?

Compile error: Else without if


Private Sub Command3_Click()
Dim strForm As String

If Me.CboForm = "Form 1" Then strForm = "Form1"
Else
strForm = "Form2"
End If

DoCmd.OpenForm strForm
End Sub
 
Yeah, I hate VBA, its so finicky about spacing for no reason. Try this:

Private Sub Command3_Click()
Dim strForm As String

If Me.CboForm = "Form 1" Then strForm = "Form1" Else strForm = "Form2"

DoCmd.OpenForm strForm
End Sub
 
Change your code to the following and retest....

Code:
Private Sub Command3_Click()

Dim strForm As String

If Me.CboForm = "Form 1" Then
  strForm = "Form1"
Else
  strForm = "Form2"
End If

DoCmd.OpenForm strForm

End Sub
 
Yeah, I hate VBA, its so finicky for no reason.

It is not "no reason".

The OP had tried to put the THEN operation right behind THEN. It belongs on the next line.

No need to do a knee-jerk reaction and revert to even uglier code! :eek:
 
Uglier but equally correct syntax wise.

If...Then...Else Statement (Visual Basic)

Code:
' Multiple-line syntax:
If condition [ Then ]
  [ statements ]
[ ElseIf elseifcondition [ Then ]
  [ elseifstatements ] ]
[ Else
  [ elsestatements ] ]
End If

' Single-line syntax:
If condition Then [ statements ] [ Else [ elsestatements ] ]
 
It is not "no reason".

The OP had tried to put the THEN operation right behind THEN. It belongs on the next line.

No need to do a knee-jerk reaction and revert to even uglier code! :eek:

You're correct. I needed to block it out properly. That's what happens when you work on code all night. Thanks.
 
Thanks to all in the thread. I just had it blocked out incorrectly.
 
The OP had tried to put the THEN operation right behind THEN. It belongs on the next line.

I stand by my accusation. White space is white space is white space. If the next characters are 'THEN' (no matter how far down the page they are), it needs to be processed correctly.

I also submit that my code suggestion isn't ugly, in fact its the cleanest, correct version offered:

If Me.CboForm = "Form 1" Then strForm = "Form1" Else strForm = "Form2"
 

Users who are viewing this thread

Back
Top Bottom