• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Why First Recor Contain The Heder Of Field

luca90

New Member
>>> use code - tags <<<
Code:
Option Explicit
Sub Main()

    Dim CON As ADODB.Connection
    Dim RS As ADODB.Recordset
    Dim SQL As String
    Dim thePath As String
    'C:\TABULATI\TEST_ISTAT.CSV

    Set CON = New ADODB.Connection
    Set RS = New ADODB.Recordset

   
    thePath = "C:\TABULATI\"

    CON.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & thePath & ";" _
                          & "Extended Properties=""text;HDR=no;"""
    CON.Open

   
    SQL = "SELECT * FROM TEST_ISTAT.CSV"

    RS.CursorLocation = adUseClient
    RS.Open SQL, CON, adOpenForwardOnly, adLockReadOnly

    Do While Not RS.EOF
       
        Debug.Print RS.Fields(0).Value

        RS.MoveNext
       
    Loop

    RS.Close
    Set RS = Nothing
    CON.Close
    Set CON = Nothing

End Sub
Iin effect instead to Debug.Print RS.Fields("REG").Value=1 i have Debug.Print RS.Fields(0).Value="REG"
attached the .csv file and schema.ini
 

Attachments

  • TEST_ISTAT.zip
    85.7 KB · Views: 2
Last edited by a moderator:
Hello @luca90. The first line contains the field title because you specified in the connection string: Extended Properties=""text;HDR=no;""
The value HDR=no means "Header = No", that is, the first line of the file is considered data, not column headers. Therefore, RS.Fields(0).Value in the first iteration is exactly the text from the first line of the file (that is, the header).
If your CSV file contains headers in the first line (which is the default case), replace the connection string with:
Rich (BB code):
CON.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & thePath & ";" & _
                       "Extended Properties=""text;HDR=Yes;FMT=Delimited;"""
HDR=Yes - the first line contains headers and will not be included in the result. FMT=Delimited - CSV format (separators such as commas, tabs, etc.). I hope I was able to explain it to you and thus helped you. Good luck.
 
Back
Top