cross trim function and replace command 
Author Message
 cross trim function and replace command

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.


Fri, 11 Mar 2005 02:20:36 GMT  
 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.



Fri, 11 Mar 2005 03:30:54 GMT  
 cross trim function and replace command

Access XP has replace function
some = Replace(some, "_", "")

If I remember corectly in previous versions does not have this function.

So you could create own function
(create public if needed)
Function OwnTrim(stValue As String) As String

    Dim intMax As Integer
    Dim intStart As Integer
    Dim intFound As Integer

    intMax = Len(stValue)
    intStart = intMax - 1

    Do Until intStart = 0
    intFound = InStr(intStart, stValue, "_")
    If intFound > intStart Then Exit Do
    intStart = intStart - 1
    Loop

    stValue = Mid(stValue, 1, intFound - 1)

    OwnTrim = stValue

End Function

--
Posted via http://dbforums.com



Fri, 11 Mar 2005 03:25:58 GMT  
 cross trim function and replace command
Thanks Dirk, and MNS.
This will take a little work, but I think I get the idea.  
Use Len to count the number of characters, then test each
one.  I also did find an article in the Knowledge
Base "How to Strip Extra Characters from a String".  From
these, I should be able to come up with a function that
can be used over and over.

Quote:
>-----Original Message-----
>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

- Show quoted text -

Quote:
>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)



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

>.



Fri, 11 Mar 2005 04:25:33 GMT  
 cross trim function and replace command

Quote:
> Thanks Dirk, and MNS.
> This will take a little work, but I think I get the idea.
> Use Len to count the number of characters, then test each
> one.  I also did find an article in the Knowledge
> Base "How to Strip Extra Characters from a String".  From
> these, I should be able to come up with a function that
> can be used over and over.

Um, I thought that's what we both just gave you:  general-purpose
functions that can be used over and over.  Assuming you put one
version or the other of these functions into a standard module, all
that's missing is a call to the function from your code or from an
update query, specifying the appropriate arguments.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)



Fri, 11 Mar 2005 04:36:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. replace function: start keyword also a left trim?

2. Underlining in body of cross tab report and trimming characters

3. removing and replacing cross-reference field codes to text

4. removing and replacing cross-reference field codes to text

5. vb shell command to replace dos command

6. Trim() command not working properly

7. Difference between trim and trim$

8. Trim not trimming?

9. Trim() vs Trim$(), etc

10. Equivalent functionality to trim - but with a differenct character set trimmed

11. Trim vs Trim$

12. Graphics control with point() equivalent function and replace color functions

 

 
Powered by phpBB® Forum Software