If, Then, Else

A

Ann

Hi,

I'm a bit confused and hope someone can help. I'm just learning code and
can't figure out why I keep getting an compile error; Else Without If on the
following code. Thanks in advance.

Private Sub Command930_Click()
On Error GoTo Err_Command930_Click

Dim stDocName As String
Dim stLinkCriteria As String

If SSN.Value = -1 And ysnName.Value = -1 _
And Recipient_of_PHI = "Entity" Or Recipient_of_PHI = "Person" Then _
stDocName = "frmAction1-5"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
stDocName = "frmAction5"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If

Exit_Command930_Click:
Exit Sub

Err_Command930_Click:
MsgBox Err.Description
Resume Exit_Command930_Click

End Sub
 
D

Dirk Goldgar

Ann said:
Hi,

I'm a bit confused and hope someone can help. I'm just learning code and
can't figure out why I keep getting an compile error; Else Without If on
the
following code. Thanks in advance.

Private Sub Command930_Click()
On Error GoTo Err_Command930_Click

Dim stDocName As String
Dim stLinkCriteria As String

If SSN.Value = -1 And ysnName.Value = -1 _
And Recipient_of_PHI = "Entity" Or Recipient_of_PHI = "Person" Then _
stDocName = "frmAction1-5"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
stDocName = "frmAction5"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If

Exit_Command930_Click:
Exit Sub

Err_Command930_Click:
MsgBox Err.Description
Resume Exit_Command930_Click

End Sub


I believe it's because you have a line-continuation character after the
keyword Then:
And Recipient_of_PHI = "Entity" Or Recipient_of_PHI = "Person" Then _

It should just be

And Recipient_of_PHI = "Entity" Or Recipient_of_PHI = "Person" Then


For a block If, the statements that follow the If are not continuations of
the If statement.
 
A

Ann

That was it! Thank you.

Dirk Goldgar said:
I believe it's because you have a line-continuation character after the
keyword Then:


It should just be

And Recipient_of_PHI = "Entity" Or Recipient_of_PHI = "Person" Then


For a block If, the statements that follow the If are not continuations of
the If statement.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 
J

John W. Vinson

Hi,

I'm a bit confused and hope someone can help. I'm just learning code and
can't figure out why I keep getting an compile error; Else Without If on the
following code. Thanks in advance.

Private Sub Command930_Click()
On Error GoTo Err_Command930_Click

Dim stDocName As String
Dim stLinkCriteria As String

If SSN.Value = -1 And ysnName.Value = -1 _
And Recipient_of_PHI = "Entity" Or Recipient_of_PHI = "Person" Then _
stDocName = "frmAction1-5"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
stDocName = "frmAction5"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If

Exit_Command930_Click:
Exit Sub

Err_Command930_Click:
MsgBox Err.Description
Resume Exit_Command930_Click

End Sub

You need to remove the underscore line continuation character after the Then.

The If statement has two syntaxes: the more common

If <Condition> Then
<statements if true>
Else
<statements if false>
End If

and the "one line if"

If <Condition> Then <statement if true>

Your lines

If SSN.Value = -1 And ysnName.Value = -1 _
And Recipient_of_PHI = "Entity" Or Recipient_of_PHI = "Person" Then _
stDocName = "frmAction1-5"

are being read as a single line - since you have the line continuation
characters; the line

stDocName = "frmAction1-5"

is the end of the entire IF block, as far as Access is concerned, so when you
get to the ELSE further down, it's not seen as being part of an IF block.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top