I have a message box on the NoData event of my report that just says "There's no data for the options you selected." But I want to add separate lines to this, something like:
"Please check:
1) The dates you entered
2) The locations you ticked
3) Something else"
When I press [Enter] in the VBA window I get a Compile error that says "Expected: list separator or )", so that's obviously not allowed.
Is there a character code or something like that I can enter that will tell Access to go to the next line in the message box?
And just out of interest, is there a maximum size/no of lines I can have?
I would do it like so
Msg = ""
Msg = Msg & "This is line 1" & vbNewline
Msg = Msg & "This is line 2" & vbNewline
Msg = Msg & "This is line 3" & vbNewline
To keep the layout the way you would get it in the messagebox.... But thats formatting of code...
The first line is just to make sure that your variable is indeed an empty string... no previous content "leaking" in...
Msg = ""
Msg = Msg & "This is line 1" & vbNewline
Msg = Msg & "This is line 2" & vbNewline
Msg = Msg & "This is line 3" & vbNewline
msgbox
... more code
Msg = ""
Msg = Msg & "This is line 1" & vbNewline
Msg = Msg & "This is line 2" & vbNewline
Msg = Msg & "This is line 3" & vbNewline
msgbox
... etc.
If you dont have the first line Msg = "", you either have to have multiple variables or have the old message show up too.
You can/should do same with sql that you have in code...
Sub aaa()
MsgBox "This is my message for line one" & _
vbCrLf & "This is my message for line two" & _
vbCrLf & "This is my message for line three"
End Sub
The new line sequence is Chr$(13) & Chr$(10) on systems where the Chr$(13) is required.
The Chr$(13) was issued before the Chr$(10) because the Chr$(10) can be done while the Chr$(13) is in operation.
It was simply faster to hit the line feed while the carriage was returning.
The carriage return was the longest operation that an old mechanical printer had to do so hit the line feed while the carriage was returning.
It didn’t make much difference on long carriage returns but on shorter returns it did.
Trivia, but it helps to remember the sequence is actually reversed from the normal ASCII sequence…13 then 10.
vbNewLine will drop the Chr$(10) linefeed for systems that do not require it.
(It might be an error in the Access help file but I think UNIX drops the 13 in text files.)
Screen based text editors use the Vertical Tabulation character (11) which was designed to tab vertically without affecting the column position. (Normally only VT down because old printers did not have the capability to reverse a new line vertically. Didn’t make a lot of sense either since the line above probably already had something printed on it. Remember, this is old stuff and perhaps the first printer capable of doing it was the IBM Selectric (Golfball) typewriter.
In any case, I don’t think the VT is used natively in Windows but that doesn’t prevent screen text editor writers from using it.
Sub aaa()
MsgBox "This is my message for line one" & _
vbCrLf & "This is my message for line two" & _
vbCrLf & "This is my message for line three"
End Sub
Oh Jeez...have I opened a can of worms here? You folks have now totally lost me, but do feel free to carry on discussing this. I'm off to post another question on something that's puzzling me!
>>oh noooos... please... no... forget the line continuations! (look up in the thread)<<
I really don’t understand that at all.
Sure there’s a limitation of about 50 (from memory, and that can be reset) but it seems to me to be a matter of style.
Actually, since I think it is just a matter of style, I preferred your first method in order to restrict the line length in code: -
Code:
Sub aaa()
MsgBox "This is my message for line one" & vbNewLine & _
"This is my message for line two" & vbNewLine & _
"This is my message for line three", _
vbInformation + vbOKOnly, _
"My message title."
End Sub
>>oh noooos... please... no... forget the line continuations! (look up in the thread)<<
I really don’t understand that at all.
Sure there’s a limitation of about 50 (from memory, and that can be reset) but it seems to me to be a matter of style.
It is mostly a matter of style, but... there are 2 problems attached to this
1) Formatting
A lot of the Line Continuation code is frigging unreadable for one or more reasons, again this can be fixed with some simple spaces. However 'my' style more or less forces it.
2) the limit on continuations
Though it is pretty much (dont remember exact numbers on the limits either) there have been plenty of complaints around this and other forums " I added 1 line now it fails, WTF" answer: Line continuations either broken or to many.
Problem of the limit is that no one is aware of it, running into problems 'my' style prevents it...
Which is worse IMHO, it is unreadable to a semi untrained Eye...
With a message box it is not as much a problem, with sql or other code it is...
Just get yourself used to making readable code, this will prevent yourself from having problems in the future....
While formatting code is trivial at best... Why do we do this??
Code:
Sub test
if something then
do this
do that
else
do while Patato
if chips then
paprika
else
bolognese
endif
loop
endif
End Sub
And why not simply
Code:
Sub test
if something then
do this
do that
else
do while Patato
if chips then
paprika
else
bolognese
endif
loop
endif
End Sub
To access it dont matter, formatting is trivial, yet we all agree the first is better than the latter, why? Readability to the human eye + easier fault detection for missing stuff (missing end if or loop or something)
In that spirit in my (maybe not so) humble opinnion
Code:
MsgBox "This is my message for line one" & vbCrLf & "This is my message for line two" & vbCrLf & "This is my message for line three"
vs
Code:
Sub aaa()
MsgBox "This is my message for line one" & vbNewLine & _
"This is my message for line two" & vbNewLine & _
"This is my message for line three", _
vbInformation + vbOKOnly, _
"My message title."
End Sub
vs
Code:
Sub aaa()
txt = ""
txt = txt & "This is my message for line one" & vbNewLine
txt = txt & "This is my message for line two" & vbNewLine
txt = txt & "This is my message for line three"
MsgBox txt, vbInformation + vbOKOnly _
, "My message title."
End Sub
I think you will agree that on the whole the last one is most readable... again for msgbox trivial, for other bigger stuff it gets more important IMHO.
Getting into the habbit of "good behaviour" is good, regardless of triviality...