ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   SQL Server Stored Procedure (https://www.excelbanter.com/excel-programming/425173-sql-server-stored-procedure.html)

JimP

SQL Server Stored Procedure
 
Is it possible to use a SQL Server stored procedure in an Excel query?

Where can I get documentation to get me started? I'm unfamiliar with Excel
programming, but pretty familiar with MS-Access, VBA and TSQL.



joel

SQL Server Stored Procedure
 
In excel you simply create an Access object and then use the same code as
Access except use the Access object. You also have to add the reference to
the Access library in excel from the VBA menu Tools - Reference - Microsoft
Access 11.0 object library.

You can also run queries that were dfefine in Access from excel. And
finally you can actual execute Access macro from excel like you requested.
let me know which method you prefer.

set obj = createobject("Access.Application")

or delcare the Access Object like in this code I wrote in January

Sub Submit()
'filename of database is with MakeDatabase macro

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

strDB = Folder & FName

If Dir(strDB) = "" Then
MsgBox ("Database Doesn't Exists, Create Database" & strDB)
MsgBox ("Exiting Macro")
Exit Sub
End If

ConnectStr = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Folder & FName & ";" & _
"Mode=Share Deny None;"

cn.Open (ConnectStr)
With rs
.Open Source:="Submissions", _
ActiveConnection:=cn, _
CursorType:=adOpenDynamic, _
LockType:=adLockOptimistic, _
Options:=adCmdTable

If .EOF < True Then
.MoveLast
End If
End With

With Sheets("Internal Project Plan")

ClientName = .Range("B4")
ImpMgr = .Range("B5")
LaunchDate = .Range("C4")

LastRow = .Range("K" & Rows.Count).End(xlUp).Row
For RowCount = 7 To LastRow

If UCase(.Range("K" & RowCount)) = "X" Then

DueDate = .Range("E" & RowCount)
ActualDate = .Range("F" & RowCount)
DateDif = .Range("M" & RowCount)
Accurate = .Range("L" & RowCount)
Task_ID = .Range("B" & RowCount)

With rs
.AddNew
!Task_ID = Task_ID
![Client Name] = ClientName
![Effective Date] = LaunchDate
![Imp Mgr] = ImpMgr
![Due Date] = DueDate
![Actual Date] = ActualDate
![Date Difference] = DateDif

.Update
End With
End If
Next RowCount

End With

Set appAccess = Nothing
End Sub



"JimP" wrote:

Is it possible to use a SQL Server stored procedure in an Excel query?

Where can I get documentation to get me started? I'm unfamiliar with Excel
programming, but pretty familiar with MS-Access, VBA and TSQL.




[email protected][_2_]

SQL Server Stored Procedure
 
Hi JimP,

How can you be unfamiliar with Excel programming but be pretty
familiar with VBA?

And how can you be pretty familiar with VBA if you don't know ADO?

And it occur to you to search the Web before asking your question?

All you need to do is create an ADODB connection, a command object,
and run execute.

What is there not to know?

On Mar 6, 12:16*pm, "JimP" wrote:
Is it possible to use a SQL Server stored procedure in an Excel query?

Where can I get documentation to get me started? I'm unfamiliar with Excel
programming, but pretty familiar with MS-Access, VBA and TSQL.



Tim Zych

SQL Server Stored Procedure
 
Here's one example which returns a SQL Server recordset to Excel.

Sub RecordsetToXL()

Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset

cn.Open "Driver=SQL
Server;Server=<Your_Server;Database=<Your_Databas e"

If cn.State = adStateOpen Then

With cmd

' set the command object properties
.ActiveConnection = cn
.CommandText = "<Your_Stored_Proc_Name"
.CommandType = adCmdStoredProc
' = "ParamValue"

Set rs = .Execute

' Stick in a sheet
Range("A1").CopyFromRecordset rs

End With

End If

Set cmd = Nothing
If rs.State = adStateOpen Then
rs.Close
End If
If cn.State = adStateOpen Then
cn.Close
End If
Set cn = Nothing
Set rs = Nothing

End Sub

This connection string above uses the "bare minimum" for Windows
authentication.

See www.connectionstrings.com for many more connection string examples.


--
Tim Zych
www.higherdata.com


"JimP" wrote in message
...
Is it possible to use a SQL Server stored procedure in an Excel query?

Where can I get documentation to get me started? I'm unfamiliar with Excel
programming, but pretty familiar with MS-Access, VBA and TSQL.




JimP

SQL Server Stored Procedure
 
I use DAO objects, not ADO. I also tried MSDN and didn't get very far.

wrote in message
...
Hi JimP,

How can you be unfamiliar with Excel programming but be pretty
familiar with VBA?

And how can you be pretty familiar with VBA if you don't know ADO?

And it occur to you to search the Web before asking your question?

All you need to do is create an ADODB connection, a command object,
and run execute.

What is there not to know?

On Mar 6, 12:16 pm, "JimP" wrote:
Is it possible to use a SQL Server stored procedure in an Excel query?

Where can I get documentation to get me started? I'm unfamiliar with Excel
programming, but pretty familiar with MS-Access, VBA and TSQL.




JimP

SQL Server Stored Procedure
 
Thanks to all - will check this out.

"Tim Zych" <tzych@nospam at earthlink dot net wrote in message
...
Here's one example which returns a SQL Server recordset to Excel.

Sub RecordsetToXL()

Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset

cn.Open "Driver=SQL
Server;Server=<Your_Server;Database=<Your_Databas e"

If cn.State = adStateOpen Then

With cmd

' set the command object properties
.ActiveConnection = cn
.CommandText = "<Your_Stored_Proc_Name"
.CommandType = adCmdStoredProc
' = "ParamValue"

Set rs = .Execute

' Stick in a sheet
Range("A1").CopyFromRecordset rs

End With

End If

Set cmd = Nothing
If rs.State = adStateOpen Then
rs.Close
End If
If cn.State = adStateOpen Then
cn.Close
End If
Set cn = Nothing
Set rs = Nothing

End Sub

This connection string above uses the "bare minimum" for Windows
authentication.

See www.connectionstrings.com for many more connection string examples.


--
Tim Zych
www.higherdata.com


"JimP" wrote in message
...
Is it possible to use a SQL Server stored procedure in an Excel query?

Where can I get documentation to get me started? I'm unfamiliar with
Excel programming, but pretty familiar with MS-Access, VBA and TSQL.







All times are GMT +1. The time now is 10:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com