Compile error: Else without If (1 Viewer)

tho28199

Registered User.
Local time
Today, 01:48
Joined
Jan 25, 2007
Messages
28
I simply cannot see what I have done wrong here:

Private Sub Form_Load()
txtCheck.Value = strCheckMark

If txtCheck.Value = "A" Then _
lblTitle.Caption = "Amended Entries Completed - Change Flag Form"
ElseIf txtCheck.Value = "N" Then _
lblTitle.Caption = "Entry On File - Audit Input Form"
ElseIf txtCheck.Value = "Y" Then _
lblTitle.Caption = "Entries for Amendment - Change Flag Form"
Else
lblTitle.Caption = "This did not work"
End If

The first ElseIf is hilited on error.

I tried this on both the Form - Open and Form - Load action, and neither works.

Help, please!
 

boblarson

Smeghead
Local time
Yesterday, 22:48
Joined
Jan 12, 2001
Messages
32,059
Go to a Select Case Statement instead:

Code:
txtCheck = strCheckMark

Select Case txtCheck
	Case "A"
		lblTitle.Caption = "Amended Entries Completed - Change Flag Form"
	Case "N"
		lblTitle.Caption = "Entry On File - Audit Input Form"
	Case "Y"
		lblTitle.Caption = "Entries for Amendment - Change Flag Form"
	Case Else
		lblTitle.Caption = "This did not work"
End Select

By the way, you don't need to use .Value as .Value is the default for text boxes.
 

tho28199

Registered User.
Local time
Today, 01:48
Joined
Jan 25, 2007
Messages
28
Hi Bob,

That works... now I just need to figure out why it is not picking up the public variable.

Thanks for the quick reply.
 

boblarson

Smeghead
Local time
Yesterday, 22:48
Joined
Jan 12, 2001
Messages
32,059
For it to be public, you need to declare it in the general declarations section of a STANDARD MODULE (not a form's module), and you have to declare it as
Public strCheckMark As String
 

DJkarl

Registered User.
Local time
Today, 00:48
Joined
Mar 16, 2007
Messages
1,028
I simply cannot see what I have done wrong here:

Private Sub Form_Load()
txtCheck.Value = strCheckMark

If txtCheck.Value = "A" Then _
lblTitle.Caption = "Amended Entries Completed - Change Flag Form"
ElseIf txtCheck.Value = "N" Then _
lblTitle.Caption = "Entry On File - Audit Input Form"
ElseIf txtCheck.Value = "Y" Then _
lblTitle.Caption = "Entries for Amendment - Change Flag Form"
Else
lblTitle.Caption = "This did not work"
End If

The first ElseIf is hilited on error.

I tried this on both the Form - Open and Form - Load action, and neither works.

Help, please!

Just in case you're wondering why it didn't work, get rid of the line continuation markers from your If statement and it will compile just fine.
 

phatnq2002

Registered User.
Local time
Today, 12:48
Joined
Mar 28, 2007
Messages
23
Dear tho28199

If txtCheck.Value = "A" Then _
lblTitle.Caption = "Amended Entries Completed - Change Flag Form"

You use the character _ after Then, it says that the statement behind is an element of the If, but break to new line.

So the translator do it like an only statement. The error occurs for the next ElseIf not for If.
 

DJkarl

Registered User.
Local time
Today, 00:48
Joined
Mar 16, 2007
Messages
1,028
Dear tho28199

If txtCheck.Value = "A" Then _
lblTitle.Caption = "Amended Entries Completed - Change Flag Form"

You use the character _ after Then, it says that the statement behind is an element of the If, but break to new line.

So the translator do it like an only statement. The error occurs for the next ElseIf not for If.

The _ character in this case is telling VBA that this is an if ONLY statement, not an If, Elseif, Elseif... etc statement. You can only use the _ character with an if statement if there is a single outcome, if you are proceeding to do other Elseif checks use the code as it appears below. The code below does what you're looking for and does not get a compile error.

Code:
If txtCheck.Value = "A" Then 
lblTitle.Caption = "Amended Entries Completed - Change Flag Form"
ElseIf txtCheck.Value = "N" Then 
lblTitle.Caption = "Entry On File - Audit Input Form"
ElseIf txtCheck.Value = "Y" Then 
lblTitle.Caption = "Entries for Amendment - Change Flag Form"
Else
lblTitle.Caption = "This did not work"
End If
 

tho28199

Registered User.
Local time
Today, 01:48
Joined
Jan 25, 2007
Messages
28
DJkarl & Phatnq2002... thanks for the replies. I switched to Bob's solution of using the Select Case, which worked perfectly, but I appreciate the answers to what I was doing wrong with the If Then Else syntax.
 

Users who are viewing this thread

Top Bottom