Using ":" in coding

D

Dymondjack

I've recently discovered that you can simulate a carriage return in the VBA
code window (for lack of a better phrase) using a colon

ex.
Case 0: strString = ""

instead of the usual

Case 0
strString = ""

I see this used a little bit here and there, but really know nothing about
using a colon in vba programming (I know the colon has a very significant
meaning most programming languages, but vba is all I really know). Searches
on the subject have left me unsatisfied (though I've learned that certain
parameters can be set using := ) <-- not a smiley...

Anyway I was hoping someone might be able to shed some light on it. With so
many ways to condense code by utilizing this, I would think it would be seen
plastered everywhere rather than just here and there. I know there has to be
much more to this picture, I just can't seem to find it.

Consider this code:

'start code
Function fCmdSubst(aDrive As String, aPath As String) As Double
On Error Goto Error_Function
Dim x: x = 1 / 0
Exit_Function: Exit Function
Error_Function: MsgBox ("Error"): GoTo Exit_Function
End Function
'end code

Obviously these code examples are rather weak in functionality, but the
implications behind the concept are rather broad I think, especially in
redundant procedure openings and closings.

Anyone that might be able to provide feedback or links on drawbacks (or
anything else) regarding this, it would be very much appreciated. I would
love to utilize this in an effort to better organize my code, but am afraid
of playing with fire as I am uneducated in the subject...

Many TIA!
-jack
 
P

pietlinden

My two cents isn't worth much, but I don't really like the idea of
jamming a bunch of commands on a single line. I would use the colon
only very sparingly in my code, if at all. I just find that it helps
my coding very much. I would rather have single statements that I can
comment out and test my code. That's just my personal opinion,
though.
 
A

Allen Browne

The colon does allow you to put multiple lines of code on one line.

For example, you can generate a loop in the Immediate Window (Ctrl+G) like
this:
For i = 1 to 5:Debug.Print i:Next

In general, this is not good coding style, as it makes the program harder to
read. The example you give where there is a single line for each Case is
probably one of the places where the style does not really detract from
readability.

Not something you want to do often, though.

The other example you mentioned sounds like named parameters. You can open a
form in dialog like this:
DoCmd.OpenForm "Form1",,,,,acDialog
But how many commas are needed?
How easy is that to read and tell if it's correct?
A better solution would be to use one comma, and name the argument you are
trying to set:
DoCmd.OpenForm "Form1", WindowMode:=acDialog

This is a completely different use than putting multiple VBA statements on a
single line.

HTH
 
D

Dymondjack

Thanks for the tips guys. At least now I know actually what it is meant for
and doesn't have some hidden meaning that is going to severely corrupt my
code. The more my code evolves the more I find lengthy procedure headers and
footers (error handling, procedure call stacking, user activity logging, ect
ect ect). Nothing like having 25 lines of code for a 1 line function.

I can see how oversaturation of this feature could make code difficult for
someone to read, and have decided that I'm probably better off using it
sparingly.

For anyone interested, I've noticed that if you are going to use the Goto
statement, the label for Goto needs to be at the start of a fresh line. For
example:

Goto Exit_Function
Function = varRet: Exit_Function: Exit Function

raises a compile error stating that the function or sub Exit_Function is not
defined.

This raises my curiousity towards the following standard example:

Function fFunc() As Long
On Error Goto Error_fFunc
fFunc = 1 + 1
Exit_fFunc:
Exit Function
Error_fFunc:
Msgbox ("Error")
Goto Exit_fFunc
End Function

I've always wondered why the Exit_fFunc and Error_fFunc lines have a colon
after them (this seems to be standard for every exit/error section).

I'm not nessecarily looking for an answer on this, more just idle
speculation (trying not to beat a dead horse too hard here).

Thanks for the feedback
-jack
 
G

Guest

I see that you have a third use of the colon there.

Firstly as a statement separator, used for code formatting and
so that complex statements can be evaluated by versions of
basic (like the Access immediate window), that can only evaluate
one line at a time.
if a=1 then b=2:c=3 else b=0: c=0

Secondly as part of an assignment statement, used with named parameters.
WindowMode:=acDialog

And thirdly as part of a Line Label.
Exit_Function: Exit Function


Line Labels aren't statements, and the colon at the end of the line
label is not a separator, it is part of the line label.

If you use numbers instead of line labels, you do not need the colon

On error goto 1
1 msgbox "one"

If you use a line label instead of a number, you must use the colon,
because line labels end with a colon.

on error goto david
david: msgbox "david"


This is different from your case statement. Each case statement
is actually a mini IF statement, and it can be separated from the next
statement by a colon or by and new line.

Not like the case statement in Pascal or C, where each Case
is actually just a label. In VBA, each case line is a special
kind of If statement

select case 5
case I
call mysub
case J,K
call mysub
end select

Is the same as

If (I=5) then
call mysub
else
if (J=5) then
call mysub
else
if (K=5) then
call mysub
endif
endif
endif


(david)
 
J

John W. Vinson

I've always wondered why the Exit_fFunc and Error_fFunc lines have a colon
after them (this seems to be standard for every exit/error section).

That's the syntax for a line label. From the Help file:

line label
Used to identify a single line of code. A line label can be any combination of
characters that starts with a letter and ends with a colon :)). Line labels
are not case sensitive and must begin in the first column.
 

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