
cross trim function and replace command
Here's a general-purpose t{*filter*} function I just threw together. It
hasn't been thoroughly tested, but it appears to work:
'----- start of code -----
Function fncTrimChars( _
StringToTrim As String, _
CharsToTrim As String, _
Optional ByVal TrimWhere As String = "B", _
Optional ByVal CompareMethod As Integer = vbBinaryCompare)
_
As String
' Trim leading and/or trailing occurrences of specified characters
from a string.
'
' Arguments are
' StringToTrim - character string to be trimmed
' CharsToTrim - string containing the characters to be
trimmed. Multiple
' characters may be specified; they will be
considered
' interchangeable and will all be trimmed
' TrimWhere - Optional character to specify where the
t{*filter*} should occur:
' L = on the left
' R = on the right
' B = both left and right (the default)
' CompareMethod - Optional integer specifying whether binary
comparison or
' text comparison is used. Default is binary.
Use the
' VB constants vbBinaryCompare and
vbTextCompare to specify
' this option.
'
' fncTrimChars -- Copyright 2002, Dirk Goldgar
' License is granted to use this function in your programs so long
as the
' copyright notice remains unchanged.
Dim lngLength As Long
Dim lngStart As Long
Dim lngStop As Long
If Len(TrimWhere) = 0 Then
TrimWhere = "B"
End If
If Not TrimWhere Like "[BLR]" Then
Err.Raise 5
End If
lngLength = Len(StringToTrim)
If lngLength = 0 Then
Exit Function
End If
If TrimWhere = "B" _
Or TrimWhere = "L" _
Then
For lngStart = 1 To lngLength
If InStr(1, CharsToTrim, Mid$(StringToTrim, lngStart, 1),
CompareMethod) = 0 _
Then
Exit For
End If
Next lngStart
Else
lngStart = 1
End If
If TrimWhere = "B" _
Or TrimWhere = "R" _
Then
For lngStop = lngLength To lngStart Step -1
If InStr(1, CharsToTrim, Mid$(StringToTrim, lngStop, 1),
CompareMethod) = 0 _
Then
Exit For
End If
Next lngStop
Else
lngStop = lngLength
End If
fncTrimChars = Mid$(StringToTrim, lngStart, 1 + lngStop -
lngStart)
End Function
'----- end of code -----
Beware of lines wrapped by the newsreader in the above code.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)
Quote:
> I do a lot of importing from my work mainframe, using what
> I call screen scraping. Using VB, we control the
> mainframe front end (EXTRA), collect data from certain
> fields and store it in a table, for further processing.
> Some fields from the mainframe application include
> underscore, that I do not want . ex. 367-AP-666______
> I want to import this as 367-AP-666. I am looking for
> something like the trim function, but instead of t{*filter*}
> spaces, I need it to trim underscore. I can use replace
> off of the menu, but that is akward. I would prefer to
> fix this as I put the data into the table.
> Thanks for any suggestions.