Results 1 to 7 of 7
  1. #1
    Yap is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Dec 2010
    Posts
    5

    Unusual string


    I have a an usual string that needs to be splited. Here's a sample:
    XT225.00AJ516.00YQ45.11EU26.88VJ143.62US23.65YC 30.10XY21.50XA10.75AY

    It should split to this:
    XT 225.00 AJ 516.00 YQ 45.11 EU 26.88 VJ 143.62 US 23.65 YC 30.10 XY 21.50 XA 10.75 AY

    Any ideas?

  2. #2
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Is the result also one long string or do you need separate strings?

    One method - maybe the only way - is to read each character starting from the first and identify numeric to alphabetic (and vice versa) changes, inserting a space at these points. The decimal points complicate things a little.

    I'll play with some code and get back to you.

  3. #3
    Yap is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Dec 2010
    Posts
    5
    Each as seperate string.

    I was also thinking about reading each chr since the only way to detect the change is when it switches from text to number or space. Did you notice a space between YC and 30? The space is not static at that location, it can appear anywhere in the string.

  4. #4
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Here you go.

    Code:
    Public Function ConvertString(rstrString As String) As String
    
        Dim i As Integer
        
        rstrString = Trim(rstrString)
        If Len(rstrString) <= 1 Then ConvertString = rstrString: Exit Function
        
        ConvertString = ""
        For i = 1 To Len(rstrString)
            ConvertString = ConvertString & Mid(rstrString, i, 1)
            If i < Len(rstrString) Then
                If Mid(rstrString, i, 1) Like "[a-z,A-Z]" And Mid(rstrString, i + 1, 1) Like "[0-9]" Then
                    ConvertString = ConvertString & " "
                ElseIf Mid(rstrString, i, 1) Like "[0-9]" And Mid(rstrString, i + 1, 1) Like "[a-z,A-Z]" Then
                    ConvertString = ConvertString & " "
                End If
            End If
        Next
        
    End Function
    P.S. Just seen your second post. No, I didn't see the space and my published solution returns one string. However this can easily be adapted to split into an array. I'll modify my code.
    Last edited by Rod; 12-13-2012 at 08:14 PM. Reason: Added PS

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Do you want to extract these values for saving to separate fields in a table?

    The alpha parts are always 2 characters?

    The first alphanumeric pair is easy.

    Left([stringfield],2)

    Format(Val(Mid([stringfield],3)),"0.00")

    After that, have to whittle away the parts. Take the length of the first pair and use that to extract remainder of the string. Repeat. Until Rod comes back, consider:
    Code:
    Sub BreakString()
    Dim x As String, y As String, z As String, S As String
    x = Replace("XT225.00AJ516.00YQ45.11EU26.88VJ143.62US23.65YC 30.10XY21.50XA10.75AY", " ", "")
    Do While InStr(x, ".") > 0
        y = Left(x, 2)
        z = Format(Val(Mid(x, 3)), "0.00")
        Debug.Print y & " " & z
        x = Mid(x, Len(y & z) + 1)
        S = S & y & " " & z & " "
    Loop
    Debug.Print Right(x, 2)
    S = S & Right(x, 2)
    Debug.Print S
    End Sub
    EDIT: Rod already back. As you can see, more than one way ...
    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.

  6. #6
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Modified:

    Code:
    Public Function ConvertString(rstrString As String) As Variant
        Dim i As Integer
        Dim strWork As String
        
        rstrString = Trim(rstrString)
        rstrString = Replace(rstrString, " ", "")
        
        
        If Len(rstrString) <= 1 Then strWork = rstrString: GoTo Exit_Procedure
        
        strWork = ""
        For i = 1 To Len(rstrString)
            strWork = strWork & Mid(rstrString, i, 1)
            If i < Len(rstrString) Then
                If Mid(rstrString, i, 1) Like "[a-z,A-Z]" And Mid(rstrString, i + 1, 1) Like "[0-9]" Then
                    strWork = strWork & ":"
                ElseIf Mid(rstrString, i, 1) Like "[0-9]" And Mid(rstrString, i + 1, 1) Like "[a-z,A-Z]" Then
                    strWork = strWork & ":"
                End If
            End If
        Next
        
    Exit_Procedure:
        ConvertString = Split(strWork, ":")
        Exit Function
        
    End Function
    Use this call to test:

    Code:
        Dim var As Variant
        Dim i As Integer
        var = ConvertString("XT225.00AJ516.00YQ45.11EU26.88VJ143.62US23.65YC 30.10XY21.50XA10.75AY")
        If IsArray(var) Then
            MsgBox UBound(var)
            For i = 0 To UBound(var)
                MsgBox var(i)
            Next
        End If
    P.S. Hi June - as I often say, 'Give ten programmers a problem and you will end up with eleven solutions.'
    Last edited by Rod; 12-13-2012 at 10:16 PM. Reason: Added PS

  7. #7
    Yap is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Dec 2010
    Posts
    5
    Yes, the alpha part is alway 2 letters. The extracted values are to be saved to separate fields in a table. I'll give it a try.

    Thanks.

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

Similar Threads

  1. Importing Text - Unusual delimiters
    By Jackfam58 in forum Programming
    Replies: 4
    Last Post: 08-07-2012, 06:33 PM
  2. Vba string
    By rchtan in forum Programming
    Replies: 5
    Last Post: 05-17-2012, 04:29 AM
  3. Unusual Sub Totals & Totals in Groups.
    By Robeen in forum Reports
    Replies: 12
    Last Post: 03-20-2012, 08:55 AM
  4. SQL + VBA String
    By jgelpi16 in forum Programming
    Replies: 2
    Last Post: 08-22-2011, 08:44 AM
  5. Percent textbox unusual behaviour
    By Deutz in forum Forms
    Replies: 4
    Last Post: 06-05-2011, 06:46 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