Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read .csv file using VB6 application

Posted on 2010-09-14
9
Medium Priority
?
742 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
LVL 17

Accepted Solution

by:
Zhaolai earned 2000 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 34

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 34

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

Industry Leaders: 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!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

704 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