Replace a charater with a carriage return

G

Guest

Does anyone know how to replace a character in a string with a carriage return?

This is the code I have and what it does is replace the character with a
character shapped like a cube:

txttemp = Replace(txttemp, Mid(txttemp, "A", 1), chr(13))

I've tried chr(10) and chr(10) & chr(13) combined and I keep getting the
cube shapes instead of the actual carriage return.

Any help is appreciated.

Thank you,

Sarah
 
J

Joshua A. Booker

Sarah,

Try this;

txttemp = Replace(txttemp, Mid(txttemp, "A", 1), vbCRLF)


HTH,
Josh
 
D

Duane Hookom

I don't understand
Mid(txttemp, "A", 1)
Mid() usually takes a string followed by 1 or 2 numbers. What is "A"
expected to do in there?

Duane Hookom
MS Access MVP
 
G

Guest

Yes you're correct... I was just giving an example of a character I would
replace with the carriage return. I actually have a counter there as follows:

Mid(txttemp, i, 1)

If Mid(txttemp, i, 1)="A" then I want the "A" replaced with the carriage
return
 
G

Guest

What are you expecting to see? chr(10) & chr(13) is not printable. You can
use the vbCrLf contstant which is the same thing. If you have a string that
is "ABC" & vbCrLf & "EFG", then if you print it out in the immediate window,
it should be:
ABC
DEF
 
G

Guest

You're right it should be, but it does not show up that way. It shows up as:

ABC then a cube shaped character then EFG all on the same line. I am doing
this in a report in a text box.
 
D

Dirk Goldgar

Sarah said:
You're right it should be, but it does not show up that way. It
shows up as:

ABC then a cube shaped character then EFG all on the same line. I am
doing this in a report in a text box.

Note that Chr(13) by itself won't work, and Chr(10) by itself won't
work, and Chr(10) & Chr(13) won't work (that's a line feed followed by a
carriage return). Only Chr(13) & Chr(10) will be recognized as a new
line. As Klatuu and others have said, in VBA code you can use the
defined constant vbCrLf to represent the carriage return+line feed
combination. You can also use vbNewLine for the same thing. In those
places where you can't use the named constants, such in queries and in
controlsource expressions, you have to use Chr(13) & Chr(10), not the
named constants.
 
G

Guest

In a text box in a report! Okay, here is the issue. If you are putting that
string together in the text box control source, it will not work. It will
not understand vbCrLf at all, and will give you that square gizmo if you use
Chr(10) & chr(13).
Here is where it will work. Put the code in the On Format event of the
section of the report the text box is in. You can create an invisible
control that is bound to the field you want to split up, then make this
control unbound and assign the value to it.
 
G

Guest

Everyone's help is getting me closer. I have a control on the report called
comments. I call a function called AddCR passing the comments control as
follows:

AddCR(Comments)

Public Function AddCR(txtTemp as string) as string
txtTemp=Replace(txtTemp, "A", vbNewLine)
AddCR=txtTemp
End Function

Example: if comments = "BCAGD", the value returned should be

BC
GD

when I print this out in the module it is correct, but when it goes back to
the report it only shows

BC

It looks like the carriage return works, but then nothing prints out
afterwards.

Any ideas?
 
G

Guest

Make you text box taller.

Sarah said:
Everyone's help is getting me closer. I have a control on the report called
comments. I call a function called AddCR passing the comments control as
follows:

AddCR(Comments)

Public Function AddCR(txtTemp as string) as string
txtTemp=Replace(txtTemp, "A", vbNewLine)
AddCR=txtTemp
End Function

Example: if comments = "BCAGD", the value returned should be

BC
GD

when I print this out in the module it is correct, but when it goes back to
the report it only shows

BC

It looks like the carriage return works, but then nothing prints out
afterwards.

Any ideas?
 
G

Guest

I did that and it worked, but I don't like that. The text that will print
out may be 1 line long, 2 lines long or 100 lines long. I tried the Can Grow
feature and it doesn't work when there is a carriage return in the value of
the text box. How can I fix this?
 
G

Guest

Thanks everyone. I finally got it to do what I wanted. Thanks for all your
time and patience!!!!!

Have a great Easter!!!!

Sarah
 

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