Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,196

    Trim Before text

    Hi Guy's i am trying to trim all text BEFORE specific words but keep everyting AFTER these words, the code I have got deletes the full string ???



    Kindest

    Code:
    OldStr = InStr(Me.txtMailMessage, "Form Response Notification")
    MyRight = Trim(Right(Me.txtMailMessage, Mystr))
    NewStr = MyRight
    Me.txtMailMessage = NewStr
    

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,969
    InStr() returns a number which is position of first character of substring. Trim just removes spaces.

    So you do not want to retain the search words? There will always be preceding and following text?

    x represents your data.

    Trim(Mid(x, InStr(x, "Form Response Notification")+26))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,196
    Hi June7, I will try and adapt your suggestion, yes basically delete everything before "Form Response Notification" and KEEP everything after ie:

    me.txtMailMessage is:

    This was sent to you, please do not print
    This is also a message for you
    Form Response Notification
    My Name is Joe Bloggs
    My Number is 123456

    I am trying to delete:
    This was sent to you, please do not print
    This is also a message for you

    New txtMailMessage is:
    Form Response Notification
    My Name is Joe Bloggs
    My Number is 123456

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    if it is consistant that your removing "This was sent to you, please do not print This is also a message for you" you may also be able to use

    replace(
    me.txtMailMessage,"This was sent to you, please do not print This is also a message for you","")

    (you may have to include any carraige returns in the text to be replaced.)

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    As moke123 said, if there is a consistent format to the incoming info, then address that up front.
    In what format is the data received? (csv, text file???)
    There are definitions and samples of Access functions here.

  6. #6
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Looks like the Mid() function is designed for your issue.
    With assist of InStr() function, returns exactly that you want.

    You can use them in your own function as seems in sample code bellow:

    Code:
    Function StringRight(strText As String, strStartFrom As String, Optional fTrimStartText As Boolean = False) As String
        'Returns the right part of a text starting from the strStartFrom text.
        'Keeps the strStartFrom with the returned text if the third argument is False or is omited.
        Dim lngPos As Long
    
        lngPos = InStr(1, strText, strStartFrom)
        
        If lngPos > 0 Then
            If fTrimStartText Then lngPos = lngPos + Len(strStartFrom)
            StringRight = Trim(Mid(strText, lngPos))
        End If
    End Function
    
    Sub TestStringRight()
        Dim strOld As String
        Dim strStart As String
    
        strOld = "This was sent to you, please do not print" & vbCrLf _
                 & "This is also a message for you" & vbCrLf _
                 & "Form Response Notification" & vbCrLf _
                 & "My Name is Joe Bloggs" & vbCrLf _
                 & "My Number Is 123456" & vbCrLf
    
        strStart = "Form Response Notification"
        
        MsgBox StringRight(strOld, strStart)
    
        MsgBox StringRight(strOld, strStart, True)
    
        MsgBox StringRight(strOld, "My Name")
        
        MsgBox StringRight(strOld, "My Num")
    
        MsgBox StringRight(strOld, "My Number is", True)
    
    End Sub
    Good luck!

    John

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,969
    So you do want to retain the search text.

    Mid(x, InStr(x, "Form Response Notification"))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,196
    Hi June7, and thanks to accesstos yes this does start the newstring from "Form Response Notification"

    How do i stop before specific text so i am picking out text from "Form Response Notification" and stopping at "reply to Joe Bloggs"

    Can I exclude "Form Response Notification" and pick text from after that To and exclude "Reply To Joe Bloggs"

    So Bold text below is picked out

    ie:

    This is a message
    It was sent from me
    Form Response Notification
    Hello
    My name is: Joe Bloggs
    My NUmber is 123456

    Reply to Joe Bloggs
    PLease do not print
    Goodbye
    Have a nice day

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,969
    Hello was not in the original example, nor were the additional lines starting with "Reply".

    Now you show you DON'T want to retain "Form Response Notification" and also show more text at end you want to eliminate.

    Consistency of structure is critical in string manipulation. Will Hello and Reply always be in the string?

    Mid(Left(x, InStr(x, "Reply")-1), InStr(x, "Hello"))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,196
    Hu June7, thank you, i am using words as examples rather than what is officially recieved but thank you for response

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    Dave,
    As mentioned earlier, please tell us the format of the incoming data and the context in which data is sent to you from somewhere for your manipulation/processing.
    We see posts where people receive data from some other system or supplier in a text file format (.csv,.txt etc). Others get data from web sites or external sources; some from email...
    You know the source of the data and the context and readers don't, so please provide a brief description of the process for receiving your data. At the moment the details are unclear and changing as June has said.
    Good luck.

  12. #12
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,196
    Hi Orange, its email grabbed into a text box txtMailMessage, this is set to long text in the table, so i am trying to grab data from "form response notification" up to "reply to customer" because in between these 2 sets of words there are data than can be distributed to other access fields by the click of a button, so i i can get rid of everything before and after that would make it easier, this is sent to us via an automated email system froma client so i can't really ask them to take out this for our database to respond easier without who sent it, what server sent it, etc etc..... kindest

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    So is there any option for the client to add info/words/blank lines etc in the email that gets sent automatically?
    Can you provide say 4 or 5 of these actual text responses for readers to see?

    Are you getting these from Outlook or other?

  14. #14
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Well Dave, this is a multifunctional function specially designed for your needs but I hope useful to others:
    Code:
    Function StringPart(ByVal strText As String, _
                               Optional strFrom As String, _
                               Optional strTo As String, _
                               Optional ExFrom As Boolean = False, _
                               Optional ExTo As Boolean = False) As String
        'Returns the part of strText between strFrom and strTo strings.
        'If strFrom is omited, works as Left function.
        'If strTo is omited, works as Right function.
        'If both strFrom and strTo are passed, works as Mid function.
        'If ExFrom is True, exclude the strFrom of the returned value.
        'If ExTo is True, exclude the strTo of the returned value.
        
        Dim strMid As String
        Dim lngPos As Long
        Dim lngLen As Long
    
        strMid = strText
    
        'Right function.
        lngLen = Len(strFrom)
        If lngLen > 0 Then
            lngPos = InStr(1, strMid, strFrom)
            If lngPos > 0 Then
                If ExFrom Then lngPos = lngPos + lngLen + 1
                strMid = Trim$(Mid$(strMid, lngPos))
            End If
        End If
        'Left function.
        lngLen = Len(strTo)
        If lngLen > 0 Then
            lngPos = InStr(1, strMid, strTo)
            If lngPos > 0 Then
                If Not ExTo Then lngPos = lngPos + Len(strTo) + 1
                strMid = Trim$(Left$(strMid, lngPos - 1))
            End If
        End If
        StringPart = strMid
    End Function
    Run the procedure below and check the results.
    Code:
    Sub TestStringPart()
        Dim strOld As String
        Dim strStart As String
        Dim strStop As String
    
        strOld = "This was sent to you, please do not print" & vbCrLf _
                 & "This is also a message for you" & vbCrLf _
                 & "Form Response Notification" & vbCrLf _
                 & "My Name is Joe Bloggs" & vbCrLf _
                 & "My Number Is 123456" & vbCrLf _
                 & "Reply to Joe Bloggs" & vbCrLf _
                 & "PLease do not print" & vbCrLf _
                 & "Goodbye" & vbCrLf _
                 & "Have a nice day"
    
        strStart = "Form Response Notification"
        strStop = "Reply to"
    
        MsgBox "strFrom = '" & strStart & "'" & vbCrLf & vbCrLf & StringPart(strOld, strStart), , "Right function"
        MsgBox "strFrom = '" & strStart & "'" & vbCrLf & "ExFrom=True" & vbCrLf & vbCrLf & StringPart(strOld, strStart, , True), , "Right function"
        MsgBox "strTo = '" & strStop & "'" & vbCrLf & vbCrLf & StringPart(strOld, , strStop), , "Left function"
        MsgBox "strTo = '" & strStop & "'" & vbCrLf & "ExTo=True" & vbCrLf & vbCrLf & StringPart(strOld, , strStop, , True), , "Left function"
        MsgBox "strFrom = '" & strStart & "'" & vbCrLf & "strTo = '" & strStop & "'" & vbCrLf & vbCrLf & StringPart(strOld, strStart, strStop), , "Mid function"
        MsgBox "strFrom = '" & strStart & "'" & vbCrLf & "strTo = '" & strStop & "'" & vbCrLf & "ExFrom=True" & vbCrLf & "ExTo=True" & vbCrLf & vbCrLf & StringPart(strOld, strStart, strStop, True, True), , "Mid function"
          
    End Sub
    Cheers,
    John

  15. #15
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    its email grabbed into a text box txtMailMessage, this is set to long text in the table,
    Is the email plain text or html? Rich text?

    Just to be sure, I would check to see if your seeing everything in the string. For instance if its html or rich text, there may be formatting you cant see.
    I usually use this simple function that debug.prints the ascii code of each character including the non-printable ones (ie. chr(10) & chr(13) or <div>)
    It may save you a world of frustration.

    Code:
    Public Sub sChkNonPrint(strCheck)
        Dim lngLoop As Long
    
    
        For lngLoop = 1 To Len(strCheck)
            Debug.Print Mid(strCheck, lngLoop, 1) & ": " & Asc(Mid(strCheck, lngLoop, 1))
        Next lngLoop
    
    
    End Sub
    @Orange: I hope your not in W.P.B. with the storm coming. If you are, stay safe and could you go check my place in Jupiter next week?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 01-25-2015, 02:10 AM
  2. Replies: 7
    Last Post: 06-20-2014, 08:55 PM
  3. Trim!
    By redbull in forum Programming
    Replies: 9
    Last Post: 11-06-2012, 06:01 AM
  4. how to trim text in a column using sql query of ms access
    By learning_graccess in forum Access
    Replies: 4
    Last Post: 12-05-2011, 07:15 AM
  5. Trim value
    By dada in forum Programming
    Replies: 5
    Last Post: 09-02-2010, 11:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums