Results 1 to 6 of 6
  1. #1
    J Bhujanga is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2016
    Location
    Colorado
    Posts
    27

    Character Replacement by Position in String

    I have a data field that is 20-character string called [Veh_Impact]. Each character in the string starts off by default as a zero. "00000000000000000000"
    Each of these characters happens to stand for a particular location on a car body. A zero in a given position means that part of the car is not damaged, a 1 means light damage, a 2 means moderate and a 3 means severe damage.
    So a string for a car that has been in an accident might look like this: "00000123300000000222"
    We get data from the state of Louisiana that has a whole different coding system. A "G" for example corresponds to damage at positions 5 and 6. And another field will indicate the extent of that damage. They can have several such sets for a given vehicle.
    So I run this through a converter and end up with three pieces of information for each set of damage codes which has a location, which in the example would be 5, a length which would be 2 (i.e. 5 and 6) and the damage level, which we'll say was something equivalent to our damage level 3.
    So, with this bit of data I want to change the default string to "00003300000000000000". Then with any additional set of codes they provided I'd continue to update the string.
    The problem is I can't figure out a function that does this. Essentially I want to say:
    With the string [Veh_Impact] ("00000000000000000000"), begin at position 5 and replace the next 2 characters with 3's.

    I envision something like the MID function, but one that takes an action rather than just querying, like if you could say MID([Veh_Impact],5,2)="33"
    How would one do something like this?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Break the string at the desired positions then concatenate the parts back together. Example:

    x="00000000000000000000"
    x = Left(x,4) & "33" & Mid(x,7)
    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
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    or you can convert the string to an array and work on that

    Code:
    Function insertChars(str As String, chars As String, pos As Byte) As String
    Dim arry() As String
    Dim i As Integer
    
        str = StrConv(str, vbUnicode)
        arry = Split(left(str, Len(str) - 1), vbNullChar)
        For i = 1 To Len(chars)
            arry(pos + i - 2) = Mid(chars, i, 1)
        Next i
        insertChars = Replace(Join(arry), " ", "")
        
    End Function
    
    
    Sub tst()
    Dim s As String
    
       s = "00000000000000000000"
       s = insertchars(s, "33", 5)
       Debug.Print s '00003300000000000000
    
       s = insertchars(s, "21", 10)
       Debug.Print s '00003300021000000000
    
    End Sub

  4. #4
    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,870
    I agree with the options provided by June and Ajax, but I wonder if this is just a temporary fix. It seems to me that you and those you interact with have different codification mechanisms and you are attempting to solve it "one identified position and one partner at a time".

    My best guess, since I don't know your business, is you need a general solution that involves:
    -identifying all your customers/partners (those with whom you interact)
    -identifying their codification and definition ( what the code means)
    -devise a converter function (algorithm) that takes incoming codes to a standard unified code.

    This will allow you and others to communicate using some standard terminology.

    I found this link/info via Google, and this is the kind of concept I think you and the others you deal with have to address. If this current requirement ( code translation) will be/is an ongoing situation, the sooner you resolve it "generally" to include current and future partners, the less effort and confusion involved.

    This link regarding accident collision damage codification.

  5. #5
    J Bhujanga is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2016
    Location
    Colorado
    Posts
    27
    Thanks for your replies. I think for my purposes June's idea will work the best as I want to build it into an update query.
    Unfortunately our data is provided in the form of various city and state accident report databases, so we are locked into accepting the data in their particular formats.
    Thanks.

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    not a problem, its your project, you need to decide what is best for you.

    good luck

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

Similar Threads

  1. Replies: 5
    Last Post: 05-10-2017, 12:10 PM
  2. Replies: 7
    Last Post: 02-08-2017, 03:30 PM
  3. DLookup 2nd character in a text string
    By DubCap01 in forum Forms
    Replies: 5
    Last Post: 03-06-2015, 02:37 PM
  4. Replies: 5
    Last Post: 08-25-2014, 05:33 PM
  5. How do I return last character in a string?
    By SSgtBarry in forum Queries
    Replies: 4
    Last Post: 06-22-2014, 08:10 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