ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Connection String to SQL to run Stored Procedure (https://www.excelbanter.com/excel-programming/345257-connection-string-sql-run-stored-procedure.html)

Carl[_8_]

Connection String to SQL to run Stored Procedure
 
Does anyone have samle code to connect and run a stored Proc on SQL Server?
I have the Server name. It is added to my ODBC and connection is good, but
to simply connect and run a sp from Excel is driving me crazy.
Thanks



Nigel

Connection String to SQL to run Stored Procedure
 
This can drive you mad!!

Try recording a macro while using the menu options Data-Import External
Data and using the appropriate connections etc. It should give you a
starter template if not the whole answer.

--
Cheers
Nigel



"Carl" wrote in message
news:t7Hcf.65$7B.48@fed1read02...
Does anyone have samle code to connect and run a stored Proc on SQL

Server?
I have the Server name. It is added to my ODBC and connection is good, but
to simply connect and run a sp from Excel is driving me crazy.
Thanks





[email protected]

Connection String to SQL to run Stored Procedure
 
Hi
This from code samples with "Excel 2002 VBA" by Stephen Bullen et al

Public Sub SavedQuery()

Dim objField As ADODB.Field
Dim rsData As ADODB.Recordset
Dim lOffset As Long
Dim szConnect As String

' Create the connection string.
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Files\Northwind.mdb;"

' Create the Recorset object and run the query.
Set rsData = New ADODB.Recordset
rsData.Open "[Sales By Category]", szConnect, adOpenForwardOnly, _
adLockReadOnly, adCmdTable

' Make sure we got records back
If Not rsData.EOF Then
' Add headers to the worksheet.
With Sheet1.Range("A1")
For Each objField In rsData.Fields
.Offset(0, lOffset).Value = objField.Name
lOffset = lOffset + 1
Next objField
.Resize(1, rsData.Fields.Count).Font.Bold = True
End With
' Dump the contents of the recordset onto the worksheet.
Sheet1.Range("A2").CopyFromRecordset rsData
' Close the recordset
rsData.Close
' Fit the column widths to the data.
Sheet1.UsedRange.EntireColumn.AutoFit
Else
MsgBox "Error: No records returned.", vbCritical
End If

Set rsData = Nothing

End Sub

regards
Paul



All times are GMT +1. The time now is 05:07 PM.

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