Extracting Information from a CSV File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kjflash
    New Member
    • Jan 2007
    • 9

    Extracting Information from a CSV File

    Hi,

    I've got the following in a txt file (it could just as easily be a csv file):

    "Conroy","West" ,"1618 TAMARACK ROAD","","OWENS BORO","KY","423 01-1234","11/05/2005","27068244 54","6240","321 ","ENFRB"," Y"
    "Larry","Todd", "1419 Cleveland Blvd","","OWENS BORO","KY","423 01-1234","11/05/2005","27068244 54","6240","321 ","ENFRB"," Y"

    Each record is on it's own line. How would I extract the information from each field for each record?

    Thanks,

    Keith
  • danp129
    Recognized Expert Contributor
    • Jul 2006
    • 323

    #2
    Originally posted by kjflash
    Hi,

    I've got the following in a txt file (it could just as easily be a csv file):

    "Conroy","West" ,"1618 TAMARACK ROAD","","OWENS BORO","KY","423 01-1234","11/05/2005","27068244 54","6240","321 ","ENFRB"," Y"
    "Larry","Todd", "1419 Cleveland Blvd","","OWENS BORO","KY","423 01-1234","11/05/2005","27068244 54","6240","321 ","ENFRB"," Y"

    Each record is on it's own line. How would I extract the information from each field for each record?

    Thanks,

    Keith
    Is this VB6/VBA/VBScript/VB.Net?

    CSV is a PITA to parse IMO, I usually do tab-delimited with no text qualifier when program allows it and I know there are not any tabs in the fields.

    Some programs export CSV differently than others. This will only work with the sample data you provided (VB6/VBA/VBScript).

    [CODE=vb]Private Sub Command1_Click( )
    Dim sMyFilePath As String, sMyLineofText As String
    Dim lngFile As Long

    sMyFilePath = "c:\test.tx t"

    'set lngFile variable to an value from other open files
    lngFile = FreeFile

    'open file and for input
    Open sMyFilePath For Input As #lngFile

    'read each line of the file
    Do While Not EOF(lngFile)

    'set sMyLineofText variable to the line of text VB reads from file
    Line Input #lngFile, sMyLineofText

    'do your commands based on the text in sMyLineofText

    'Remove quote at beginning and end
    If Len(sMyLineofTe xt) > 1 Then sMyLineofText = Mid(sMyLineofTe xt, 2, Len(sMyLineofTe xt) - 2)

    'split fields by ","
    artmp = Split(sMyLineof Text, """,""")

    'Check to see that line was parsed correctly, should have an exact number of expected elements
    'the array is 0 based so if you have 13 fields check for a ubound of 12
    If UBound(artmp) <> 12 Then
    MsgBox "Line not parsed correctly"
    Exit Sub
    End If

    'Loop through the fields (for example)
    For i = 0 To UBound(artmp)
    Debug.Print "Field" & i & ": " & artmp(i)
    Next 'i

    'Or specify a field manually
    Debug.Print artmp(0) '1st field (first name)
    Debug.Print artmp(1) '2nd field (last name)
    Debug.Print artmp(2) '3rd field (address)
    Debug.Print artmp(3) '4th field (address2)
    Debug.Print artmp(4) '5th field (city)
    Debug.Print artmp(5) '6th field (State)
    Debug.Print artmp(6) '7th field (zip)
    Debug.Print artmp(7) '8th field (a date)
    Debug.Print artmp(8) '9th field (??)
    Debug.Print artmp(9) '10th field (??)
    Debug.Print artmp(10) '11th field (??)
    Debug.Print artmp(11) '12th field (??)
    Debug.Print artmp(12) '13th field (??)
    Loop
    Close #lngFile
    End Sub[/CODE]

    Comment

    Working...