Why not End if required with If then

G

Guest

End if not needed with If in the following code. Could someone pls explain
about why it works this way.

My app has the public function IsLoaded and a sub for the OnClose event of a
form.

I have copied from Northwind, for the sub OnClose
The If statement is all on one line, and it doesn’t need an ‘End if’.
My question is why isn’t the ‘End if’ needed, or why set the code this way
instead of the more common:
If then
End if


Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet view.
Dim oAccessObject As AccessObject

Set oAccessObject = CurrentProject.AllForms(strFormName)
If oAccessObject.IsLoaded Then
If oAccessObject.CurrentView <> acCurViewDesign Then
IsLoaded = True
End If
End If
End Function

Private Sub Form_Close()
If IsLoaded("frmViewTracks") Then DoCmd.Close acForm, "frmViewTracks"
 
J

James Hahn

There are two different forms of the IF statement - single line and multiple
line. You choose the one most suited to the purpose. Language syntax is
often arbitrary.
1. If ... Then ....
2. If ... Then
...
End If
 
A

Andi Mayer

End if not needed with If in the following code. Could someone pls explain
about why it works this way.
if you have only one line you can spare the "End If", the compiler is
doing it for

You can write on a single line:

If True then doThis Else doThat


If you expect an answer to a personal mail, add the word "manfred" to the first 10 lines in the message
MW
 
A

Andi Mayer

if you have only one line you can spare the "End If", the compiler is
doing it for

You can write on a single line:

If True then doThis Else doThat
I forgot a third variant:

value=iif(True, "thisIsTrue","ThisIsFalse")

Take care: this form is evaluating both arguments, therefore use it
only with constants

this will give you a runtime error:
Dim Five as Long, Zero as Long, Value as Long
Five=5
Zero=0

value= iif(True,10/Five,10/Zero)
If you expect an answer to a personal mail, add the word "manfred" to the first 10 lines in the message
MW
 
M

Mike Painter

Liz said:
End if not needed with If in the following code. Could someone pls
explain about why it works this way.
If IsLoaded("frmViewTracks") Then DoCmd.Close acForm

I suspect it works because nobody ever took it out.

The original BASIC and all of the early version of mbasic had one line IF
statements,period.
No end if was needed because it all had to be on one line.
IF X = Y THEN GOTO 500 was about the best you could do.
(On early 4k machines this would be IFX=YTHENGOTO500 since a space took up a
byte)
 

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