PDA

View Full Version : How to call a Stored Procedure in Word



QueenofChaos
04-15-2009, 06:36 AM
Hi,
I have a Store Procedure in SQL2005 Express and I'd like to know, how to call it in a Word macro. I'm a bit of Newbie with the connection string and all that stuff, if someone can explain me a bit, it would be nice.
Thanks

stanl
04-17-2009, 02:04 AM
Normally, I would just say set up an ADODB.Connection and Execute()... but could you be more specific about what the stored proc does? Stan

QueenofChaos
04-17-2009, 02:25 AM
Hi,
Thanks for your answer but I solved my problem, here is the solution for anyone who needs help
Function CheckID(ByRef oldPath As String, ByRef newPath As String) As Integer
Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim Rs1 As ADODB.Recordset
Dim Connect As String
Dim Result As Integer
Dim Exist As Integer

Let Connect = "driver={sql server};server=servername;Database=DBName;UID=User;PWD=Pass;"
'Etablit la connection
Set Conn1 = New ADODB.Connection
Conn1.ConnectionString = Connect
Conn1.Open
'Ouvre le recordset
Set Cmd1 = New ADODB.Command
Cmd1.ActiveConnection = Conn1
Cmd1.CommandText = "YourStoredProcedureName"
Cmd1.CommandType = adCmdStoredProc
Cmd1.Parameters.Refresh
Cmd1.Parameters(1).Value = newPath
Cmd1.Parameters(2).Value = oldPath
Set Rs1 = Cmd1.Execute()
While Not Rs1.EOF
Result = Rs1.Fields(0).Value
Exist = Rs1.Fields(1).Value
Rs1.MoveNext
Wend
Rs1.Close
Conn1.Close
Set Rs1 = Nothing
Set Conn1 = Nothing
End Function