开发者

VB6 comma separating CSV file

I am trying to separate a comma delimited file and for some reason I seem to not be getting the output that I was expecting to get.

Here is the code:

strCSVPath = "E:\cfaApp\tester.csv"
int77 = FreeFile
Open strCSVPath For Input As #int77

Do Until EOF(int77)
    Input #int77, strName, intHours, strMon, strTue, strWed, strThu, strFri, strSat
    Debug.Print strName & vbCr & intHours
    'Debug.Print strName & vbCr & intHours & vbCr & strMon & vbCr & strTue & vbCr & strWed & vbCr & strThu & vbCr & strFri & vbCr & strSat & vbCr
Loop

The output looks like this:

1.0-Store Manager (1 Employee)

11:00 AM-8:30 PM

10:00 AM-7:30 PM
1.1-Assistant Managers
Wood, Chris

Above output is wrong. This is the CSV file (some of it, not all of it)

1.0-Store Manager (1 Employee),,,,,,,,
"Pro, Bob",1.0-Store Manager,47.5,5:30 AM-3:00 PM,5:30 AM-3:00 PM,11:00 AM-8:30 PM,11:00 AM-8:30 PM,9:00 AM-6:30 PM,OFF
1.1-Assistant Managers (3 Employees),,,,,,,,
"Crow, Billy",1.1-Assistant Mana开发者_JAVA百科gers,47.5,10:00 AM-7:30 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM,OFF,11:00 AM-8:30 PM
"Ras, Pat",1.1-Assistant Managers,47.5,5:30 AM-3:00 PM,9:00 AM-6:30 PM,10:00 AM-7:30 PM,,11:00 AM-8:30 PM,9:00 AM-6:30 PM
"Wood, Chris",1.1-Assistant Managers,47.5,,11:00 AM-8:00 PM,8:30 AM-6:30 PM,10:00 AM-7:30 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM
1.2- Supervisors (7 Employees),,,,,,,,

As you can see, the output does skips Pro, Bob altogether. And then skips Crow and Ras before displaying Wood.

I also get an error of

 Input past end of file


In short, Input # is not designed to read CSV. See http://msdn.microsoft.com/en-us/library/aa243386(v=VS.60).aspx

There may be an external CSV parser you could use from VB6 but I don't know of one offhand. You could write a CSV parser fairly easily that gets one character at a time and uses a finite state machine.


It's been a while since I have used VB6, but I happened to remember that it is possible to use the ADODB.Recordset object to query the CSV file like a SQL database.

Here is Microsoft documentation, including how to read CSV files without header rows.


Try using a other separator like ; or Pipe. Then see if the output is okay. If YES try to quote the commas in the name or set the name in other quotes like ' Have Fun, John

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜