BEST PRACTICES -> SQL with DAO

Irish_Griffin

Board Regular
Joined
Jan 16, 2009
Messages
138
Hi guys,
I've discovered excel can do SQL !!! It feels like I'm the primitive caveman discovering fire :LOL:

Well lets not talk about how much time I've wasted writing code that would have been soooooooo much easier in SQL :( .... Instead, lets talk about BEST PRACTICES using SQL in EXCEL :cool:

I would like to make this thread a good reference for others ( not much online surprisingly ). I'll make this a sweet thread if I can get the guru's help.

So first and foremost:
#1 - Is DAO the right choice ?

Remember to take learning one bite at a time:
QrUHZ.jpg
 
Basic ADO shell for you, this is the only way I use SQL from VBA.

Code:
Sub Main()
Dim Conn As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim Cmd As New ADODB.Command
Dim sqlText As String
Dim Row As Long
Dim Findex As Long
Dim Data As Worksheet
Dim X As Long
Dim UID As String
Dim PWD As String
Dim Server As String
    Application.Calculation = xlCalculationManual
    UID = "" 'Enter the User ID
    PWD = "" 'Enter the password
    Server = "" 'Enter the name of your ODBC connection string
    Set Data = Sheets("Main") 'Change this to the name of the sheet you want to return data to
    Data.Select
    Range("A:F").ClearContents 'Change A:F to the range you expect data to return to
    Conn.Open "PROVIDER=MSDAORA.Oracle;DATA SOURCE=" & Server & ";" & "USER ID=" & UID & ";PASSWORD=" & PWD 'Note, I am using MSDAORA as I use an ORACLE DB, you will need to change it for what DB you are using
    Cmd.ActiveConnection = Conn
    Cmd.CommandType = adCmdText
    sqlText = "select Something from somewhere where something = something" 'Put your SQL Statement in here
    Cmd.CommandText = sqlText
    Set RS = Cmd.Execute
    For X = 0 To 4 'Change the 4 to the number of columns you are selecting MINUS 1
        Data.Cells(1, X + 1) = RS.Fields(X).Name
    Next
    Do While Not RS.EOF
       Row = Row + 1
       For Findex = 0 To RS.Fields.Count - 1
         Data.Cells(Row + 1, Findex + 1) = RS.Fields(Findex).Value
       Next Findex
       RS.MoveNext
    Loop
    Application.Calculation = xlCalculationAutomatic
    Application.Calculate
End Sub
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Links and examples is exactly what I'm looking for!! :biggrin:

It's hard as a noob to make this step because there is soooooo much bad code online.....

I get it to work after hours and hours only to find out that the code is not efficient. Not to mention, an experienced coder thinks the author was smoking crack at the time.... :p

Give me links to the truely best practices of SQL in excel...
.... and I will give you guys a great tutorial to point stupid noobs to ;)

Thanks,
Griff
 
Upvote 0
Links and examples is exactly what I'm looking for!! :biggrin:

It's hard as a noob to make this step because there is soooooo much bad code online.....

I get it to work after hours and hours only to find out that the code is not efficient. Not to mention, an experienced coder thinks the author was smoking crack at the time.... :p

Give me links to the truely best practices of SQL in excel...
.... and I will give you guys a great tutorial to point stupid noobs to ;)

Thanks,
Griff

The instructions are in the code as comments.
 
Upvote 0
The instructions are in the code as comments.


No doubt, and that is PERFECT. Please include notes as much as possible.

But us noobs need in really broken down into small bites. My professional job is to write technical instructions for others to learn from. Pretty good if I say so myself ;)

I thought it would be cool to make a nice tutorial document for your community in exchange for getting taught from the community :biggrin:

OK, it's time to clock out of work. I'll catch you guys tomorrow. :p

-Griff
 
Upvote 0
Griff

I can't think of the top of my head of any particular sites to recommend.

I actually use Microsoft quite regularly for reference for this sort of thing.

Here's one link I sometimes visit.

It's not SQL specifit, but then again I sometimes use this link.
 
Upvote 0
I also refer to the link that Norie provided.
To add to it (and include it again) to some further online ADO resources (it's been a while - I'm not even sure what's in all of these any more. I know erlandsen has a quite a lot on ADO):

http://msdn2.microsoft.com/en-us/library/ms678086(VS.85).aspx
http://www.erlandsendata.no/english/
http://www.beyondtechnology.com/geeks023.shtml
http://support.microsoft.com/kb/257819
http://www.rondebruin.nl/ado.htm
http://support.microsoft.com/default.aspx?scid=kb;en-us;257819
http://www.mrexcel.com/forum/showthread.php?p=1207314
http://www.datawright.com.au/index.htm

Of greatest value to me (I think) has been:
1) Wrox's Excel VBA Programmer's Reference (I've used 2003 edition by Kimmel at al. but 2007 is now out by Green et al)
2) w3schools is excellent on ADO objects and methods: http://www.w3schools.com/ado/default.asp

Just to note that DAO is fine - I'm not as used to implementing it from Excel, but I think it will be supported for years yet. It works very well with Access, in fact.
 
Last edited:
Upvote 0
Links and examples is exactly what I'm looking for!! :biggrin:

It's hard as a noob to make this step because there is soooooo much bad code online.....

I get it to work after hours and hours only to find out that the code is not efficient. Not to mention, an experienced coder thinks the author was smoking crack at the time.... :p

So one has graduated from noob to Guru in 4 posts!

If the code achieves the objective set by the op does it really have to be ultra efficient?

Also if there are glaring deficiencies in the code the Gurus generally add a note to point the responders in the right direction.

Mike
 
Upvote 0
So one has graduated from noob to Guru in 4 posts!

If the code achieves the objective set by the op does it really have to be ultra efficient?

Also if there are glaring deficiencies in the code the Gurus generally add a note to point the responders in the right direction.

Mike


Hmmmmm, well ultra efficient is not my goal. To better define it:

Goal is to create:
- Standard template where the user needs to have SQL capabilities in excel using ADO
- Extensive detailed notes so that the code is easy to learn from
- Module for many common functions
- ROBUST : flexible template that meets most project needs and does not break !!


Well gents, haven't forgotten about my task. Finally got some time at work to continue. Go grabbin a cup of Joe and placing my thinkin cap on.

Time to get my ADO learn on!! ;)
 
Upvote 0
Happy to help, post back if you want any other working examples, I wrote one a while back for uploading rows of data to a database then reading back and verifying the data. There are other ones I have done which make calling Stored Procs a breeze and simply report back true or false from the function to say if it ran or not.
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,396
Members
449,446
Latest member
CodeCybear

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top