VBA in EXCEL - Connect To SQL Server
VBA in EXCEL - Connect To SQL Server
Home
Library
Learn
Samples
Downloads
Support
Community
Sign in
Forums
Microsoft Developer Network >Forums Home > Microsoft ISV Community Center Forums > Visual Basic for Applications (VBA) > VBA in EXCEL - Connect to SQL Server
My Forum Links
Ask a question
Roda Chan
30 Points
Sign In To Forums
Forums Home
Browse Forums Users
Frequently asked questions - Forums help
Quote
All Replies
Roda Chan
30 Points
Thursday, April 13, 2006 4:42 AM
Ah.... I found an answer. I need to add the reference of DAO object library.
social.msdn.microsoft.com/Forums/en-US/isvvba/thread/f599f280-b786-43f4-9357-77f19a107c3f/
1/8
28/05/13
connstring =
"ODBC;UID=userid;PWD=password;DATABASE=db;DSN=datasource;dbDriverNoPrompt"
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("B1"),
Sql:=sqlstring)
.Refresh
End With
It always displays the ODBC Data Sources dialog box even though I have passed all the
information in the connstring. Can anyone help please??
Reply
Quote
4,945 Points
Tuesday, April 25, 2006 3:13 PM
social.msdn.microsoft.com/Forums/en-US/isvvba/thread/f599f280-b786-43f4-9357-77f19a107c3f/
2/8
28/05/13
3/8
28/05/13
Reply
Quote
4,945 Points
Tuesday, May 09, 2006 3:42 PM
Due to an overlap in communication, I have more information on this topic that I might as
well post in case it helps someone.
Open Excel.
2.
3.
4.
In the editor window, type the following information, substituting the proper names for the
server and the tables you want in between the "Sub xxxx" and "End Sub" tags:
4/8
28/05/13
"ODBC;DSN=pubs;UID=;PWD=;Database=pubs"
' Now implement the connection, run the query, and add
' the results to the spreadsheet starting at row A1
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("A1"), Sql:=s
qlstring)
.Refresh
End With
Save and close the macro, and run it from the same menu you accessed in step 2.
NOTE
There are probably at least ten programmatic ways to do this exact same thing and all of them are
correct! If you're really interested in programming using VBA, you'll find several good books here at
InformIT and also on Safari.
Keeping The Database Connection Live
We now move away from the "Transfer In and Out" category to the "Linked Data" category of methods.
I'll describe the two main methods to link data, both of which link Excel data into a SQL Server query.
There are ways to use Excel to manage data directly in SQL Server, but they involve a bit more
programming, and I've found them to be a bit clumsy especially for daily use. Excel simply doesn't
handle locks and connectivity issues as well as you need for large-scale production solutions.
You could also code a solution that accesses data stored in an Excel spreadsheet and update a SQL
Server table as a result, but this is really event-driven and not a linked solution. (Again, there's a great
deal of programming help in that vein here on InformIT.)
So returning to the methods I've found easy to implement, there is a two-step process you can use to
query data in an Excel spreadsheet.
First, you'll need to create a linked server. Here's how to do that:
1.
2.
5/8
28/05/13
GO
By doing so, you create a linked server, and give that connection a name. Notice also the name of the
spreadsheet, which can also be a share location. You only have to do this once for each spreadsheet; if
you're not going to access that spreadsheet again, it's a good idea to drop the linked server after you've
used it.
Now that we have a linked server, we can access the data. The process for this method is as follows:
1.
GO
In this section, we've used the OPENQUERY function, which passes the query on to the provider. You'll
find the query language is pretty limited at times, with Excel. If the queries aren't selective enough, set
up another worksheet in the workbook with the data you want, and query that one.
As you can see, you have several options open to integrate data between Microsoft Excel and SQL
Server. Keep the limitations in mind, and get that data out there!
Online Resources
These links are from Microsoft. There are several others out there but these deal with some of the issues
that I brought up in the article.
Here's an article that details the data type woes with DTS and Excel imports.
This article from Microsoft details the process of using DTS with Excel.
Microsoft has a larger discussion of ODBC to Excel datatype issues here.
This Microsoft article details programming with ADO against Excel files it shows you how to open one
as a database.
social.msdn.microsoft.com/Forums/en-US/isvvba/thread/f599f280-b786-43f4-9357-77f19a107c3f/
6/8
28/05/13
Quote
mayberce
0 Points
Friday, May 01, 2009 5:23 PM
Reply
Quote
mayberce
0 Points
Friday, May 01, 2009 5:24 PM
Reply
Quote
165 Points
Tuesday, January 18, 2011 7:23 PM
A little late to help this situation but for anyone else with a similar problem of having the ODBC
Data Sources dialog box open every time they execute code to return query data. I've found
this issue is due to the fact that the QueryTable "Save Password" has not been set to "True"
social.msdn.microsoft.com/Forums/en-US/isvvba/thread/f599f280-b786-43f4-9357-77f19a107c3f/
7/8
28/05/13
via code, or manually you can set this in the C onnection Properties dialog box.
As long as you are supplying the password with code and have the "Save Password" property
set to True (or checked in the dialog box) then the ODBC Data Sources dialog box should only
appear the first time you execute the code, and sometimes it doesn't display even on the first
time as long as you have the ODBC connection set-up on your machine.
Dan
Reply
Quote
TOOLS
PLATFORMS
SERVERS
DEVELOPER RESOURCES
Visual Studio
Visual Studio
Windows Server
MSDN Subscriptions
Expression
Windows
Exchange Server
MSDN Magazine
ASP.NET
Windows Phone
SQL Server
Silverlight
Windows Azure
Code Samples
School faculty
Office
Data
MSDN Forums
social.msdn.microsoft.com/Forums/en-US/isvvba/thread/f599f280-b786-43f4-9357-77f19a107c3f/
8/8