Results 1 to 11 of 11
  1. #1
    manonash is offline Novice
    Windows 11 Access 2016
    Join Date
    Feb 2024
    Posts
    4

    Question I Need a Non-Manual Way that WORKS to Replace a Character with a Carriage Return!


    I am using Access 2016 on Windows 11. Please see the image below for a description of the issue. Thank you!

    Click image for larger version. 

Name:	Access Converts CR to Space (writeup).png 
Views:	27 
Size:	215.2 KB 
ID:	51530

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Well that is a little interesting... In the immediate window:

    Code:
    ? "A " & chr(13) & chr(10) & "B"
    A 
    B
    So in theory that should work, those are the correct Chr() values.
    Again in the immediate window

    Code:
    ?Replace ("A@@B","@@" , chr(13) & chr(10))
    A
    B
    I then thought it might be the double @@@@ but nope that works as well:

    Code:
    ?Replace ("A@@@@B","@@" , chr(13) & chr(10))
    A
    
    
    B


    So I have no idea why your's doesn't work.
    In Notepad ++ the last example shows up as

    Code:
    ?Replace ("A@@@@B","@@" , chr(13) & chr(10))
    ACRLF
    CRLF
    BCRLF
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    IIRC, you cannot show line wraps in a textbox unless the table field is set to Long Text (aka Memo). Could that be the issue (because your query expression looks good)? Or check your table field instead of the form. You might find that the field text is wrapped but the form control isn't showing it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    manonash is offline Novice
    Windows 11 Access 2016
    Join Date
    Feb 2024
    Posts
    4
    Thanks for looking at this, Minty and Micron. The [Body] field is type Long Text, and the Text Format property on both the table and the form is set to "Rich Text". I could've left the form out of the mockup. It's irrelevant, because the problem can be seen in the table directly.

    I first tried using the Import dialog to bring the data in. Then I tried it with VBA code. Then with the update query. The *only* way I've been able to get a real carriage return in there is by manual entry.

    I am using Access2016. Any chance this is just an old bug that never got fixed?

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Any chance this is just an old bug that never got fixed?
    I'd say no, because it works for me:
    Code:
    UPDATE myTable SET myTable.Item = Replace([Item],"A",Chr(13) & Chr(10))
    WHERE ((Mid([Item],2,1)="A"));
    The values CA8 were replaced with C and a line wrap and the 8 character.
    Item_No Code1 Item Country Temp F
    4 CA C
    8
    AT1
    1 CA C
    8
    BE1
    2 CA C
    8
    CN1
    3 CA C
    8
    CNB3
    The table Item field is short text. I have not tested this with a form because you say that the issue can be seen in the table. Your table row height is sufficient to show 2 or more lines I hope. Consider copying, compact/repair the copy, zip it and post here. See "How to attach files" at the top if you need help with that.
    Last edited by Micron; 02-19-2024 at 01:11 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Does RichText recognise CrLF ?
    I thought it needed to be <BR> or <P> inserted?

    Try copying the table and turning off the rich text, just as an experiment?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I think you are on to something there. RTF is like HTML.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    manonash is offline Novice
    Windows 11 Access 2016
    Join Date
    Feb 2024
    Posts
    4
    Quote Originally Posted by Minty View Post
    Does RichText recognise CrLF ?
    I thought it needed to be <BR> or <P> inserted?
    Brilliant! That was the problem: I needed to replace the text token representing the newline with "<p>" -- the HTML code for a new paragraph -- rather than Chr(13) & Chr(10)!

    I did a few experiments to consolidate my understanding of the whole business. Here, for the benefit of anyone who may need it in the future, is what I found and/or concluded:

    --------------------------------------------------------------------------------------------------------------------------
    IMPORTING TEXT DATA WITH LINE BREAKS INTO ACCESS FROM EXCEL

    I experimented with two different forms of spreadsheet with data to be imported. The first, in a "TextWithLineBreaks" column, contained text tokens ("¶¶") -- not line breaks, but rather simply the "¶" character. The second contained line breaks. The latter were created using the Replace dialog in Excel, replacing each text token ("¶¶") with the character that entering CTRL+J in the "with" textbox of the Replace dialog produces.


    Here are the results I got importing these two different versions of the spreadsheet.

    Source: Excel spreadsheet with line breaks in the [TextWithLineBreaks] field
    Target: Access Table with a [TextWithLineBreaks] field set for PLAIN Text;
    Result: The line breaks appear simply to get discarded. Each line from the text in the Body cell of a given row runs into the next line with no intervening spaces or line break.

    Source: Excel spreadsheet with line breaks in the [TextWithLineBreaks] field
    Target: Access Table with a [TextWithLineBreaks] field set for RICH Text;
    Result: The line breaks are converted to SPACEs. (Confirmed by copying and pasting some of the text into Notepad++ and displaying it in hex view.)


    Importing Text with Tokens, and Replacing the Tokens After Import
    When I import the version of the Excel spreadsheet that has text tokens instead of line breaks, both versions of the receiving database table (Plain Text and Rich Text) seem to store exactly the same data.

    If I then perform a Replace operation on the text tokens using an update query, to get the data to display correctly, I must replace the tokens as follows:

    Receiving database table: [TextWithLineBreaks] field set for PLAIN Text
    Replace token ("¶¶") with Chr(13) & Chr(10).

    Receiving database table: [TextWithLineBreaks] field set for RICH Text
    Replace token ("¶¶") with "<p>". (No doubt "<br>", "<p/>", and "<br/>" would also work.)


    In summary, the successful process for importing formatted text with line breaks into Access from an Excel spreadsheet is as follows:

    1. Replace line breaks in the Excel spreadsheet with a suitable text token of your choice. The token must, of course, be a character or string of characters that does not otherwise appear in the data.
    2. Import the spreadsheet into the Access table.
    3. If support for Rich Text is desired in one or more of the Long Text fields in the receiving table, set that field's "Text Format" property to "Rich Text". This can be done either before, or after, the data import, but must be done before executing the Replace operation.
    4. If the target field or fields are set for Plain Text, use an update query to replace every occurrence of the text token in the data with Chr(13) & Chr(10).
    5. If the target field or fields are set for Rich Text, use an update query to replace every occurrence of the text token in the data with either "<p>" or "<br>" (depending upon whether you want paragraphs or just line breaks).
    6. If the data is to be displayed in a form (or presumably a report), be sure to set the Text Format property in any control that will display text with line breaks to the appropriate value. (E.g., if storing Rich Text in the table, set the Text Format property of the form control to "Rich Text").



    Thanks so much, both of you, for the indispensable assistance with this!

  9. #9
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Thanks for posting your testing and solution. I can't help but wonder what would happen if the table field was just short text and not formatted as rtf because the query example I showed worked with ASCII character codes and short text. TBH, I thought in order to format a field as rtf it had to be long text, but you say you are using short.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    manonash is offline Novice
    Windows 11 Access 2016
    Join Date
    Feb 2024
    Posts
    4
    Quote Originally Posted by Micron View Post
    Thanks for posting your testing and solution. I can't help but wonder what would happen if the table field was just short text and not formatted as rtf because the query example I showed worked with ASCII character codes and short text. TBH, I thought in order to format a field as rtf it had to be long text, but you say you are using short.
    No, I said I was using Long Text: and I am. As I mentioned in my description of my testing and solution, the Long Text field will display Chr(13) & Chr(10)'s as line breaks IF the Text Format is set to "Plain Text". Since a Short Text field doesn't have a Text Format property and doesn't support Rich Text, I would expect it to handle the Chr(13) & Chr(10)'s as line breaks by default, which you reported that it did.

  11. #11
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Sorry, I remembered wrong. If anyone had asked me I would have said that Long Text (aka Memo) field supports rtf (e.g. <P>, <BR> and the like, but not ASCII characters like chr(13). There would be no need to support both, and the rtf/html format would not know what to do with them. In post 3 I was wrong about needing Long Text to show line wraps and tried to correct that in post 5. I hope anyone reading this in the future sees that. Again, it's great that you have followed up like you did. Many don't.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 11-09-2022, 09:33 AM
  2. Replies: 18
    Last Post: 10-11-2017, 03:07 PM
  3. Replace function adding in unwanted carriage return
    By timmygrover in forum Queries
    Replies: 10
    Last Post: 07-02-2012, 11:58 AM
  4. Replies: 1
    Last Post: 02-08-2012, 04:44 PM
  5. Replace Carriage Returns with Spaces
    By chitan in forum Queries
    Replies: 1
    Last Post: 12-15-2011, 11:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums