To the best of my knowledge DAO is better suited for Access databases . I'd use ADO as the better library for SQL Server connections these days.
You have more than one option.
1)
You can use Data | New Database Query... as a tool right in Excel. This will use a Data Source Name that you can save and re-use. See Zapawa's book
Excel Advanced Report Development for the excellent resources on this, or keep posting your questions here.
2)
For ADO, there's a lot to learn at first although the basics are extremely simple. The first thing you'll need is a connection string and the proper authentications. Try this
connection string trick and see if it works for you (otherwise, you can google connection strings and you'll find more info):
There's a lot of good info on ADO at
W3 Schools. ADO has some three main objects (the Recordset object, the Connection object, and the Command object) and they can all be used to accomplish your task. The simplest is to use the Recordset object, but the Command object is capable of updates, deletes, and so on.
I also found Kimmel et al.
Excel 2003 VBA to be very helpful with their chapter on ADO (specifically deals with SQL server as well - also where I found the trick posted in the link above).
Here's an example of an ADO routine. The only difference for you would be the connection string and the the SQL statement:
Code:
Sub GetDataFromAccessMDB()
'---------------------------------
'USER: Please make sure you set references to the ADO object Library
' 1. In the Visual Basic window select Tools on the main menu
' 2. Then from the Tools menu select References...
' 3. Then click the box for Microsoft ActiveX Data Objects
' 4. If there is more than one choice, choose the highest version
'---------------------------------
Const MY_DATABASE_PATH As String = _
"\\Emc_nas\Users\David.Pecot\Ultra_Inventory 2007.mdb"
Const strSQL = _
"Select Top 5 Inventory.Year from Inventory;"
'Variables
Dim myRecordset As ADODB.Recordset
Dim strConnection_String As String
'Connection String
strConnection_String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & MY_DATABASE_PATH & ";" & _
"Persist Security Info=False"
'Object References
Dim ws As Worksheet
Set ws = Workbooks.Add.Sheets(1)
Set myRecordset = New ADODB.Recordset
'Initialize recordset and run the query
Call myRecordset.Open(strSQL, strConnection_String, CursorTypeEnum.adOpenForwardOnly, _
LockTypeEnum.adLockReadOnly, CommandTypeEnum.adCmdText)
'Copy to worksheet
If Not myRecordset.EOF Then
Call ws.Cells(1, 1).CopyFromRecordset(myRecordset)
End If
'Close recordset object and release memory
If (myRecordset.State And ObjectStateEnum.adStateOpen) Then myRecordset.Close
Set myRecordset = Nothing
End Sub
HTH