Ads 468x60px

Thursday, July 12, 2012

Using Excel As Your Database

Using Excel As Your Database:
This is a guest post by Vijay, our in-house VBA Expert.
Often I have thought, if I could have write “Select EmployeeName From Sheet Where EmployeeID=123″ and use this on my excel sheet, my life would be simpler. So today we will learn how to do this.
People spend a lot of time thinking whether to use Excel as their database or not. Eventually they start using Access or SQL Server etc.
Today we will learn how to use Excel as a Database and how to use SQL statements to get what we want.

Excel as a Database – Demo

We will learn how to build this:
Using Excel as your Database - demo

Before we begin:

  1. The entire sheet (where the raw data has been stored) will be referred as one single database table by Excel. This is very important to understand.
  2. This has nothing related with the in-built Table (2007 and greater) / List (2003 and previous) feature of Excel.
  3. If you know SQL (Structured Query Language) your task becomes much easier.

Setting up Excel as Database

We need some raw data and we will utilize Customer Service Dashboard sample database here.
Let’s get started.
First we will design the structure of what all option we want to present for filtering the data, which you can see in the interface below.
Once the user clicks on Show Data we will use a SQL statement to filter-out the data as per the drop down options selected by the user and the put them in the table below.
We will also use another SQL statement to populate the top right hand side table for calls data when all the 3 drop downs have some options selected.
Screen Design

Adding Active-x data objects reference

We need to add a reference to the Microsoft ActiveX Data Objects Library to be able to use the worksheet as a database table. You can do this from Visual Basic Editor > Tools.
VBA References
I usually select the most recent version, however if you are developing a product it will be best suited if you are familiar with the operating system and office version used by the end-user’s system and accordingly select the best version available.

Opening Excel as a Database

Once this is done we need to hit the road with some VBA code.


Public Sub OpenDB()

If cnn.State = adStateOpen Then cnn.Close

cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _

ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name

cnn.Open

End Sub


The above procedure is the heart of this post, this is where we define how to use the current Excel workbook as our database.
cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _

ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name

On this line, we define all the possible file extensions that we are allowed to create an Excel Workbook and then use as our database.

Let’s understand the code module

When you click on the Update Drop Downs button, the VBA code uses the “Data” worksheet as a table and then finds unique values for Products, Region and Customer Types and then populates them as ListItems for the ComboBox controls.
Example for Products drop down


strSQL = "Select Distinct [Product] From [data$] Order by [Product]"

closeRS

OpenDB

cmbProducts.Clear

rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic

If rs.RecordCount > 0 Then

Do While Not rs.EOF

cmbProducts.AddItem rs.Fields(0)

rs.MoveNext

Loop

Else

MsgBox "I was not able to find any unique Products.", vbCritical + vbOKOnly

Exit Sub

End If


What is important to notice here is how the Table and Fields have been identified using square brackets unlike traditional SQL where we just provide the name, also the table name has to be suffixed with a $ symbol at the end.
As I have suggested earlier, one entire sheet will be treated as one single table, so if you have multiple datasets that were currently organized within one sheet you may have to create multiple sheets to store that data to be able to use them as tables. This would also make maintenance of data easier.

Using Excel SQL to consolidate two sheets in to one

Many people ask, how to consolidate 2 or more sheets which have the similar data. Well I would have adopted this method and wrote a simple query as below.


SELECT ID, FirstName, MiddleName, LastName, Age, DOB From [Table1$]

UNION ALL

SELECT ID, FirstName, MiddleName, LastName, Age, DOB From [Table2$]

This would allow me to use both the sheets as one table and fetch all of my data into a new sheet.

Download Excel As Database Demo File

Click here to download the demo file & use it to understand this technique.

Do you use Excel as a database?

Do you also user Excel as your database? If yes please put in the comment below how do you use the same and what has been your experience. Leave a comment.

More on VBA & Macros

If you are new to VBA, Excel macros, go thru these links to learn more.

Join our VBA Classes

If you want to learn how to develop applications like these and more, please consider joining our VBA Classes. It is a step-by-step program designed to teach you all concepts of VBA so that you can automate & simplify your work.
Click here to learn more about VBA Classes & join us.

About Vijay

Vijay (many of you know him from VBA Classes), joined chandoo.org full-time this February. He will be writing more often on using VBA, data analysis on our blog. Also, Vijay will be helping us with consulting & training programs. You can email Vijay at sharma.vijay1 @ gmail.com. If you like this post, say thanks to Vijay.

0 comments:

Post a Comment