PC Review


Reply
Thread Tools Rate Thread

Best way to call a SQL Server Stored Procedure from within Excel

 
 
SQLScott
Guest
Posts: n/a
 
      29th Oct 2008
I am totally new to Excel programming, so please be patient with me while I
try and explain what it is that I need.

I would like to call a SQL Server stored procedure from Excel and have the
results
placed in a specific range of cells, for example starting at D7 to K7 for
however many rows are returned.

So my question is this: What is the best way to call the SQL Server Stored
Procedure and how do i place the results in a specific range of cells?

Should I place a button on the page and call the proc on the click event?

Any advise is greatly appreciated.
--
Thanks,

Scott
 
Reply With Quote
 
 
 
 
dmoney
Guest
Posts: n/a
 
      29th Oct 2008
I have always used the sql statement that the stored procedure uses which
made it considerably easier to get only the data i wanted to put in excel.
here is an example:


Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim dbase As String
Dim projec As String
over:
projec = TextBox1.Text
dbase = Range("C1").Value



cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;" _
& "Initial Catalog=xxxx;Data Source=xxxxx"
cn.Open
cn.CursorLocation = adUseClient

strsql = "SELECT
PrgMgr1,Analyst,PrgGroup,ContAdmin,ProjectDesc,Customer,ContractType,DateStart,DateClose,ContractNum FROM vblProject " _
& "WHERE HistoryName = '" & Range("G17") & "' AND Project =
'" & rngProj & "'"

If rs.State = adStateOpen Then rs.Close
rs.Open strsql, cn
If rs.RecordCount = 0 Then GoTo Title
rs.MoveFirst
Range("c10") = rs!ContAdmin
Range("C9") = rs!PrgMgr1
Range("C11") = rs!Analyst

rs.Close



"SQLScott" wrote:

> I am totally new to Excel programming, so please be patient with me while I
> try and explain what it is that I need.
>
> I would like to call a SQL Server stored procedure from Excel and have the
> results
> placed in a specific range of cells, for example starting at D7 to K7 for
> however many rows are returned.
>
> So my question is this: What is the best way to call the SQL Server Stored
> Procedure and how do i place the results in a specific range of cells?
>
> Should I place a button on the page and call the proc on the click event?
>
> Any advise is greatly appreciated.
> --
> Thanks,
>
> Scott

 
Reply With Quote
 
SQLScott
Guest
Posts: n/a
 
      29th Oct 2008
Thanks dmoney, this is GREAT. I really appreciate it.
--
Thanks,

Scott


"dmoney" wrote:

> I have always used the sql statement that the stored procedure uses which
> made it considerably easier to get only the data i wanted to put in excel.
> here is an example:
>
>
> Dim cn As New ADODB.Connection
> Dim rs As New ADODB.Recordset
> Dim strsql As String
> Dim dbase As String
> Dim projec As String
> over:
> projec = TextBox1.Text
> dbase = Range("C1").Value
>
>
>
> cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated
> Security=SSPI;Persist Security Info=False;" _
> & "Initial Catalog=xxxx;Data Source=xxxxx"
> cn.Open
> cn.CursorLocation = adUseClient
>
> strsql = "SELECT
> PrgMgr1,Analyst,PrgGroup,ContAdmin,ProjectDesc,Customer,ContractType,DateStart,DateClose,ContractNum FROM vblProject " _
> & "WHERE HistoryName = '" & Range("G17") & "' AND Project =
> '" & rngProj & "'"
>
> If rs.State = adStateOpen Then rs.Close
> rs.Open strsql, cn
> If rs.RecordCount = 0 Then GoTo Title
> rs.MoveFirst
> Range("c10") = rs!ContAdmin
> Range("C9") = rs!PrgMgr1
> Range("C11") = rs!Analyst
>
> rs.Close
>
>
>
> "SQLScott" wrote:
>
> > I am totally new to Excel programming, so please be patient with me while I
> > try and explain what it is that I need.
> >
> > I would like to call a SQL Server stored procedure from Excel and have the
> > results
> > placed in a specific range of cells, for example starting at D7 to K7 for
> > however many rows are returned.
> >
> > So my question is this: What is the best way to call the SQL Server Stored
> > Procedure and how do i place the results in a specific range of cells?
> >
> > Should I place a button on the page and call the proc on the click event?
> >
> > Any advise is greatly appreciated.
> > --
> > Thanks,
> >
> > Scott

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call sql server stored procedure with parameters from Acces =?Utf-8?B?SmFjaw==?= Microsoft Access VBA Modules 2 28th Jun 2007 04:33 PM
how do I call a SQL Server stored procedure? cj Microsoft VB .NET 11 14th Mar 2006 12:11 AM
how to call a stored procedure from ms access to sql server 2000 =?Utf-8?B?bWF4enNpbQ==?= Microsoft Access VBA Modules 0 22nd Apr 2005 02:36 AM
How do I call a SQL Server Stored Procedure from an Access Report. =?Utf-8?B?REpDb3ZhbGw=?= Microsoft Access Reports 2 21st Jan 2005 03:50 PM
How do you call an SQL Server Stored Procedure from Access? Marco Napoli Microsoft Access ADP SQL Server 5 17th Feb 2004 04:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:25 AM.