Automating Analysis of CSV Files with ADO SQL

How to automate the analysis of CSV files with ADO and SQL using VBA.

Automating Analysis of CSV Files with ADO SQL

What is a CSV file? CSV stands for comma separated values. Here is a picture of a CSV file.

CSV File Image
CSV File Image (cdsportfolio.csv)

CSV files can be used by applications like Notepad, Microsoft Excel and Google spreadsheets, for example. When opened with a spreadsheet application CSV files display only one worksheet. No formulas are saved in this format. They can be used to export large amounts of data to a database quickly. As the image shows CSV files are plain text files. They can be used on any computer and can be imported into a database quickly and easily. CSV files don’t have a special format like a Word document which helps to convert them into other formats easily.

What is ADO?

The full form of ADO is ActiveX Data Objects. ADO is Microsoft’s Client-Server technology to access the data between Client and Server. ADO takes help of OLE DB Provider to communicate with the data source. OLE Database (OLE DB) is a programming interface for data access from Microsoft. Applications can use OLE DB to access data sources directly or they can use OLE DB to call ODBC to access ODBC databases. ODBC or Open Data Base Connectivity is a general purpose provider. With help of this, ADO can access any Data source which can understand ODBC. ODBC permits maximum interoperability, which means a single application can access different Database Management Systems or DBMS.

What is SQL?
SQL Stands for Structured Query Language. ADO use SQL commands to communicate with the databases.
SELECT command is used to retrieve the data from a data source.

Databases are mostly stored in tables with rows functioning as records and field or columns functioning as headers. An Excel workbook can act as a data source. The worksheet becomes a table and the rows and the columns of the worksheet function as records and the fields of the table. The process of analyzing CSV files into Excel using ADO involves the following steps:

  1. First we open the connection to the Data Source
  2. Next we need to run the relevant SQL command
  3. We copy the resulting record set or rows into our worksheet
  4. Finally we close the record set and connection

Here’s the complete VBA code to automating analysis of CSV files with ADO and SQL:

Sub CSVDataAnalysisWithADO()

Dim sSQLString As String
Dim Conn As New ADODB.Connection
Dim myrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String

DBPath = “C:\zerodha\”

sconnect = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=” & DBPath & “;Extended Properties=’text;HDR=YES;FMT=Delimited’;”

Conn.Open sconnect

sSQLString = “SELECT * from [cdslportfolio.csv] where ([ISIN Name] like ‘H%’ or [ISIN Name] like ‘S%’) and [Value]>300000”

myrs.Open sSQLString, Conn

ActiveSheet.Range(“A2”).CopyFromRecordset myrs

myrs.Close
Conn.Close

End Sub

Further reading:

ADO in Excel

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.