Reading a .CSV File in VB6

The CSV file specs I followed can be read into / written from MS-Excel

Public Function ParseLineCSVExcel(ByRef strLine As String) As String

   'strLine will be altered witin this procedure and sent back.......
  
   'Excel's formula SEEMS to be when there are quotes & comma all mixed up is that
   '  an ODD number of quotes before a comma means that the comma IS DATA
   '     EVEN  --------------------------------------------------------------------------- A DELIMETER
   '     ODD  => open, IT IS DATA
   '     EVEN => closed,  valid delimeter
  
Const ASCIISPACE As Long = 32                                                     '( )
Const ASCIIQUOTE As Long = 34                                                    '(")
Const ASCIICOMMA As Long = 44                                                 '(,)

Dim iStrlen As Integer
Dim bOpen As Boolean                                                                       'If in open mode, then delimeters are actual data
Dim strBuild As String
Dim chChar As String * 1

iStrlen = Len(strLine)
If iStrlen = 0 Then                                                                                'nothing to do, but do not raise an error
   ParseLineCSVExcel = ""                                                                 'pass an empty string back
   Exit Function
End If

'Brought this in from orig parse csv
If iStrlen = 1 And (Asc(strLine) = 34 Or Asc(strLine) = 44) Then
   strLine = ""
   ParseLineCSVExcel = ""
   Exit Function
End If

'Most bogus calls were terminated above, so now it's time to work!

strBuild = ""                                                                                          'Empty the output build variab;e
bOpen = False                                                                                        'Treat 0 as an EVEN number of quotes

chChar = Left$(strLine, 1)
Do While iStrlen > 0
   If Asc(chChar) = ASCIIQUOTE Then
      If iStrlen = 1 Then                                                                                'Assume it's the end of the data line
         ParseLineCSVExcel = strBuild  'loose the last quote
         Exit Function
      End If                                                                                                   'Anything below this is essentally an "ELSE" clause
      bOpen = Not bOpen                                                                           'Toggle the flag
   End If
  
   If (Asc(chChar) = ASCIICOMMA) And (Not bOpen) Then               'Data closed, it's a delimeter
      strLine = Mid$(strLine, 2)                                                                  'Dump the delimeting comma
      If Len(strBuild) > 0 Then
         If Asc(Right$(strBuild, 1)) = ASCIIQUOTE Then
            strBuild = Left$(strBuild, (Len(strBuild) - 1))                             'Return the built string *WITHOUT* ending quote
         End If
      End If
      ParseLineCSVExcel = strBuild                                                         'Return the built string *WITHOUT* ending quote
      Exit Function
   End If
  
'Need to drop opening "
   If Not ((Asc(chChar) = ASCIIQUOTE) And Len(strBuild) = 0) Then    'Kill opening double quote
      strBuild = strBuild + chChar                                                                  'Add the char to the line
   End If
   strLine = Mid$(strLine, 2)                                                                        'the input string becomes the "rest" of the line (first char dropped)
   chChar = Left(strLine, 1)
   iStrlen = Len(strLine)                                                                               'Why keep a counter? This is more accurate!
Loop

'last without an ending delimeter exits loop whe the remaining string is 0, so
'   while other field exit the function before loop termination when the fiekd delimeter is found
'   so this last bit takes csre of the last field data that has no ending delimeter
If Len(Trim(strBuild)) > 0 Then
   ParseLineCSVExcel = strBuild
Else
   ParseLineCSVExcel = ""
End If
Exit Function

ParseLineCSVExcelOops:
   MsgBox Str(Err.Number) + " " + Err.Description, "Parser Error"
'   Debug.Print strBuild

End Function


Revised   Jan 13, 2010