Force carriage return in string (1 Viewer)

KeithIT

Registered User.
Local time
Today, 18:07
Joined
Dec 20, 2004
Messages
133
Anyone know how to force a carriage return in a concatenating string in VB? I have a long list of names that my code gathers from a list box and I want to force a carriage return inbetween each one.

I searched the forum but wasn't able to find the answer on here (although I probalby just didn't look in the right places). Thanks!
 

simongallop

Registered User.
Local time
Today, 23:07
Joined
Oct 17, 2000
Messages
611
Think, though only use it in VBScript, use:

MyString = "blah, blah" & vbcrlf & "more blah blah"
 

KeithIT

Registered User.
Local time
Today, 18:07
Joined
Dec 20, 2004
Messages
133
gratzie! :)
 

modest

Registered User.
Local time
Today, 18:07
Joined
Jan 4, 2005
Messages
1,220
vbCr
vbCrLf
vbLf
vbNewLine

they each produce a new line... however you'll find that they have different outcomes for certain cases.
 
Last edited:

BryanT

Registered User.
Local time
Tomorrow, 10:07
Joined
Mar 8, 2005
Messages
25
"My string " & chr(13) & "my next string"
 

Oldsoftboss

AWF VIP
Local time
Tomorrow, 08:07
Joined
Oct 28, 2001
Messages
2,499
Sorry Rich, I'm with BryanT.

I only ever use & Chr(13) & ....

from Access help (believe it or not!!)

Chr(10) returns a linefeed character

Chr(13) is a carrage return (the difference is what? :confused: ), thus making your code give a space between the lines.

Dave
 

RoyVidar

Registered User.
Local time
Tomorrow, 00:07
Joined
Sep 25, 2000
Messages
805
Oldsoftboss- have you tested your suggestion, and displayed the string in a control on a form or report ;)
 

nickellis

Registered User.
Local time
Today, 23:07
Joined
Oct 13, 2004
Messages
21
I always just use & chr(13) &

It's always worked absolutely fine for me, both in forms and in message boxes. What is the difference of which you speak?

Nick
 

RoyVidar

Registered User.
Local time
Tomorrow, 00:07
Joined
Sep 25, 2000
Messages
805
Would you mind testing again? Cause on my setups, 97 through 2003, assigning with only one of the characters (either only carriage return or only linefeed), provides small funny characters, that even though they are the carriage return character (chr(13)) or the linefeed character (chr(10)), well produces those funny characters, and not new lines.

Here's a simple test on a text control called txtTest, that should either reproduce the behaviour I have or not:

me!txtTest.value = "one" & chr(13) & "small" & chr(13) & "test"

On my setups, chr(13) & chr(10) (in that order), vbCr & vbLf (in that order), vbCrLf or vbNewLine is needed to provide new lines in text controls. You think this could be some regional settings thingie?

In message boxes, however, it's enough with only one of them.
 

nickellis

Registered User.
Local time
Today, 23:07
Joined
Oct 13, 2004
Messages
21
You are quite right. I've just performed your test and found that it only worked when using both.

The reason I have not come across this problem before is that I mainly use it in message boxes, where it is not an issue. I would swear blind I've done it on forms, but perhaps I haven't since I have never come across this before.

I once did a very natty message box that allowed users to select which group membership they wanted to use. to do this it looped round a recordset derived from a query that got all the groups they were members of. the end of each loop was chr(13), not a chr(10) in sight!

Nick
 

modest

Registered User.
Local time
Today, 18:07
Joined
Jan 4, 2005
Messages
1,220
chr(number) is something that you should use sparingly. Using the vb keywords that I gave you above are more descriptive and remind you what is going on.

Again, they are:
vbCr
vbCrLf
vbLf
vbNewLine

vbVerticalTab may also be one, I can't remember what it does though.
 

ChrisO

Registered User.
Local time
Tomorrow, 08:07
Joined
Apr 30, 2003
Messages
3,202
Hmmm…

Since there probably is no carriage, but maybe if it is an old printer: -
(Numbers are in decimal.)

Chr$(13) Carriage return, moves carriage (print head) to the left margin.
(Not necessarily the hardware left carriage shock absorber.)
(If you want to see something funny then write a small program that moves the carriage of an old printer from left mechanical stop column 1 to, say, column 132. Then back to column 2 and onto column 131, 3, 130, 4, 129, 5, 128… don’t have a coffee on the same table as the printer because harmonics are a wonderful thing. :D )

Chr$(10) Line Feed, forces the paper to advance by one line.

Chr$(11) Vertical Tabulation. Seldom used on printers but forces position one line down.
(If no lower line is visible then invoke a Line Feed. Could be wrong on that, long time ago. :( )

In VBA, vbNewLine…forces the result of a carriage return + line feed even if the system does/does not require the Chr$(10) (UNIX and some versions of Mac.)

So why is the carriage return issued before the line feed? One answer is that some systems don’t need the line feed but that is incorrect. If a system doesn’t need a line feed then it should not be sent and the printers could be setup to automatically insert a line feed if required and not sent.

When a printer receives a carriage return that will be the longest, in time, of any job it has to do. Remember this goes back quite a while and the carriage was returned by a spring. So to save time the carriage return was issued first and while the carriage was returning a line feed, if required, was performed during transit. It was simply faster to do it that way.

Regards,
Chris.
 
Last edited:
R

Rich

Guest
modest said:
chr(number) is something that you should use sparingly. Using the vb keywords that I gave you above are more descriptive and remind you what is going on.

.

vb doesn't work in the control source, sql etc
 

modest

Registered User.
Local time
Today, 18:07
Joined
Jan 4, 2005
Messages
1,220
Rich said:
vb doesn't work in the control source, sql etc
KeithIT said:
Anyone know how to force a carriage return in a concatenating string in VB?

Do notice that I said use sparingly. There are certain occassions where it may be required to call on the ASCII character code, but for a concatenating string... just stick to making it readable.
 

AndyBr

New member
Local time
Today, 12:07
Joined
Jun 19, 2009
Messages
7
On my setups, chr(13) & chr(10) (in that order), vbCr & vbLf (in that order), vbCrLf or vbNewLine is needed to provide new lines in text controls.

Just to save someone else a morning of searching: the 'in that order' is very important. I was going bonkers trying to figure out why the reverse - Chr(10) then Chr(13) - would return in my report as two boxes, a little like this: [] []

The reason was the order of the two chr.

This is at least for Access 97.

Andy
 

SOS

Registered Lunatic
Local time
Today, 15:07
Joined
Aug 27, 2008
Messages
3,517
Just to save someone else a morning of searching: the 'in that order' is very important. I was going bonkers trying to figure out why the reverse - Chr(10) then Chr(13) - would return in my report as two boxes, a little like this: [] []

The reason was the order of the two chr.

This is at least for Access 97.

Andy
And the reason for that is that Chr(13) is a Carriage Return and Chr(10) is the Line Feed.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:07
Joined
Sep 12, 2006
Messages
15,641
in a text box, i would use vbcrlf, which is chr(13) + chr(10)

writing to a text file either

print line
write line

automatically add a crlf at the end of each line
 

SOS

Registered Lunatic
Local time
Today, 15:07
Joined
Aug 27, 2008
Messages
3,517
And you can't use vbCrLf in a query or control source so you have to use Chr(13) & Chr(10) for those.
 

Users who are viewing this thread

Top Bottom