Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 745
  • Last Modified:

Read .csv file using VB6 application

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
Aquarus
Asked:
Aquarus
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Surone1Commented:
on which line does the error occur?
try    
For R = 0 To num_rows -1 'for fun!
0
 
ZhaolaiCommented:
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
 
Surone1Commented:
if num_rows is 7 loop from 0 to 6
0 to num_rows - 1
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZhaolaiCommented:
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
 
AquarusAuthor Commented:
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
 
AquarusAuthor Commented:
Let me check "her is your working code" from Zhaolai.  I did not notice his last reply.
0
 
NorieCommented:
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
 
NorieCommented:
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
 
AquarusAuthor Commented:
Thank you for the help.  
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now