Connecting Tech Pros Worldwide Forums | Help | Site Map

Extracting Information from a CSV File

Newbie
 
Join Date: Jan 2007
Posts: 9
#1: Jun 7 '07
Hi,

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

"Conroy","West","1618 TAMARACK ROAD","","OWENSBORO","KY","42301-1234","11/05/2005","2706824454","6240","321","ENFRB","Y"
"Larry","Todd","1419 Cleveland Blvd","","OWENSBORO","KY","42301-1234","11/05/2005","2706824454","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's Avatar
Expert
 
Join Date: Jul 2006
Posts: 250
#2: Jun 8 '07

re: Extracting Information from a CSV File


Quote:

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","","OWENSBORO","KY","42301-1234","11/05/2005","2706824454","6240","321","ENFRB","Y"
"Larry","Todd","1419 Cleveland Blvd","","OWENSBORO","KY","42301-1234","11/05/2005","2706824454","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).

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     Dim sMyFilePath As String, sMyLineofText As String
  3.     Dim lngFile As Long
  4.  
  5.     sMyFilePath = "c:\test.txt"
  6.  
  7.     'set lngFile variable to an value from other open files
  8.     lngFile = FreeFile
  9.  
  10.     'open file and for input
  11.     Open sMyFilePath For Input As #lngFile
  12.  
  13.     'read each line of the file
  14.     Do While Not EOF(lngFile)
  15.  
  16.         'set sMyLineofText variable to the line of text VB reads from file
  17.         Line Input #lngFile, sMyLineofText
  18.  
  19.         'do your commands based on the text in sMyLineofText
  20.  
  21.         'Remove quote at beginning and end
  22.         If Len(sMyLineofText) > 1 Then sMyLineofText = Mid(sMyLineofText, 2, Len(sMyLineofText) - 2)
  23.  
  24.         'split fields by ","
  25.         artmp = Split(sMyLineofText, """,""")
  26.  
  27.         'Check to see that line was parsed correctly, should have an exact number of expected elements
  28.         'the array is 0 based so if you have 13 fields check for a ubound of 12
  29.         If UBound(artmp) <> 12 Then
  30.             MsgBox "Line not parsed correctly"
  31.             Exit Sub
  32.         End If
  33.  
  34.         'Loop through the fields (for example)
  35.         For i = 0 To UBound(artmp)
  36.             Debug.Print "Field" & i & ": " & artmp(i)
  37.         Next 'i
  38.  
  39.         'Or specify a field manually
  40.         Debug.Print artmp(0) '1st field (first name)
  41.         Debug.Print artmp(1) '2nd field (last name)
  42.         Debug.Print artmp(2) '3rd field (address)
  43.         Debug.Print artmp(3) '4th field (address2)
  44.         Debug.Print artmp(4) '5th field (city)
  45.         Debug.Print artmp(5) '6th field (State)
  46.         Debug.Print artmp(6) '7th field (zip)
  47.         Debug.Print artmp(7) '8th field (a date)
  48.         Debug.Print artmp(8) '9th field (??)
  49.         Debug.Print artmp(9) '10th field (??)
  50.         Debug.Print artmp(10) '11th field (??)
  51.         Debug.Print artmp(11) '12th field (??)
  52.         Debug.Print artmp(12) '13th field (??)
  53.     Loop
  54.     Close #lngFile
  55. End Sub
Reply