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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If..Then..End if 1

Status
Not open for further replies.

mf4

Programmer
Apr 9, 2001
2
US
Does anyone know if you do:

If i = 1 Then
msgbox "yes, i equals 1"
End If

you need an End If but if you did

If i = 1 Then msgbox "yes, i equals 1"

Then you dont need an End If... do you know if this is true?!
 
Well why don't you try it? :)

Actually it works, but IMHO it compromises readability of the code and should be avoided.

-Mats Hulten
 
I just found this out today, I normally use If..Then and close it with End if...I think i'll stick with it :) Thanks for the reply.
 
It's a leftover from older versions of BASIC. People used to code like this, to save space:

IF A=1 THEN LET A=A+1:LET AR=AR-1:LET CR=CR*0.1

Hard to read, isn't it? That's why the new style (with the End If on a separate line) is so much better.

If A = 1 Then
[tab]A = A + 1
[tab]AR = AR - 1
[tab]CR = CR * 0.1
End If

Chip H.
 
Hi,

If it is something like: if OverMarker then MovePlanetSchema(PlanetID)

then I think using the all on line format is fine and preferred. Afterall, why waste screen space by having an useless end if?

If it is a longer line going off the screen/page or many statements then use the block format.

Have a good one!
BK
 
I nice example is this :

Normal code :

If intX > 100 Then
intTest = 1
Else
intTest = 10
End If

Change to :

intTest = IIf(intX > 100, 1, 10)
Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
Yes, Eric -

Since Microsoft moved the IIF function into VB I use it a lot more often. If you recall, IIF(,,) used to be in a separate DLL that you had to ship back in VB3 (and I always forgot :-( )

IIF is also good when checking for NULL in database calls.

sMyVar = IIF(IsNull(adoRS("Field1"), "", adoRS("Field1"))
iMyVar = IIF(IsNull(adoRS("Field2"), 0, adoRS("Field2"))


Chip H.
 
chiph:

Thanks very much. I did not know that the IIF function was in VB. Use to use it in Access but never dawned on me it might be present/useful in VB. That is an excellent way to get rid of nulls.

Parke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top