Call a stored procedure from VBA - sql-server

I'm pretty new to SQL and VBA and I've written the following query in SQL Server:
ALTER PROCEDURE SP_insert_search_archive
#IP_Address VARCHAR(20),
#DT VARCHAR(30),
#search_word VARCHAR(50),
#search_time TIME
AS
INSERT INTO Search_Archive
values (#IP_Address, #DT, #search_word, #search_time);
Now I would like to execute this stored procedure in VBA (Excel):
Dim StartTime As Double
Dim SecondsElapsed As Double
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim par As String
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset
StartTime = Timer
Application.DisplayStatusBar = True
Application.StatusBar = "Contacting SQL Server..."
con.Open "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=master;Integrated Security=SSPI;Trusted_Connection=Yes;"
cmd.ActiveConnection = con
SecondsElapsed = Round(Timer - StartTime, 4)
Dim search_word As String
search_word = Range("D2").Text
Dim IP As String
IP = GetMyPublicIP
cmd.Parameters.Append cmd.CreateParameter("IP", adVarChar, adParamInput, 20, IP)
cmd.Parameters.Append cmd.CreateParameter("DT", adVarChar, adParamInput, 30, Now)
cmd.Parameters.Append cmd.CreateParameter("Search Word", adVarChar, adParamInput, 50, search_word)
cmd.Parameters.Append cmd.CreateParameter("Search Time", adVarChar, adParamInput, 20, SecondsElapsed)
cmd.CommandText = "SP_insert_search_archive"
Set rs = cmd.Execute(, , adCmdStoredProc)
rs.Close
Set rs = Nothing
Set cmd = Nothing
MsgBox (DT)
con.Close
Set con = Nothing
When I'm activating the macro, I get this error:
Runtime error '-2147217900 (80040e14)':
Procedure or function SP_insert_search_archive has too many arguments specified

possible that the issue is with the name of the parameters, you left spaces in them instead of separating with "_" ("Search Word" instead of "search_word")

Related

Call SQL stored procedure from VBA - ADO - Named Parameters

Experimenting with VBA ADO with below code to call a stored procedure (with three parameters #p1, #p2, #p3) that writes data to an SQL table (with three columns p1,p2,p3).
Despite having NamedParameters set to true, the parameter names, though populated in the Parameter object, seemingly do not feed through to SQL, i.e. in the SQL table I get < p1,p2,p3> = <7,8,9> instead of <7,9,8>.
Sub UploadShareclassDatatoDB()
Dim Conn As ADODB.Connection
Dim ADODBCmd As ADODB.Command
Dim rs As ADODB.Recordset
NamedParameters = True
Dim i As Integer
Dim sConnect As String
sConnect = "Provider=SQLOLEDB.1;User ID=**;Password=**;Initial Catalog=**;Data Source=**;"
Set Conn = New ADODB.Connection
Conn.ConnectionString = sConnect
Conn.Open
Set ADODBCmd = New ADODB.Command
ADODBCmd.ActiveConnection = Conn
ADODBCmd.CommandText = "test"
ADODBCmd.CommandType = adCmdStoredProc
ADODBCmd.Parameters.Append ADODBCmd.CreateParameter("#p1", adInteger, adParamInput, , 7)
ADODBCmd.Parameters.Append ADODBCmd.CreateParameter("#p3", adInteger, adParamInput, , 8)
ADODBCmd.Parameters.Append ADODBCmd.CreateParameter("#p2", adInteger, adParamInput, , 9)
Set rs = ADODBCmd.Execute()
End Sub
What do I need to do so the procedure is invoked with parameters depending on parameter name rather than the order in which the parameters are constructed by the code?

SQL Server Stored Procedure doesnt appear to be called

The syntax of my code in MS Access appears to be correct.
I can run the stored procedure manually in SQL and get the correct result.
When I try to call the stored procedure from MS Access using ADO nothing appears to happen; any help would be greatly appreciated.
Private Sub Combo13_LostFocus()
Dim P1 As Integer
P1 = Forms![Frm_Ws10a]![Frm_WS10b]![Combo13].Column(0)
Dim rs As ADODB.Recordset
Dim CN As ADODB.Connection
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim adstring As Variant
Set CN = New ADODB.Connection
CN.ConnectionString = "Driver=SQL
Server;Server=LDXFBHD013492 \SQLEXPRESS;Database=xxxx;Trusted_Connection=YES;"
CN.Open
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = CN
.CommandText = "dbo.usp_update_consequenceweighting"
.CommandType = adCmdStoredProc
.Parameters.Refresh
Set prm = .CreateParameter("#ITID", adInteger, adParamInput)
prm.Value = P1
End With
Set rs = cmd.Execute
Me.Recalc
Me.Refresh
Thanks in advance
You are missing the .parameters.append in your code below
With cmd
.ActiveConnection = CN
.CommandText = "dbo.usp_update_consequenceweighting"
.CommandType = adCmdStoredProc
.Parameters.Refresh
Set prm = .CreateParameter("#ITID", adInteger, adParamInput)
Try adding the parameter.append instead of setting it
With cmd
.ActiveConnection = CN
.CommandText = "dbo.usp_update_consequenceweighting"
.CommandType = adCmdStoredProc
.parameter.append .createparameter("#ITID", adInteger, adParamInput, 50, [Value or variable here])
.execute 1
End With

SQL Server return table to MS Access via stored procedure

I'm trying to return a table back to MS Access that has 50+ columns and the rows can vary from 0 to 5000. For each case there can be multiple records and vehicle type.
I can execute the stored procedure and it works fine. I'm having trouble getting the data to return back to MS Access.
Stored procedure code:
ALTER PROCEDURE [dbo].[pJDB_Export]
(#dteFrom int,
#dteTo int,
#Veh nvarchar(80))
AS
BEGIN
SET NOCOUNT ON;
DECLARE #stWhere VARCHAR(200)
SELECT DISTINCT [Case]
INTO #tmp
FROM data
WHERE [VEHICLE TYPE] = #Veh
AND (CY BETWEEN #dteFrom AND #dteTo)
SELECT DISTINCT *
FROM dbo.vdata_Export_V3_3_2 v
INNER JOIN #tmp t ON v.[CASE] = t.[CASE]
MS Access code:
Function Exec_pJDB_export(sqlConn as string)
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim iFrom, iTo As Integer
Dim stv As String
iFrom = 1999
iTo = 2002
stv = "1 TO 2 TON TRUCKS (COMMERCIAL)"
Set conn = New ADODB.Connection
conn.Open "DSN=Cars"
conn.ConnectionString = sqlConn
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "pJDB_export"
cmd.Parameters.Append cmd.CreateParameter("#dteFrom", adInteger, adParamInput, , iFrom)
cmd.Parameters.Append cmd.CreateParameter("#dteTo", adInteger, adParamInput, , iTo)
cmd.Parameters.Append cmd.CreateParameter("#vehicle", adVarChar, adParamInput, 80, stv)
cmd.Execute
conn.Close
End Function
The execute method returns a recordset object. It is this object that contains your records. Here is example:
Function Exec_pJDB_export(sqlConn As String)
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset ' ADO recordset object, for accessing records.
Dim iFrom, iTo As Integer
Dim stv As String
iFrom = 1999
iTo = 2002
stv = "1 TO 2 TON TRUCKS (COMMERCIAL)"
Set conn = New ADODB.Connection
conn.Open "DSN=Cars"
conn.ConnectionString = sqlConn
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "pJDB_export"
cmd.Parameters.Append cmd.CreateParameter("#dteFrom", adInteger, adParamInput, , iFrom)
cmd.Parameters.Append cmd.CreateParameter("#dteTo", adInteger, adParamInput, , iTo)
cmd.Parameters.Append cmd.CreateParameter("#vehicle", adVarChar, adParamInput, 80, stv)
' This line has changed.
Set rs = cmd.Execute()
' Loops over the records.
Do Until rs.EOF
' Display the contents of column one to the user.
MsgBox rs.Fields(0).Value
rs.MoveNext
Loop
rs.Close
conn.Close
End Function
Quick overview:
EOF stands for End of File. It is true when you have viewed all records.
Don't forget to call MoveNext, or the do loop will continue forever!
The line rs.Fields(0).Value can be changed to rs.Fields("YourFieldName").Value, if you prefer. I find it easier to refer to fields by name, rather than position.

Excel VBA passing wrong number of parameters to ADO command object

I am trying to debug Excel VBA code that is calling a stored procedure resident on our SQL Server (SQL Server 2008 R2). With a bit of error trapping I can now see what the error is but I have no idea what is causing it.
Here is my VBA code:
Sub Add_Results_Of_ADO_Recordset()
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Dim stSQL As String
Const stADO As String = "Provider=SQLOLEDB.1;User ID =xxxxx;Password=xxxxx;" & _
"Persist Security Info=False;" & _
"Initial Catalog=MyDatabase;" & _
"Data Source=xxxxxxxxxxxx"
' initialise ADO
Set cnt = New ADODB.Connection
cnt.Open stADO
On Error GoTo Err_SaveProposal
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = cnt
.CommandText = "PolicyList"
.CommandType = adCmdStoredProc
.NamedParameters = True
.Parameters.Refresh
.Parameters.Append .CreateParameter("#Incept", adVarChar, adParamInput, 10, "2/1/2014")
.Parameters.Append .CreateParameter("#Expire", adVarChar, adParamInput, 10, "2/1/2014")
.Parameters.Append .CreateParameter("#PStatus", adVarChar, adParamInput, 20, "BOOK")
End With
' Debug code to ensure parameters are set correctly
For Each prm In cmd.Parameters
Debug.Print prm.Name & " : " & prm.Value
Next
cmd.Execute
If cnt.State = adStateOpen Then cnt.Close
Exit Sub
Err_SaveProposal:
Debug.Print Err.Number & ": " & Err.Description
If cnt.State = adStateOpen Then cnt.Close
End Sub
Here are the messages written to the immediate window:
#RETURN_VALUE :
#Incept :
#Expire :
#PStatus :
#Incept : 2/1/2014
#Expire : 2/1/2014
#PStatus : BOOK
-2147217900: Procedure or function PolicyList has too many arguments specified.
It looks like the three arguments I am trying to send to the stored procedure are being sent twice. Once as empty strings and one with the values I want to send. Any suggestions?

How to return values from a SQL Server Stored Procedure and Utilise them in Access VBA

I've set up a Stored Procedure in SQL Server that works fine. I can now call it from VBA, but want to return a value to know if there were any errors etc. The last parameter in my SP is set up as OUTPUT:
#DataSetID int = 0,
#Destination char(1)='-',
#errStatusOK bit OUTPUT
My VBA to call the SP is below, but it won't work now, after adding the new parameter and I'm not sure where I'm going wrong, I keep getting 3708 - Parameter object is improperly defined. Inconsistent or incomplete information was provided.:
Set cnn = New adodb.Connection
cnn.ConnectionString =
"DRIVER=SQL Server;SERVER=SERVER\SERVER;DATABASE=a_db;Trusted_Connection=Yes"
cnn.Open cnn.ConnectionString
Set cmd = New adodb.Command
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "stprMoveDataSet"
Set param = cmd.CreateParameter
("#DataSetID", adInteger, adParamInput, , stDataSet)
cmd.Parameters.Append param
Set param = cmd.CreateParameter
("#Destination", adChar, adParamInput, 1, stDestination)
cmd.Parameters.Append param
Set param = cmd.CreateParameter
("#errStatusOK", adBit, adParamReturnValue)
cmd.Parameters.Append param
rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Open cmd
How can I get the vba to work with the OUTPUT parameter and make the return value 'readable' by the vba.
EDIT - I've changed the question to be more specifically about returning values and not just about using OUTPUT Parameters.
Several ways are possible to get values back using VBA.
Recordset
Count of records affected (only for Insert/Update/Delete otherwise -1)
Output parameter
Return value
My code demonstrates all four. Here is a stored procedure that returns a value:
Create PROCEDURE CheckExpedite
#InputX varchar(10),
#InputY int,
#HasExpedite int out
AS
BEGIN
Select #HasExpedite = 9 from <Table>
where Column2 = #InputX and Column3 = #InputY
If #HasExpedite = 9
Return 2
Else
Return 3
End
Here is the sub I use in Excel VBA. You'll need reference to Microsoft ActiveX Data Objects 2.8 Library.
Sub CheckValue()
Dim InputX As String: InputX = "6000"
Dim InputY As Integer: InputY = 2014
'open connnection
Dim ACon As New Connection
'ACon.Open ("Provider=SQLOLEDB;Data Source=<SqlServer>;" & _
' "Initial Catalog=<Table>;Integrated Security=SSPI")
'set command
Dim ACmd As New Command
Set ACmd.ActiveConnection = ACon
ACmd.CommandText = "CheckExpedite"
ACmd.CommandType = adCmdStoredProc
'Return value must be first parameter else you'll get error from too many parameters
'Procedure or function "Name" has too many arguments specified.
ACmd.Parameters.Append ACmd.CreateParameter("ReturnValue", adInteger, adParamReturnValue)
ACmd.Parameters.Append ACmd.CreateParameter("InputX", adVarChar, adParamInput, 10, InputX)
ACmd.Parameters.Append ACmd.CreateParameter("InputY", adInteger, adParamInput, 6, InputY)
ACmd.Parameters.Append ACmd.CreateParameter("HasExpedite", adInteger, adParamOutput)
Dim RS As Recordset
Dim RecordsAffected As Long
'execute query that returns value
Call ACmd.Execute(RecordsAffected:=RecordsAffected, Options:=adExecuteNoRecords)
'execute query that returns recordset
'Set RS = ACmd.Execute(RecordsAffected:=RecordsAffected)
'get records affected, return value and output parameter
Debug.Print "Records affected: " & RecordsAffected
Debug.Print "Return value: " & ACmd.Parameters("ReturnValue")
Debug.Print "Output param: " & ACmd.Parameters("HasExpedite")
'use record set here
'...
'close
If Not RS Is Nothing Then RS.Close
ACon.Close
End Sub
Set cnn = New adodb.Connection
cnn.ConnectionString =
"DRIVER=SQL Server;SERVER=SERVER\SERVER;DATABASE=a_db;Trusted_Connection=Yes"
cnn.Open cnn.ConnectionString
Set cmd = New adodb.Command
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "stprMoveDataSet"
Set param1 = cmd.CreateParameter
("#DataSetID", adInteger, adParamInput, , stDataSet)
cmd.Parameters.Append param
Set param2 = cmd.CreateParameter
("#Destination", adChar, adParamInput, 1, stDestination)
cmd.Parameters.Append param
Set param3 = cmd.CreateParameter
("#errStatusOK", adBit, adParamOutput, , adParamReturnValue)
cmd.Parameters.Append param
rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Open cmd
I'd initially looked at OUTPUT Parameters, but could not find out how to get them back to Access (in VBA) to then provide feedback to the user. A colleague suggested using a SELECT in the Stored procedure and to use this.
STORED PROCEDURE:
Added the following at the end:
SELECT #errStatusOK as errStatusOK, #countCurrent as countCurrent, #countHistorical as countHistorical
VBA:
Dim cnn As ADODB.Connection
Dim cmd As New ADODB.Command, rs As New ADODB.Recordset, param As New ADODB.Parameter
Dim fld As ADODB.Field
Dim stMessage As String
Set cnn = New ADODB.Connection
cnn.ConnectionString = "DRIVER=SQL Server;SERVER=SERVER\SERVER;DATABASE=a_db;Trusted_Connection=Yes"
cnn.Open cnn.ConnectionString
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "stprMoveDataSet"
Set param = cmd.CreateParameter("#DataSetID", adInteger, adParamInput, , stDataSet)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("#Destination", adChar, adParamInput, 1, stDestination)
cmd.Parameters.Append param
rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
'rs.Open cmd
Set rs = cmd.Execute
If rs!errstatusok = True Then
stMessage = "Operation appears to have been successful, check the DataSets Listing..." & Chr(13) & "Also, the Server returned the following information: ["
Else
stMessage = "Operation appears to have failed, check the DataSets Listing..." & Chr(13) & "Also, the Server returned the following information: ["
End If
For Each fld In rs.Fields
stMessage = stMessage & "| " & fld.Name & " / " & fld.Value & " |"
Next fld
stMessage = stMessage & "]"
MsgBox stMessage
This returns the folliwing:
Operation appears to have failed, check the DataSets Listing...
Also, the Server returned the following information: [| errStatusOK / False || countCurrent / 0 || countHistorical / 10 |]
Among the other parameter enumerations from which "adParamInput" is taken, another is "adParamOutput", which is to indicate an out parameter from a stored procedure, and "adParamInputOutput" for a parameter which goes "both directions," as it were. In your case, I believe "adParamOutput" would be appropriate. I hope this is what you're looking for.

Resources