Link to home
Start Free TrialLog in
Avatar of mlcktmguy
mlcktmguyFlag for United States of America

asked on

Error Calling Stored Procedure with Multiple Parameters

I am a long time user of Access but new to using a SQL Server backend in Access.

I have written my second stored procedure in SQL, it is to insert a record into a table.  The parameters passed to the SP are the fields to be populated in the table.  Here's the SP"

USE [JTSConversion]
GO
/****** Object:  StoredProcedure [dbo].[spAddNameAddressWork]    Script Date: 7/30/2015 10:25:20 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spAddNameAddressWork] 
	@ID                   int,
	@ControlNumber        int,
	@SequenceNumber       int,
	@AddrLine1            nvarchar(30),
	@AddrLine2            nvarchar(30),
	@AddrLine3            nvarchar(30),
	@AddrLine4            nvarchar(30),
	@City                 nvarchar(50),
	@State                nvarchar(2),
	@Zip                  nvarchar(50),
	@UnMailable           bit,
	@Bankruptcy           bit,
	@AddressCheckNeeded   bit,
	@SelectForAct20       bit,
	@MuniCode             smallint

As

Begin

Set NOCOUNT on;

Insert dbo.SQNA_NameAddress_Work (ID, 
	ControlNumber, 
	SequenceNumber,
	AddrLine1,
	AddrLine2,
	AddrLine3,
	AddrLine4,
	City, 
	State,
	Zip,
	UnMailable,
	Bankruptcy,
	AddressCheckNeeded,
	SelectForAct20, 
	MuniCode)
	Values (@ID, 
	@ControlNumber, 
	@SequenceNumber,
	@AddrLine1,
	@AddrLine2,
	@AddrLine3,
	@AddrLine4,
	@City, 
	@State,
	@Zip,
	@UnMailable,
	@Bankruptcy,
	@AddressCheckNeeded,
	@SelectForAct20, 
	@MuniCode)


END

Open in new window


Here is the code I created so far to call the stored procedure.

Public Sub addNameAddr_SQL_SP(wkID As Long, _
                              wkMuniCode As Long, _
                              wkControlNumber As Long, _
                              wkSequenceNumber As Long, _
                              wkAddrLine1 As String, _
                              wkAddrLine2 As String, _
                              wkAddrLine3 As String, _
                              wkAddrLine4 As String, _
                              wkCity As String, _
                              wkState As String, _
                              wkZip As String, _
                              wkUnMailableYN As Boolean, _
                              wkBankruptcyYN As Boolean, _
                              wkAddrCheckNeededYN As Boolean, _
                              wkSelectForAct20YN As Boolean)
                              
'
Dim adCn As ADODB.Connection
Dim adCmd As ADODB.Command
Dim adRs As ADODB.Recordset
Dim sConn As String

Const spName = "dbo.spAddNameAddressWork"

sConn = "DRIVER=SQL Server;SERVER=MIKE-PC\WIN71SQLSERVER;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=JTSConversion;"

Set adCn = New ADODB.Connection
adCn.Open sConn

Set adCmd = New ADODB.Command
adCmd.ActiveConnection = adCn
adCmd.CommandText = spName
adCmd.CommandType = adCmdStoredProc
adCmd.Parameters.Refresh
adCmd.Parameters(1).Value = wkID
adCmd.Parameters(3).Value = wkControlNumber
adCmd.Parameters(4).Value = wkSequenceNumber
adCmd.Parameters(5).Value = wkAddrLine1
adCmd.Parameters(6).Value = wkAddrLine2
adCmd.Parameters(7).Value = wkAddrLine3
adCmd.Parameters(8).Value = wkAddrLine4
adCmd.Parameters(8).Value = wkCity
adCmd.Parameters(9).Value = wkState
adCmd.Parameters(10).Value = wkZip
adCmd.Parameters(11).Value = wkUnMailableYN
adCmd.Parameters(12).Value = wkBankruptcyYN
adCmd.Parameters(13).Value = wkAddrCheckNeededYN
adCmd.Parameters(14).Value = wkSelectForAct20YN
adCmd.Parameters(15).Value = wkMuniCode

'
Set adRs = adCmd.Execute
'
adRs.Close
adCn.Close
'
Set adRs = Nothing
Set adCmd = Nothing
Set adCn = Nothing
'
End Sub

Open in new window


Finally the routine I created to test the call to fill the parameters used to call the stored procedure.

Public Sub testAddNameAddrSQL90()
'
Dim wkID  As Long
Dim wkMuniCode  As Long
Dim wkControlNumber  As Long
Dim wkSequenceNumber  As Long
Dim wkAddrLine1  As String
Dim wkAddrLine2  As String
Dim wkAddrLine3  As String
Dim wkAddrLine4  As String
Dim wkCity As String
Dim wkState As String
Dim wkZip As String
'
Dim wkUnMailableYN As Boolean
Dim wkBankruptcyYN As Boolean
Dim wkAddrCheckNeededYN As Boolean
Dim wkSelectForAct20YN As Boolean
'
wkID = 1111
wkMuniCode = 222
wkControlNumber = 333
wkSequenceNumber = 444
wkAddrLine1 = "This is Addr1"
wkAddrLine2 = "This is Addr2"
wkAddrLine3 = "This is Addr3"
wkAddrLine4 = "This is Addr4"
wkCity = "SomeCity"
wkState = "ST'"
wkZip = "55555"
'
wkUnMailableYN = True
wkBankruptcyYN = False
wkAddrCheckNeededYN = True
wkSelectForAct20YN = False
'
addNameAddr_SQL_SP wkID, _
                   wkMuniCode, _
                   wkControlNumber, _
                   wkSequenceNumber, _
                   wkAddrLine1, _
                   wkAddrLine2, _
                   wkAddrLine3, _
                   wkAddrLine4, _
                   wkCity, _
                   wkState, _
                   wkZip, _
                   wkUnMailableYN, _
                   wkBankruptcyYN, _
                   wkAddrCheckNeededYN, _
                   wkSelectForAct20YN
'
End Sub

Open in new window


In the routine calling the stored procedure I am getting an error on the statement

adCmd.Parameters(9).Value = wkState

The error is: 3421 Application uses a value of the wrong type for the current operation

I checked the contents of wkState are 'ST' exactly as expected.  The parameter is defined as nvarchar(2) and the 'State' field in the table is defined as nvarchar(2)

Can anyone spot an issue?

Here's a snapshot of the table definitionUser generated image
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Line 29 in testAddNameAddrSQL90

wkState = "ST'"

It has an extra single quote in it.
Beaten by 7 seconds!
Avatar of mlcktmguy

ASKER

Thanks, I could have looked at that for hours and never seen that.