PDA

View Full Version : Solved: Carriage Return Question



CreganTur
04-25-2008, 09:21 AM
This may be a nit-picky little question, but one thing I'm learning about VBA is that it's a nit-picky language:razz2:

When I'm building message boxes, and want my prompt text on multiple lines, I use the vbCrlf command for carriage return. The resource book I'm currently working through uses Chr(13) or Chr(10) for carriage return.

Is there a functional reason that makes one of these options more optimal?

fumei
04-25-2008, 12:01 PM
vbCrLf is a VB Constant.

Chr() is a VB Function.

Chr(CharCode As Long)

So, Chr(34) - or any other number - calls the Function Chr(), and passes to it the required argument CharCode.

To be fussy fussy fussy.....

1. any call to a function has to be parsed by the compiler;

2. the required argument must pass that parsing.

Chr("yadda") will of course fail, as "yadda" is not a Long. Even if the parameter IS a Long, it will fail if it is not a valid CharCode.

Say you slipped and double key...

"yadda" & Chr(344)

Option Explicit will not catch this, as 344 is within the parameters of being a Long.

It will fail as run-time error 5.

"Invalid procedure call or argument."

which is not terribly informative. Worse yet...it will NOT, repeat NOT break to point of error.

Selection.TypeText Text:="yadda" & Chr(34)
Selection.TypeText Text:="yadda" & Chr(30)
Selection.TypeText Text:="yadda" & Chr(344)
Selection.TypeText Text:="yadda" & Chr(34)


will fail with run-time error 5, but it will NOT point (or highlight) the line with the actual error. This is because it is a parsing error.

Bottom line? It is nick picking, but IMO vbCrLf is generally better. It is immediately recognized as a constant....and that is that. End of story.