Solved

Read .csv file using VB6 application

Posted on 2010-09-14
9
684 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
Comment Utility
on which line does the error occur?
try    
For R = 0 To num_rows -1 'for fun!
0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
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
Comment Utility
if num_rows is 7 loop from 0 to 6
0 to num_rows - 1
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Aquarus
Comment Utility
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
Comment Utility
Let me check "her is your working code" from Zhaolai.  I did not notice his last reply.
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for the help.  
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now