I am trying to pass variables/parameters from an Access table to a stored procedure in SQL Server using an ADODB connection. I am pulling the variables in Access from and Access query (so I’m trying to pass a variable that is updated each time the code is executed). I am getting the following error message when trying to pass the variables below in the Access VBA Code:
Parameter object is improperly defined. Inconsistent or incomplete information was provided.
I am new to this kind of coding. Could someone please provide some direction on how to pass variables (a result of an Access query) to a stored procedure, and then call those variables in the stored procedure?
Access VBA code:
'''CREATE LINKED TABLE THAT PULLS FROM TBL_STATEMENT_MASTER IN SQL
''variables to pass to get dFileRE
'' (1) stateDate
stateDate = getItem("select distinct paydate from update_statement_master_re")
'' (2) stateNGN
stateNGN = getItem("select distinct deal_code from update_statement_master_re")
user = GetUser()
'' Connect to Data Source - Securities DB - SQL Server
Set dbconn = New ADODB.Connection
dbconn.ConnectionString = "driver=SQL Server;server=R7SQL1;database=SecuritiesDB;trusted_connection=YES"
dbconn.Open dbconn.ConnectionString
Set cmd = New ADODB.Command
cmd.ActiveConnection = dbconn
'' Set CommandText equal to the stored procedure name (spStatementCheck)
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "spStatementCheck"
cmd.NamedParameters = True 'paramStatementCheck'
'' THIS IS THE PART OF THE CODE THAT IS CRASHING – I AM NOT SURE HOW TO CALL THESE VARIABLES
cmd.Parameters.Append _
cmd.CreateParameter("@SPstateNGN", adVarChar, adParamInput, 0, "& stateNGN &")
cmd.Parameters.Append _
cmd.CreateParameter("@SPstateDate", adDate, adParamInput, 0, "& stateDate &")
SQL Server stored procedure code:
USE [SecuritiesDB_TEST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spStatementCheck]
@SPstateNGN as nvarchar(25),
@SPstateDate as datetime
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO UPDATE_Statement_Master_Check ( NGN_Full, [Date], [DESCRIPTION], AMOUNT, NGN_SHORT, Series, FileDate, EffectiveDate, ActualDate, [Source], ID )
SELECT tbl_Statement_Master.NGN_Full, tbl_Statement_Master.[Date],
tbl_Statement_Master.[DESCRIPTION],
tbl_Statement_Master.AMOUNT,
tbl_Statement_Master.NGN_SHORT,
tbl_Statement_Master.Series,
tbl_Statement_Master.FileDate,
tbl_Statement_Master.EffectiveDate,
tbl_Statement_Master.ActualDate,
tbl_Statement_Master.[Source],
tbl_Statement_Master.ID
FROM tbl_Statement_Master
where tbl_Statement_Master.[Date] <> @SPstateDate
and tbl_Statement_Master.NGN_Full = @SPstateNGN
and tbl_statement_master.FileDate = (Select distinct max(filedate) from tbl_statement_master
where [DATE] = @SPstateDate and NGN_Full = @SPstateNGN);
GetItemlines would return multiple values and you are trying to pass these multiple values to theCreateParametercalls. Wouldn't you be looking to have a loop in there somewhere to loop through every record and execute the stored procedure based on the parameter values in that record. I don't understand the"& stateNGN &"part of creating the parameter either. In the past I simply place the variable in that argument without the quotes or the ampersand.GetItem, the second point was aboutCreateParameter("@SPstateNGN", adVarChar, adParamInput, 0, stateNGN), just removing the quotes and ampersands as whatever value is held in the variable is passed in as the parameter value.