If,Then, Else

  • Thread starter Thread starter Alansa59
  • Start date Start date
A

Alansa59

Could someone please help me with the correct syntax for the "IF, Then: Else:
" command
 
Alansa59 said:
Could someone please help me with the correct syntax for the "IF, Then:
Else:
" command

Sure. Let's say you want to do something if a file doesn't exist. It could
be coded like this

If Dir(MyFileName) = "" Then
'This part executes when the above condition is True
MsgBox "Doesn't exist"
Else
'This part executes when the above condition is False
MsgBox "Exists"
End If

This could also be coded on one line:

If Dir(MyFileName) = "" Then MsgBox "Doesn't exist" Else MsgBox "Exists"

Note that when you use the one-line syntax you don't provide an End If.
 
Can you be a bit more specific? In general you could either have....

If <Condition> then <Action>

or

If <Condition> then
<Action>
else
<Action>
end if

You can nest your IF statements as well.

Try using the Access Help function - this describes it very well.
 
I have a form that all the text boxes need to be filled in before it is saved
using a button "SUBMIT/CLOSE".
Below is the code I attempted to use and it comes up with "Error, Else
without If

If IsNull(Opened_Date) And IsNull(Start_Date) Then cmdSUBMIT_CLOSE.Enabled =
True

Else: cmdSUBMIT_CLOSE = False
End If
 
I have a form that all the text boxes need to be filled in before it is saved
using a button "SUBMIT/CLOSE".
Below is the code I attempted to use and it comes up with "Error, Else
without If

If IsNull(Opened_Date) And IsNull(Start_Date) Then cmdSUBMIT_CLOSE.Enabled =
True

Else: cmdSUBMIT_CLOSE = False
End If
 
Alansa59 said:
I have a form that all the text boxes need to be filled in before it is
saved
using a button "SUBMIT/CLOSE".
Below is the code I attempted to use and it comes up with "Error, Else
without If

If IsNull(Opened_Date) And IsNull(Start_Date) Then cmdSUBMIT_CLOSE.Enabled
=
True

Else: cmdSUBMIT_CLOSE = False
End If

Ok, that should read:

If IsNull(Opened_Date) And IsNull(Start_Date) Then
cmdSUBMIT_CLOSE.Enabled = True
Else
cmdSUBMIT_CLOSE.Enabled = False
End If

The reason you got an Else without If error is because your first code line
is the one-line syntax I mentioned previously. So the command ended, then
the next thing to come along is an Else statement, but it has no
corresponding If because the one-liner ended.

Incidentally, the whole thing could be reduced to:

cmdSUBMIT_CLOSE.Enabled = (IsNull(Opened_Date) And IsNull(Start_Date))

because the expression (the part on the right of the equals sign) will
return True or False, which is exactly what you require to set the enabled
property.
 
I have a form that all the text boxes need to be filled in before it is saved
using a button "SUBMIT/CLOSE".
Below is the code I attempted to use and it comes up with "Error, Else
without If

If IsNull(Opened_Date) And IsNull(Start_Date) Then cmdSUBMIT_CLOSE.Enabled =
True

Else: cmdSUBMIT_CLOSE = False
End If

Reread the Help message for If in the VBA online help. It's pretty clear and
it doesn't resemble what you've done much at all.

The correct syntax is

IF <true or false expression> Then
<statements to be executed if it is true>
Else
<statements to be executed if it is false>
End If

In your case:

If IsNull(Opened_Date) And IsNull(Start_Date) Then
cmdSUBMIT_CLOSE.Enabled = True
Else
cmdSUBMIT_CLOSE.Enabled = False
End If

I'm guessing that you want to set the Enabled property in the false branch,
rather than the value of the control.

In this case, it's actually quite possible to do without the IF entirely: you
just want to set the Enabled property to TRUE or FALSE, and you have an
expression which is TRUE or FALSE:

Me!cmdSUBMIT_CLOSE.Enabled = (IsNull(Opened_Date) AND IsNull(START_DATE))

This will enable the control cmdSUBMIT_CLOSE if both OPENED_DATE and
START_DATE are NULL, and disable cmdSUBMIT_CLOSE if either or both controls
contain data. (I suspect that isn't actually what you want but my crystal ball
is cloudy tonight...)
 
Stuart thanks a ton . I have another problem. I would like to use that same
control to generate an Outlook email and attach the form that the control is
part of, to the email. Any ideas?
 
Alansa59 said:
Stuart thanks a ton . I have another problem. I would like to use that
same
control to generate an Outlook email and attach the form that the control
is
part of, to the email. Any ideas?

Unfortunately I've not worked much with outlook. However there are others on
here that will no doubt help. If you get no reply to this, try re-posting as
a new thread.
 
Back
Top