I am developing an Access Project front end with a SQL server database.
I have written a stored procedure that returns a record set and also a
value via an output parameter.
The procedure is as follows
CREATE PROCEDURE qslCheckShiftTimes
@Ward NVARCHAR(6),
@Shift NVARCHAR(10),
@Exists TINYINT OUTPUT
AS
SELECT *
FROM tblWardShift
WHERE
tblWardShift.WardCode=@Ward
AND tblWardShift.strShift = @Shift
SELECT @Exists = @@ROWCOUNT
RETURN
GO
The test form has the following code to call the procedure
Private Sub Command0_Click()
Dim oConn As New ADODB.Connection
Dim oCmdTime As New ADODB.Command
Dim oRs As New ADODB.Recordset
Dim sConn As String
Dim iTime As Integer
sConn = "Provider='SQLOLEDB';Data Source='Aph-Ework-Dev';" & _
"Initial Catalog='NB2KunsecuredSQL';Integrated
Security='SSPI';"
oConn.Open sConn
oCmdTime.CommandText = "qslCheckShiftTimes"
oCmdTime.CommandType = adCmdStoredProc
Set oCmdTime.ActiveConnection = oConn
oCmdTime.Parameters("@Ward") = "AW43"
oCmdTime.Parameters("@Shift") = "Early"
oCmdTime.Parameters("@Exists") = iTime
oRs.Open oCmdTime, , adOpenForwardOnly, adLockReadOnly
MsgBox ("Result = " & Str(iTime))
MsgBox (oRs.Fields(1))
oRs.Close
Set oRs = Nothing
oConn.Close
Set oConn = Nothing
End Sub
The procedure call returns one row, but the @Exists parameter returns 0
when it should return 1. When I debug the procedure in Query Analyser
on the server, the output parameter is correctly set to 1. I can also
run the procedure from the frontend (but can't work out how to see what
parameter value is returned).
I know that I could work around this problem without using an output
parameter, but am just learning SQL server and need to know what I am
doing wrong. I wondered if the output parameter needs to initially be
set to null somehow.
Thanks in advance.