Solved

Read .csv file using VB6 application

Posted on 2010-09-14
9
708 Views
Last Modified: 2012-05-10
I have googled this code to read .csv file.  But it constantly giving me an error "Subscript is out of range".  I need to understand if this method is usefull.

This is the code:
Private Sub cmdImportMessage2_Click()
    'txtMSGFile.Text
   
    Dim file_name As String
Dim fnum As Integer
Dim whole_file As String
Dim lines As Variant
Dim one_line As Variant
Dim num_rows As Long
Dim num_cols As Long
Dim the_array() As String
Dim R As Long
Dim C As Long

    file_name = txtMSGFile.Text ' App.Path
''    If Right$(file_name, 1) <> "\" Then file_name = _
''        file_name & "\"
''    file_name = file_name & "test2.csv"

    ' Load the file.
    fnum = FreeFile
    Open file_name For Input As fnum
    whole_file = Input$(LOF(fnum), #fnum)
    Close fnum

    ' Break the file into lines.
    lines = Split(whole_file, vbCrLf)

    ' Dimension the array.
    num_rows = UBound(lines)
    one_line = Split(lines(0), ",")
    num_cols = 220 'UBound(one_line)
    ReDim the_array(num_rows, num_cols)

    ' Copy the data into the array.
    For R = 0 To num_rows
        one_line = Split(lines(R), ",")
        For C = 0 To num_cols
          Debug.Print "R =" + CStr(R), "C=" + CStr(C)
            the_array(R, C) = one_line(C)
        Next C
    Next R

    ' Prove we have the data loaded.
    For R = 0 To num_rows
        For C = 0 To num_cols
            Debug.Print the_array(R, C) & "|";
        Next C
        Debug.Print
    Next R
    Debug.Print "======="

End Sub


Attached is the sample file I am working with.
Please help


AM-001-3.CSV
0
Comment
Question by:Aquarus
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 13

Expert Comment

by:Surone1
ID: 33677321
on which line does the error occur?
try    
For R = 0 To num_rows -1 'for fun!
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33677512
The number of columns are not the same in the csv file.
Try this:

Private Sub cmdImportMessage2_Click()
    'txtMSGFile.Text
Dim file_name As String
Dim fnum As Integer
Dim whole_file As String
Dim lines As Variant
Dim one_line As Variant
Dim num_rows As Long
Dim num_cols As Long
Dim the_array() As String
Dim R As Long
Dim C As Long

    file_name = txtMSGFile.Text  ' App.Path
''    If Right$(file_name, 1) <> "\" Then file_name = _
''        file_name & "\"
''    file_name = file_name & "test2.csv"

    ' Load the file.
    fnum = FreeFile
    Open file_name For Input As fnum
    whole_file = Input$(LOF(fnum), #fnum)
    Close fnum

    ' Break the file into lines.
    lines = Split(whole_file, vbCrLf)

    ' Dimension the array.
    num_rows = UBound(lines)
    num_cols = 230 'UBound(one_line)
    ReDim the_array(num_rows, num_cols)

    ' Copy the data into the array.
    For R = 0 To num_rows
        one_line = Split(lines(R), ",")
        For C = 0 To UBound(one_line)
          Debug.Print "R =" + CStr(R), "C=" + CStr(C)
            the_array(R, C) = one_line(C)
        Next C
    Next R

    ' Prove we have the data loaded.
    For R = 0 To num_rows
        For C = 0 To num_cols
            Debug.Print the_array(R, C) & "|";
        Next C
        Debug.Print
    Next R
    Debug.Print "======="

End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:Surone1
ID: 33677591
if num_rows is 7 loop from 0 to 6
0 to num_rows - 1
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33682841
All right, here is your working code:
The reason for the error is that in your .csv file, some numeric fields are formatted like "10,000.00" which is ONE number, but when using Split method to split the line by delimiter of comma (","), that number is split into 2 fields, which is wrong.

Private Sub cmdImportMessage2_Click()
    'txtMSGFile.Text
   
Dim file_name As String
Dim fnum As Integer
Dim whole_file As String
Dim lines As Variant
Dim one_line As Variant
Dim num_rows As Long
Dim num_cols As Long
Dim the_array() As String
Dim R As Long
Dim C As Long

    file_name = txtMSGFile.Text  ' App.Path
''    If Right$(file_name, 1) <> "\" Then file_name = _
''        file_name & "\"
''    file_name = file_name & "test2.csv"

    ' Load the file.
    fnum = FreeFile
    Open file_name For Input As fnum
    whole_file = Input$(LOF(fnum), #fnum)
    Close fnum

    ' Break the file into lines.
    lines = Split(whole_file, vbCrLf)

    ' Dimension the array.
    num_rows = UBound(lines)
    num_cols = 219 'UBound(one_line)
    ReDim the_array(num_rows, num_cols)
    Dim arrSub() As String
    Dim i As Integer
    Dim arrsub1() As String
    Dim count As Integer
    ' Copy the data into the array.
    For R = 0 To num_rows
        count = 0
        one_line = Split(lines(R), "," & Chr(34))
        For C = 0 To UBound(one_line)
            If InStr(1, one_line(C), Chr(34) & ",") > 0 Then
                arrSub = Split(one_line(C), Chr(34) & ",")
                the_array(R, count) = Trim(arrSub(0))
                count = count + 1
                If UBound(arrSub) = 1 Then
                    arrsub1 = Split(arrSub(1), ",")
                    For i = 0 To UBound(arrsub1)
                        the_array(R, count) = Trim(arrsub1(i))
                        count = count + 1
                    Next
                End If
            Else
                If Right(one_line(C), 1) = Chr(34) Then
                    the_array(R, count) = Trim(Replace(one_line(C), Chr(34), ""))
                    count = count + 1
                Else
                    arrSub = Split(one_line(C), ",")
                    For i = 0 To UBound(arrSub)
                        the_array(R, count) = Trim(arrSub(i))
                        count = count + 1
                    Next
                End If
            End If
        Next C
    Next R

    ' Prove we have the data loaded.
    For R = 0 To num_rows
        For C = 0 To num_cols
            Debug.Print the_array(R, C) & "|";
        Next C
        Debug.Print
    Next R
    Debug.Print "======="

End Sub

Open in new window

0
 

Author Comment

by:Aquarus
ID: 33682951
Unfortunately I cannot use neither of the suggestions because in the Name fields ( there are few of them across the line) there are all kind of names and othes stuff.  I thought of using the Excel object.  Can you suggest any code sample to complete the task, so I can read column by column?
0
 

Author Comment

by:Aquarus
ID: 33683336
Let me check "her is your working code" from Zhaolai.  I did not notice his last reply.
0
 
LVL 33

Expert Comment

by:Norie
ID: 33685365
You could perhaps use Excel to import the CSV and it might be able to deal with things like commas in numbers, since Excel sort of expects that type of data.

You might also consider using ADO but I'm not sure if that would be as well-equipped as Excel to deal with the anomalies.
0
 
LVL 33

Expert Comment

by:Norie
ID: 33685394
Actually I just downloaded you file and opened it in Excel and everything seemed find.

Obviously I did that manually but I don't think the code would be too difficult to come up with.

Now to go test the ADO idea...
0
 

Author Closing Comment

by:Aquarus
ID: 33686658
Thank you for the help.  
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question