Solved

Read .csv file using VB6 application

Posted on 2010-09-14
9
715 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

828 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