|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Running MS SQL query from Excel VBAI 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 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 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 > > |
|||||||||||||||||||||||