Syntax for putting multiple lines in a msgbox (1 Viewer)

Big Pat

Registered User.
Local time
Today, 11:09
Joined
Sep 29, 2004
Messages
555
Hi,

I bet this is easy but I'm a VBA newbie...

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?

Pat.
 

DCrake

Remembered
Local time
Today, 11:09
Joined
Jun 8, 2005
Messages
8,632
When constructing a message with more than one line you can do the following

Code:
Dim Msg As String

Msg = "This is line 1" & vbNewline
Msg = Msg & "This is line 2" & vbNewline
Msg = Msg & "This is line 3" & vbNewline

etc

Then in your MsgBox

MsgBox Msg,,"title"

David
 

namliam

The Mailman - AWF VIP
Local time
Today, 12:09
Joined
Aug 11, 2003
Messages
11,695
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...
 

Big Pat

Registered User.
Local time
Today, 11:09
Joined
Sep 29, 2004
Messages
555
Thank you so much...that works a treat.

I never knew you could do things like that.

Pat.
 

DCrake

Remembered
Local time
Today, 11:09
Joined
Jun 8, 2005
Messages
8,632
An alternaive to vbNewline is VbCrLf or Chr(10) & Chr(13) you can also use VBTab if you want to set tab spaces in your message.

Nailmans suggestion was for brevity and easy reading the code is still the same.

David
 

Big Pat

Registered User.
Local time
Today, 11:09
Joined
Sep 29, 2004
Messages
555
Thanks Mailman,

I'm not certain what that first line is for, does it add an extra blank line?

I realised you can add an extra &vbNewLine at the end of each line of code to get double-spacing in the message box.

I like this! Thanks guys.

Pat.
 

namliam

The Mailman - AWF VIP
Local time
Today, 12:09
Joined
Aug 11, 2003
Messages
11,695
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...
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 03:09
Joined
May 13, 2009
Messages
93
vbCrLF example:
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
 

ChrisO

Registered User.
Local time
Today, 20:09
Joined
Apr 30, 2003
Messages
3,202
Just as a minor point of almost no interest: -

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.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 11:09
Joined
Sep 12, 2006
Messages
15,683
i generally use vbcrlf

but

vbcr is chr(13)
vblf is chr(10)
vbcrlf is chr(13) & chr(10)

so what does vbnewline ACTUALLY do

--------
i thought the reason for a CR AND a LF character was to move down a line AND get back to the start of the line.

eg - with most text editors, moving the cursor down a row doesnt reposition the cursor to the start of the row
 

ChrisO

Registered User.
Local time
Today, 20:09
Joined
Apr 30, 2003
Messages
3,202
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.
 

namliam

The Mailman - AWF VIP
Local time
Today, 12:09
Joined
Aug 11, 2003
Messages
11,695
Unix only uses the 10 not the 13, windows requires both

@ Rick Stanich
vbCrLF example:
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

oh noooos... please... no... forget the line continuations! (look up in the thread)
 

Big Pat

Registered User.
Local time
Today, 11:09
Joined
Sep 29, 2004
Messages
555
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!
 

ChrisO

Registered User.
Local time
Today, 20:09
Joined
Apr 30, 2003
Messages
3,202
>>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.
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 03:09
Joined
May 13, 2009
Messages
93
Unix only uses the 10 not the 13, windows requires both

@ Rick Stanich


oh noooos... please... no... forget the line continuations! (look up in the thread)
I only did that to format the code window. ;)
Other wise it will look like this.
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
 

ChrisO

Registered User.
Local time
Today, 20:09
Joined
Apr 30, 2003
Messages
3,202
G’day Rick.

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

But I can’t see it as anything other than style.
 

namliam

The Mailman - AWF VIP
Local time
Today, 12:09
Joined
Aug 11, 2003
Messages
11,695
>>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...

Other wise it will look like this.
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
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...
 

Users who are viewing this thread

Top Bottom