Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TRIM CR and LF

2 views
Skip to first unread message

david epsom dot com dot au

unread,
Jan 27, 2004, 4:06:59 AM1/27/04
to
I realised, after pasting some text into a textbox, that Access does not
trim CR and LF from pasted text.

What would be the best (cleanest) way to trim text so that it does not
include control characters? Specifically, so that text is cleared of stray
CR/LF at the end of a text value, or any non-text at the end, or any
non-text anywhere?


(david)


Ken Snell

unread,
Jan 27, 2004, 10:35:50 AM1/27/04
to
Use control's AfterUpdate event to run a function that uses the Replace
function to remove all characters that you don't want.

Private Sub txtBoxName_AfterUpdate()
Me.txtBoxName.Value = DeleteUndesirables(Me.txtBoxName.Value)
End Sub


Public Function DeleteUndesirables(strString As String) As String
Dim strWorkString As String
strWorkString = strString
strWorkString = Replace(strWorkString, Chr(13), "", 1, -1,
vbTextCompare)
strWorkString = Replace(strWorkString, Chr(10), "", 1, -1,
vbTextCompare)
' continue for all other characters to be deleted
DeleteUndesirables = strWorkingString
End Function

Alternatively, you could loop through the string in the function and compare
each character to the alphanumeric character set that you want to allow, and
delete a character if it's undesirable. But that may be more looping than is
necessary.
--
Ken Snell
<MS ACCESS MVP>

"david epsom dot com dot au" <david@epsomdotcomdotau> wrote in message
news:uPu3HUL5...@TK2MSFTNGP12.phx.gbl...

Brendan Reynolds

unread,
Jan 27, 2004, 10:39:27 AM1/27/04
to
Control characters (carriage return, line feed, tab, etc.) all have ASCII
codes less than 32 (the ASCII code for a space) so ...

Public Function StripControlCharacters(ByVal strStringToStrip As String) As
String

Dim lngCounter As Long
Dim strWork As String
Dim strChar As String
Dim intChar As Integer

For lngCounter = 1 To Len(strStringToStrip)
strChar = Mid$(strStringToStrip, lngCounter, 1)
intChar = Asc(strChar)
If intChar >= 32 Then
strWork = strWork & strChar
End If
Next lngCounter

StripControlCharacters = strWork

End Function

Example of use, in Immediate window ...

? stripcontrolcharacters("some" & vbcrlf & "text")
sometext

--
Brendan Reynolds (MVP)

"david epsom dot com dot au" <david@epsomdotcomdotau> wrote in message
news:uPu3HUL5...@TK2MSFTNGP12.phx.gbl...

0 new messages