Hi all,
I use the following function to execute a stored procedure which inserts records in to my sql database. I am trying to return the primary key value so that I can display it on my page however I keep getting the error that the "item is not found in the current collection".
My function is:
Sorry that the code is a bit rough but I have been testing it over and over.
The stored procedure is as follows and this runs fine:
The execution line for the function is:
Overall the pass through query being sent is:
The error is on the line:
In my function.. I have also tried adding .value on to the end of the .parameters with no luck.
Thank you for any help.
Chris
I use the following function to execute a stored procedure which inserts records in to my sql database. I am trying to return the primary key value so that I can display it on my page however I keep getting the error that the "item is not found in the current collection".
My function is:
Code:
Function Execute_Stored_Procedure(Proc_Name As String, Proc_Values As String, Optional ByVal Output_Return As Boolean) On Error GoTo Execute_Stored_Procedure_Error Dim MyDb As DAO.Database, MyQ As QueryDef Dim sql_send As String Set MyDb = CurrentDb() Set MyQ = MyDb.QueryDefs("qTemp_Stored_Proc") Dim stServer, stDatabase, stUsername, stPassword As String Dim stConnect As String stServer = "xxx.xxx.xxxx.net" stDatabase = "xxx" stUsername = "xxx" stPassword = "4B:mV6hpz_\2q=</Y%?b" stConnect = "ODBC;DRIVER={SQL Server};SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword sql_send = "exec " & Proc_Name & " " & Proc_Values ' Set the SQL property and concatenate the variables. MyQ.Connect = stConnect If Output_Return = False Then MsgBox "no supply" Else MsgBox "supply" sql_send = "declare @NCC_OUTPUT int " & sql_send End If Debug.Print sql_send MyQ.SQL = sql_send MyQ.ReturnsRecords = False MyQ.Execute Dim X As Integer X = MyQ.Parameters("@NCC_OUTPUT") ' Debug.Print MyQ.SQL Set MyQ = Nothing Done: Exit Function
The stored procedure is as follows and this runs fine:
Code:
ALTER PROCEDURE [dbo].[sp_Submit_NCC] -- Add the parameters for the stored procedure here @DteOccur datetime, @Pdetected nvarchar(50), @DeptRaisedBy int, @DeptResp int, @NCDescrip nvarchar(255), @NCCause nvarchar(255), @NCImmediateAct nvarchar(255), @NCLocation nvarchar(100), @PNumOrRef nvarchar(30), @EventCat int, @ReportedEmailAddy nvarchar(100), @NCC_Output_ID INT OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @NCC_ID int -- Insert statements for procedure here INSERT INTO tbllog (DteOccur, Pdetected, DeptRaisedBy, DeptResp, NCDescrip, NCCause, NCImmediateAct, NCLocation, PNumOrRef, EventCat, ReportedEmailAddy) VALUES (@DteOccur, @Pdetected, @DeptRaisedBy, @DeptResp, @NCDescrip, @NCCause, @NCImmediateAct, @NCLocation, @PNumOrRef, @EventCat, @ReportedEmailAddy) SELECT @NCC_ID=SCOPE_IDENTITY() INSERT INTO tblStatusTiming (NCIDLINK, StatusType) VALUES (@NCC_ID, 1) Set @NCC_Output_ID = SCOPE_IDENTITY() END
Code:
?Execute_Stored_Procedure("sp_Submit_NCC","@DteOccur='12/11/2011', @PDetected='Chris', @DeptRaisedBy=2, @DeptResp=2, @NCDescrip='test', @NCCause='test', @NCImmediateAct='test', @NCLocation='test', @PNumOrRef='test', @EventCat=2, @ReportedEmailAddy='', @NCC_Output_ID=@NCC_Output OUTPUT",True)
Code:
declare @NCC_OUTPUT int exec sp_Submit_NCC @DteOccur='12/11/2011', @PDetected='Chris', @DeptRaisedBy=2, @DeptResp=2, @NCDescrip='test', @NCCause='test', @NCImmediateAct='test', @NCLocation='test', @PNumOrRef='test', @EventCat=2, @ReportedEmailAddy='', @NCC_Output_ID=@NCC_Output OUTPUT
Code:
X = MyQ.Parameters("@NCC_OUTPUT")
Thank you for any help.
Chris
Comment