Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax for setting up If and If then else statement

Status
Not open for further replies.

TryTryAgain

Programmer
Mar 17, 2005
15
US
I want to set up a VBA statement checking multiple conditions to be true, but can't get the syntax right.

If <expression> and <expression> is true then <execute statements>
else if <expression> and <expression> is true then <execute statements> else
<execute statements if the above don't evaluate true>
end if

Thanks for any assistance you can provide!
 
Code:
If (<expression>) And (<expression>) Then
   <execute statements>
ElseIf (<expression>) And (<expression>) Then
   <execute statements>
Else
   <execute statements>
End If
 
Here's the code I'm trying to execute:

If rsin.Fields![land_acqrowact] > 0 Then rsout.Fields![acqrowvarpct] = -1
ElseIf (rsin.Fields![land_acqrowbud]) > 0 And (rsout.Fields![land_acqrowact] = 0) Then
rsout.Fields![acqrowvarpct] = 1
ElseIf (rsin.Fields![land_acqrowbud] > rsin.Fields![land_acqrowact]) Then
rsout.Fields![acqrowvarpct] = rsout.Fields![acqrowVarDollar] / rsin.Fields![land_acqrowbud]
Else: rsout.Fields![acqrowvarpct] = rsout.Fields![acqrowVarDollar] / rsin.Fields![land_acqrowact]
End If

I am getting a "Compile Error: Else without if" message and the (rsout.Fields![land_acqrowact] = 0) is highlighted.
 
You have to split your first line...

Code:
If rsin.Fields![land_acqrowact] > 0 Then 
    rsout.Fields![acqrowvarpct] = -1
ElseIf (rsin.Fields![land_acqrowbud]) > 0 And (rsout.Fields![land_acqrowact] = 0) Then
        rsout.Fields![acqrowvarpct] = 1
ElseIf (rsin.Fields![land_acqrowbud] > rsin.Fields![land_acqrowact]) Then
        rsout.Fields![acqrowvarpct] = rsout.Fields![acqrowVarDollar] / rsin.Fields![land_acqrowbud]
Else
   rsout.Fields![acqrowvarpct] = rsout.Fields![acqrowVarDollar] / rsin.Fields![land_acqrowact]
End If
 
This
Code:
If rsin.Fields![land_acqrowact] > 0 Then rsout.Fields![acqrowvarpct] = -1
MUST be
Code:
If rsin.Fields![land_acqrowact] > 0 Then 
   rsout.Fields![acqrowvarpct] = -1
because the one-line version is taken to be a single self-contained statement and not the start of an if-group as the second one is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top