If without end if odity

SmallTime

Registered User.
Local time
Today, 04:37
Joined
Mar 24, 2011
Messages
246
This isn't a problem just a question out of curiosity

why doesn't this throw an error

If Me.GenericForm.Visible = True Then Me.GenericForm.Visible = False

whereas this demands an End if

If Me.GenericForm.Visible = True Then
Me.GenericForm.Visible = False

How odd.

SmallTime
 
Not odd at all. If you look in VBA help at If/Then/Else, you'll see there's a one-line format and a block format. The block format requires the End If, the one-line does not. With the one-line format it knows you're done when the line ends. With the block format, you have to tell it when you're done. It can't possibly know which/how many of the following lines you want within the block.
 
Ah I knew there was sensible explanation. I punched it in by mistake and then realised that the red sqiggly hadn't appeared.

Maybe I'll go back and change all those block IF's that I've used when a one-liner could have done the trick.

Cheers
SmallTime
 
Just a word of caution when using the oneliners - I don't know if this happens all of the time but it has always occurred to me so take it as you wish -

I've found that if you use a oneliner in a procedure, then trying to use a block as well, especially right after a oneliner, generates the error If with no End If, even though you have it on the block. So, just a forewarning.
 
Hmm never had that, not that I used 1 liners very often.

Brian
 
Just read the access Hell-p for the If statement.

Lucky I didn't read this before learning the hard way otherwise I'd still be sitting here trying to decipher what it all means.

A block form If statement must be the first statement on a line. The Else, ElseIf, and End If parts of the statement can have only a line number or line label preceding them. The block If must end with an End If statement.

To determine whether or not a statement is a block If, examine what follows the Then keyword. If anything other than a comment appears after Then on the same line, the statement is treated as a single-line If statement.


IF the help was easier to understand THEN there wouldn't be such a need for forums.

Does this qualify as a one-liner? (|;-)

SmallTime
 
You could also have done this legally (I think)

If Me.GenericForm.Visible = True Then _
Me.GenericForm.Visible = False


That underscore triggers a continuation-line sequence so defers the one-line/block decision. But from an aesthetics viewpoint, don't do it because it is misleading.
 
certainly for multiple actions after THEN

such as;

f Me.GenericForm.Visible = True Then Me.GenericForm.Visible = False : Me.AnotherThing.visible = True : Me.AndAnotherThing.visible = False

could certainly use the underscore but then I suppose that's the whole point of using the block version.

Just went back and took another look at that mistake I made and guess what, I have a block IF statement after the one line version and access hasn't yet thrown a fit.

Anyhow, I'm heeding Bob's warning and changing it back to a block just in case things go a wobbly in the final complied version. I've been stung a few too many times in the past with glitches that only ever show-up when demonstrating to clients and some IT manager's holding the contract over a flushing toilet.

SmallTime
 
Many coding conventions state that you should never use in-line If statements. It makes the code harder to read and could hit the problem that Bob mentioned above.
 

Users who are viewing this thread

Back
Top Bottom