If...then

latex88

Registered User.
Local time
Today, 14:57
Joined
Jul 10, 2003
Messages
198
I am having the toughest time with the if...then statement. It seems simple enough but I always get compiling error when I include "Else" in my statements and I don't understand why. Please help. What is wrong with the below statement?

Private Sub CmdFinish_Click()
If IsNull(txtOrderType) = True Then MsgBox ("You must select an Order Type First")
Else
Call FinishOrder
End If
End Sub
 
The problem is that you are mixing two possible ways of using the If...then structure.

The first is this:

If IsNull(txtOrderType) = True Then MsgBox ("You must select an Order Type First")

That syntax is a complete If...then statement that you can use if you do not need an Else clause.

Try your code like this:

Private Sub CmdFinish_Click()
If IsNull(txtOrderType) = True Then
  MsgBox ("You must select an Order Type First")
Else
  Call FinishOrder
End If
End Sub
 
Thanks, when I copied your code, it works perfectly. Please just help me understand better. I didn't quite understand exactly what you said, but from looking at the difference between your code and mine is that the line, MsgBox ("You must select an Order Type First") is below the "Then" word.

I guess my confusion is that I don't really see the differece how the code is interpret when it is written in one continuous line as I have versus broken it down as you did.
 
No problem. There are two syntaxes for the If...Then structure:
If condition Then do this ELSE do this
or this:

If condition Then
  do these commands
Else
  do these commands
End If


The big difference, besides having the statements on multiples lines, is that with the second structure, you can have multiple statements execute if the condition is true or false. Your original code could've been written all on one line, but it makes it harder to read.
 

Users who are viewing this thread

Back
Top Bottom