Avoiding/replacing CRLF

Tiger955

Registered User.
Local time
Today, 18:26
Joined
Sep 13, 2013
Messages
140
Hi!

My users send SMS via a form.

If they insert an crlf it counts 2 ditigts in an SMS and i would like to avoid that they insert Chr(10).

Private Sub Mailtext_KeyPress(keyascii As Integer)
If keyascii = Asc(Chr(10)) Then
MsgBox "No returns in a SMS
Me.Mailtext.Text = Replace(Me.Mailtext.Text, Chr(10), Chr(32))

End If

Above code makes a strange behavoir:
Within the textbox the text starts then in the second line and it seems, like an aoutomatic return has been set BEFORE my text.

Any idea, how I can solve my probelm, either to avoid returns at all or delete the Chr(10) after inserted.

FYI: Me.Mailtext.Text = Mid(Me.Mailtext.Text,1,Len(Me.Mailtext.text)-1)
or
Me.Mailtext.Text = Left(Me.Mailtext.Text,Len(Me.Mailtext.text)-1)

makes the same. My text moves allways in the second line of the textbox and has (most likely) a return before the text.

Thanks
Michael
 
Why not try..
Code:
Me.MailText = Replace(Me.MailText, vbCrLf, vbNullString)
 
You could always strip out the unwanted carriage returns just before you send the message. <LF> (linefeed ) is character 10, <CR> (carriage return) is character 13, <space> is character 32.

Code:
' Replace CRLF with LF only.
Me.Mailtext.Text = Replace(Me.Mailtext.Text, Chr(10) & Chr(13), Chr(10))

' If necessary replace LF with a space
Me.Mailtext.Text = Replace(Me.Mailtext.Text, Chr(10) , Chr(32))

Things you could also look out for:
<LF><space> - Chr(10) & Chr(32)
<space><LF> - Chr(32) & Chr(10)
<LF><LF> - Chr(10) & Chr(10)
<space><space> - Chr(32) & Chr(32)

If you're counting the pennies (or characters) even:
<comma><space> - "," & Chr(32)
<full stop><space> - "." & Chr(32)


For diagnostic purposes only you could try using:

Code:
Me.Mailtext.Text = Replace(Me.Mailtext.Text, Chr(10) , "<LF>")
Me.Mailtext.Text = Replace(Me.Mailtext.Text, Chr(13) , "<CR>")
Me.Mailtext.Text = Replace(Me.Mailtext.Text, Chr(32) , "<space>")

Then you could see what characters were actually there.

Personally, I would tend to check the text before sending rather than after each keystroke.
 
Last edited:
Why not try..
Code:
Me.MailText = Replace(Me.MailText, vbCrLf, vbNullString)

Hi Paul!

How could it then be, that the cursor stays in line 2 with your code after replacing the linefeed?
What forces the linefeed in your code?
Or is it just like as I would move the cursor with the mouse?
I tried your code, but there is still a line 2 and I can place the curos there. But not in a third or fourth line.

So I assume, that there is still "something" in line 2.
Michael
 
[
Code:
' Replace CRLF with LF only.
Me.Mailtext.Text = Replace(Me.Mailtext.Text, Chr(10) & Chr(13), Chr(10))
 
' If necessary replace LF with a space
Me.Mailtext.Text = Replace(Me.Mailtext.Text, Chr(10) , Chr(32))

[/QUOTE]

Hi Nigel!
Replace(Me.Mailtext.Text, Chr(10) & Chr(13), Chr(32)) deletes the whole text.

As I limited the text box to 160 signs, also a crlf counts down. Thats why I want to delete a crlf at once and not before sending.

Michael
 
Go with Nigel's suggestion of checking the String before sending. This is mainly because it will avoid bulking up compiler processing, and can be handled in a single line of code.
 
I've had a bit of a play and come up with an idea of how to produce an adjusted count of the characters in the textbox.

You could allow the textbox to accept a few more characters to compensate whilst displaying a corrected character count.
 

Attachments

I've had a bit of a play and come up with an idea of how to produce an adjusted count of the characters in the textbox.

You could allow the textbox to accept a few more characters to compensate whilst displaying a corrected character count.

Thks Nigel, I will wokr on this or do it the way mentioned earlier, to replace before sending.
Michael
 

Users who are viewing this thread

Back
Top Bottom