Home All Groups Group Topic Archive Search About

Running MS SQL query from Excel VBA

Author
9 Jun 2006 3:01 AM
excel_hari
Hi,

I posted in microsoft.public.sqlserver yesterday, but it seems that
microsoft.public.sqlserver.programming would be more appropriate for my
issue.

(MS Sql 2005 and Office 2003)


I have a MS SQL query file which generates 8 outputs. I want to be able

to run this query from excel VBA and channel the 8 outputs to different

worksheets of a particular workbook. These are the things I want to
know:-


a) How to connect to the Database from Excel. Presently I click on
start--programs--SQL server management studio and then SQL server logon

window opens up in which it automatically uses my NT login to get
connected to the server.


b) How to excete the Query from within VBA (any sample codes).
Basically I was thinking of creating a constant string having the
complete query in it and somehow excuting this string.


=cb) The outputs which come preently are based on SELECT statements in
query file. Presently am copying this data manually from results window

and pasting to relevant sheets in excel. How to go about automating
this ( would need to get a handle on each of the results window)


Please offer some guidelines on the same. (I use Excel VBA job but
havent interfaced with SQL server)


Regards,
HP
India

Author
9 Jun 2006 3:29 AM
navyzhu@gmail.com
Hi,
Generally, you need use ADO to connect database in VBA. But if your
business rule is not so complex, may be there have another way. Just
tried to access Excel menu by Data->Import Data (I am using Chinese
version Excel, so the menu name may be not so accurate), and following
wizard to get data from SQL Server. Good luck!

excel_h***@yahoo.com 写道:

Show quote
> Hi,
>
> I posted in microsoft.public.sqlserver yesterday, but it seems that
> microsoft.public.sqlserver.programming would be more appropriate for my
> issue.
>
> (MS Sql 2005 and Office 2003)
>
>
> I have a MS SQL query file which generates 8 outputs. I want to be able
>
> to run this query from excel VBA and channel the 8 outputs to different
>
> worksheets of a particular workbook. These are the things I want to
> know:-
>
>
> a) How to connect to the Database from Excel. Presently I click on
> start--programs--SQL server management studio and then SQL server logon
>
> window opens up in which it automatically uses my NT login to get
> connected to the server.
>
>
> b) How to excete the Query from within VBA (any sample codes).
> Basically I was thinking of creating a constant string having the
> complete query in it and somehow excuting this string.
>
>
> =cb) The outputs which come preently are based on SELECT statements in
> query file. Presently am copying this data manually from results window
>
> and pasting to relevant sheets in excel. How to go about automating
> this ( would need to get a handle on each of the results window)
>
>
> Please offer some guidelines on the same. (I use Excel VBA job but
> havent interfaced with SQL server)
>
>
> Regards,
> HP
> India
Author
9 Jun 2006 2:40 PM
AverageUser
Try this link to Microsoft knowledge base:

http://support.microsoft.com/?scid=kb;en-us;316934&spid=2512&sid=global

It shows code for ADO connection in Excel. Hope it helps.




Show quote
"navy***@gmail.com" wrote:

> Hi,
> Generally, you need use ADO to connect database in VBA. But if your
> business rule is not so complex, may be there have another way. Just
> tried to access Excel menu by Data->Import Data (I am using Chinese
> version Excel, so the menu name may be not so accurate), and following
> wizard to get data from SQL Server. Good luck!
>
> excel_h***@yahoo.com 写道:
>
> > Hi,
> >
> > I posted in microsoft.public.sqlserver yesterday, but it seems that
> > microsoft.public.sqlserver.programming would be more appropriate for my
> > issue.
> >
> > (MS Sql 2005 and Office 2003)
> >
> >
> > I have a MS SQL query file which generates 8 outputs. I want to be able
> >
> > to run this query from excel VBA and channel the 8 outputs to different
> >
> > worksheets of a particular workbook. These are the things I want to
> > know:-
> >
> >
> > a) How to connect to the Database from Excel. Presently I click on
> > start--programs--SQL server management studio and then SQL server logon
> >
> > window opens up in which it automatically uses my NT login to get
> > connected to the server.
> >
> >
> > b) How to excete the Query from within VBA (any sample codes).
> > Basically I was thinking of creating a constant string having the
> > complete query in it and somehow excuting this string.
> >
> >
> > =cb) The outputs which come preently are based on SELECT statements in
> > query file. Presently am copying this data manually from results window
> >
> > and pasting to relevant sheets in excel. How to go about automating
> > this ( would need to get a handle on each of the results window)
> >
> >
> > Please offer some guidelines on the same. (I use Excel VBA job but
> > havent interfaced with SQL server)
> >
> >
> > Regards,
> > HP
> > India
>
>

AddThis Social Bookmark Button