A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

If, Then, Else



 
 
Thread Tools Display Modes
  #1  
Old November 5th, 2009, 08:27 PM posted to microsoft.public.access.forms
ann
external usenet poster
 
Posts: 462
Default If, Then, Else

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

  #2  
Old November 5th, 2009, 08:32 PM posted to microsoft.public.access.forms
Dirk Goldgar
external usenet poster
 
Posts: 2,529
Default If, Then, Else

"Ann" wrote in message
...
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.

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

(please reply to the newsgroup)

  #3  
Old November 5th, 2009, 09:19 PM posted to microsoft.public.access.forms
ann
external usenet poster
 
Posts: 462
Default If, Then, Else

That was it! Thank you.

"Dirk Goldgar" wrote:

"Ann" wrote in message
...
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.

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

(please reply to the newsgroup)

  #4  
Old November 5th, 2009, 10:36 PM posted to microsoft.public.access.forms
John W. Vinson
external usenet poster
 
Posts: 18,261
Default If, Then, Else

On Thu, 5 Nov 2009 11:27:01 -0800, Ann wrote:

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.
--

John W. Vinson [MVP]
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:43 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.