Results 1 to 15 of 15
  1. #1
    bjelinski is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    13

    erase characters

    I have data that represents certain ID CODE. This ID CODE has some unnecessary characters. The last star with letters (i.e. *DG or *HSL etc. ) should be removed.

    In attachment I have included a mock up of the sample data. The important part is ID CODE column. As you can see there are more stars in the code, but the relevant part is to erase the last star together with the letters after the star.
    Example: This ID CODE - 63047450201*RO*Kolis*RBBG*LIM would become THIS 63047450201*RO*Kolis*RBBG


    I did some trial and error , and created another query and used mid formula, but this simple approach is not dynamic. Because we dont know on what position is the last star (*).



    Thanks in advance,

    Bjelinski
    Last edited by bjelinski; 07-05-2010 at 12:07 PM.

  2. #2
    NTC is offline VIP
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    2,392
    manipulating text strings is an interesting project. You are on the right track with the mid function.

    in the end you are going to need to create a mix of Mid, Trim, Count.....you can count to the first * and then once that number is established - use that number in your Trim or Mid functions.

    its tricky but do-able..... some prefer to do it all inside vb - - but I've done it successfully in a query with side by side columns doing sections of the logic so that one can see the result at each step; which helps tweak it to be correct for every variation.....

  3. #3
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Here's a function that starts at the end of a String and counts backwards until it finds an asterisk (character 42). Then it cuts off everything from that point to the end of the string.

    Code:
      Dim intCurrChar, intTrimAt as Integer
    
      intTrimAt = -1
      intCurrChar = Len([ID CODE]) - 1
    
      Do While intCurrChar > 0
        If Left([ID CODE], intCurrChar, 1) = Chr(42) Then
          intTrimAt = intCurrChar
          intCurrChar = 0
        End If
    
        intCurrChar = intCurrChar - 1
      Loop
    
      If intTrimAt > -1 Then
        [ID CODE] = LEFT([ID CODE], 0, intTrimAt)
      End If
    You'll need to figure out the parts in Bold, but everything else should work.
    Last edited by Rawb; 07-01-2010 at 08:06 AM. Reason: Whoops, infinite loop FTL! :(

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The last star can be found easily with the InStrRev() function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Quote Originally Posted by pbaldy View Post
    The last star can be found easily with the InStrRev() function.
    Well yeah. . .

    If you wanna do it the easy way.

  6. #6
    NassauBob's Avatar
    NassauBob is offline Not THAT Green
    Windows Vista Access 2007
    Join Date
    Feb 2010
    Location
    Augusta, GA
    Posts
    61
    Quote Originally Posted by Rawb View Post
    Well yeah. . .

    If you wanna do it the easy way.

    Yeah, where's the fun in that? LOLOL

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sorry; it's my extreme laziness rearing its ugly head.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    bjelinski is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    13
    Hello,

    thank you on valuable responses. However I am still trying to find a way to make the formula in a new query that would be able to the above task.

    In excel I used the following formula, and now I want to make some type of similar thing in access.

    =LEFT(C2;FIND("^^^";SUBSTITUTE(C2;"*";"^^^";LEN(C2 )-LEN(SUBSTITUTE(C2;"*";""))))-1)

    D2 is the part where ID_CODE starts.

    For find function I wanted to use InStrRev and for substitute I wanted to use replace. Hm but I am not sure, because access even changes the places of parameters required

  9. #9
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    I have created a function below which will remve the *and last three letters.

    I am attaching a sample mdb file. In it you will find a query named Query 1
    run it and the data in you table will change.


    Option Compare Database
    Dim strmyString As String
    Dim intLength As Integer
    Dim intExitIndex As Integer
    Function AdjustedString(strmyString) As String
    If IsNull(strmyString) Then
    Exit Function
    End If

    intLength = Len(myString)
    intExitIndex = InStrRev(strmyString, "*")

    AdjustedString = Left$(strmyString, intExitIndex - 1)
    End Function

  10. #10
    bjelinski is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    13
    Thanks for the help, but it tells me unrecognized database format?!

  11. #11
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    check this one

  12. #12
    bjelinski is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    13
    I don’t know why? But I still get the message? strange

  13. #13
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    I can open it just fine. And since you have a newer version of Access than I do, you shouldn't be having any trouble.

    Strange.

  14. #14
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    The most strange thing is that its the database that you had posted.

    Well put the function in a module and then in the sql view of a query copy paste this:

    UPDATE Data SET Data.[ID CODE] = AdjustedString([ID Code]);
    then save the query and run it.

    Now you don't need to download the attached mdb.

  15. #15
    bjelinski is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    13
    Thanks for the effort! everything worked fine! In the process I learned few new things, that part is of course priceless

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need to delete special characters
    By tlrutledge in forum Queries
    Replies: 1
    Last Post: 08-23-2013, 03:10 AM
  2. characters in text boxes
    By Matthieu in forum Access
    Replies: 0
    Last Post: 04-08-2010, 11:02 AM
  3. replace characters in a string
    By blazixinfo@yahoo.com in forum Access
    Replies: 6
    Last Post: 08-06-2009, 03:36 PM
  4. Special Characters
    By orgelizer in forum Access
    Replies: 0
    Last Post: 03-20-2007, 08:24 PM
  5. Strings limited to 255 characters
    By ocordero in forum Programming
    Replies: 4
    Last Post: 08-09-2006, 09:13 AM

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