Results 1 to 6 of 6

Thread: VB Split Function

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    10

    Question VB Split Function

    Hello all-

    I have a split function for seperating out a csv formatted text file. (Fields seperated by commas with Double Quotes around text). The issue is that many of the fields have descriptive text where someone uses a comma. How do I make sure the split function ignores those commas?

    -----Code that does the initial split
    aTmp = Split(aData(b), chr(44))

    -----Code that runs the sql string and gets rid of the double quotes.
    sValueList = sValueList & ", " & SQLString(aTmp(aFieldPositionList(c) - 1))

    -----Function being called above
    function SQLString(sString)

    sTmp = Trim(sString)
    if Len(sTmp) then
    SQLString = "'" & Replace(Replace(sTmp, chr(34), ""), "'", "''") & "'"
    else
    SQLString = "null"
    end if

    end function

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    that is an awkward part of the split - you can't tell it which parts to ignore. There are two possible solutions that spring to mind:

    1) Write your own split function that ignores the commas that are between quotes. (or search this forum for a replacement)

    2) Convert the comma characters between quotes to a character that you know isn't going to be inside the quotes, then split it, and convert the character back to commas afterwards in your SQLString function.


    The first will always give you the right values (in the second, if the character you choose is in the string, it will be changed to a comma).


    Luckily for you, VB 5 didn't have Split, so several people wrote their own; here's one version of it, which will be a good starting point for option 1:
    VB Code:
    1. Function VB5_Split(TheString, Optional Delim, Optional Limit As Long = -1, Optional ignored) As Variant
    2. 'FROM PSC. - Duplicates the functionality of the vb6 counterpart.
    3. 'Unfortunately, I was unable to include the vbcompare part _
    4.  of the vb6 funtionality.
    5. 'Just use Option Campare at the beggining of this module.
    6.  
    7. Dim dynArray() As Variant
    8.   If Not (IsNull(TheString)) And Len(Delim) > 0 Then
    9.       Dim ArrCt%
    10.       Dim CurPos%
    11.       Dim LenAssigned%
    12.       Dim CurStrLen%
    13.       ArrCt% = 0
    14.       CurPos% = 1
    15.       LenAssigned% = 1
    16.       CurStrLen% = Len(TheString)
    17.       Do
    18.           ReDim Preserve dynArray(0 To ArrCt%)
    19.           CurStrLen% = (InStr(CurPos%, TheString, Delim) - CurPos%)
    20.           If CurStrLen% < 0 Then
    21.               dynArray(ArrCt%) = Right$(TheString, (Len(TheString) - (LenAssigned% - 1)))
    22.               Exit Do
    23.           Else
    24.               dynArray(ArrCt%) = Mid$(TheString, CurPos%, CurStrLen%)
    25.           End If
    26.           LenAssigned% = LenAssigned% + (Len(dynArray(ArrCt%)) + Len(Delim))
    27.           CurPos% = LenAssigned%
    28.           ArrCt% = ArrCt% + 1
    29.           If Not Limit = -1 Then
    30.               If ArrCt = Limit Then Exit Do
    31.           End If
    32.       Loop
    33.       VB5_Split = dynArray
    34.   Else
    35.       'duplicate the functionality more acuratley
    36.       ReDim dynArray(0 To 0)
    37.       dynArray(0) = TheString
    38.       VB5_Split = dynArray
    39.   End If
    40.  
    41. End Function

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    10

    Lightbulb Resolved

    Hello all-

    Found the fix.

    aTmp = Split(aData(b), """,""")

    This was much simpler than creating some replace object before and after the input also makes it easier to know the difference between a seperator or a comma in a description type field.

    Thanks
    James F.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Resolved

    Originally posted by jfergy

    aTmp = Split(aData(b), """,""")
    That's fine as long as ALL the fields have quotes around them

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    10

    Thanks

    Yep it does work with double quotes. The other suggestions would not work. Run through what they were then test it out and you can see why.

  6. #6
    Lively Member
    Join Date
    Jun 2003
    Posts
    83
    There is no simple solution to this. CSV only creates quotes on string data. Besides that, even if you're using all strings, your leading and trailing quotes will be ignored on each line.

    The only way to accurately parse the CSV file for its data is to create a parser (or instantiate a spreadsheet object that will parse it for you).

    EDIT: Try the Microsoft Excel Object Library
    - Steve

    "I'm glad I wore these pants."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width