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)
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...
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...