Be the first user to complete this post
|
Add to List |
VBA-Excel: Read Excel WorkBook as DataBase using ODBC Source
You can read an Excel WorkBook as a complete DataBase, means an Excel file can act as Database. You can make range of Rows and Columns as the Tables of your Database which means Excel WorkBook.
This is one of the very crucial feature excel provides because imagine if you have an excel file contains very huge data, say more than 10000X500 cells filled with data and you have to fetch some data from the entire workbook based on some conditions. Yes, you can navigate the entire WorkSheet row wise or column wise but it will take significant amount of time to navigate through each cell. That’s why we have Database and Table concept right.
Read Excel WorkBook as DataBase follows the steps below
- Store data in your excel file and save it some location in your system, say “DB Data.xlsx”
- Make Excel File as ODBC Source using Microsoft Excel Driver (give the name as SumitODBC)
- Open a new excel file in which you will fetch the data from “DB Data.xlsx”
- Open the VB Editor
- Create a “ADODB.Connection” Object
- Open the connection and provide the DSN name as the same name provided in step 2.(In this caseit is “SumitODBC”)
- Write the query for fetching the data, provide the table name as the range from the “DB Data.xlsx”
- Execute the query and store the result in resultSet
- Read the data from the resultSet and display in the newly created excel and close the resultset.
- Create a button on newly created excel and assign the macro function to it.
- Store data in your excel file and save it some location in your system, say “DB Data.xlsx”
- Make Excel File as ODBC Source using Microsoft Excel Driver (give the name as SumitODBC)
- Open a new excel file in which you will fetch the data from “DB Data.xlsx” and Open the VB Editor
- Create an “ADODB.Connection” Object
Set Connection = CreateObject("ADODB.Connection")
- Open the connection and provide the DSN name as the same name provided in step 2.(In this caseit is “SumitODBC”)
Open "DSN=SumitODBC"
- Write the query for fetching the data; provide the table name as the range from the “DB Data.xlsx”
strQuery = "SELECT * FROM [Sheet1$A1:Z500] where Dept = 'IT'"
- Execute the query and store the result in resultSet
Set resultSet = Connection.Execute(strQuery)
- Read the data from the resultSet and display in the newly created excel and close the resultSet
Do While Not resultSet.EOF intRowCounter = intRowCounter + 1 mainWorkBook.Sheets("Sheet2").Range("B" & intRowCounter).Value = resultSet.Fields("Name").Value resultSet.movenext Loop resultSet.Close
Complete Code:
Sub ReadDB() Dim mainWorkBook As Workbook Dim intRowCounter Set mainWorkBook = ActiveWorkbook intRowCounter = 2 mainWorkBook.Sheets("Sheet2").Range("A2:Z100").Clear Set Connection = CreateObject("ADODB.Connection") Connection.Open "DSN=SumitODBC" strQuery = "SELECT * FROM [Sheet1$A1:Z500] where Dept = 'IT'" Set resultSet = Connection.Execute(strQuery) Do While Not resultSet.EOF mainWorkBook.Sheets("Sheet2").Range("A" & intRowCounter).Value = resultSet.Fields("Emp Id").Value mainWorkBook.Sheets("Sheet2").Range("B" & intRowCounter).Value = resultSet.Fields("Name").Value mainWorkBook.Sheets("Sheet2").Range("C" & intRowCounter).Value = resultSet.Fields("Age").Value mainWorkBook.Sheets("Sheet2").Range("D" & intRowCounter).Value = resultSet.Fields("Dept").Value intRowCounter = intRowCounter + 1 resultSet.movenext Loop resultSet.Close End Sub