Solved

Inserting records to SQL via stored procedure with ADO in VBA in Excel is erroring on one field

Posted on 2013-10-30
12
460 Views
Last Modified: 2013-10-30
I've got an Excel spreadsheet with a lot of data I want to get into a table in an SQL 2012 server.  It is working very well with the exception of one particular field.  In the code below, everything inserts properly with the exception of @SolutionID.  If I supply a default value for @SolutionID, the code executes and all 1200 or so records get inserted.  If I don't supply a default value for @SolutionID, I get a dialog box that says
Procedure or function 'usp_InsertLogSolutions' expects parameter '@SolutionID', which was not supplied.
.  I've tried eliminating some of the other fields in case cmd.Parameters was getting too long.  Here is the VBA...
"A of other code"


Set cnn = New ADODB.Connection
cnn.Open sConnString
 
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnn
 
With cmd
    .CommandType = adCmdStoredProc
    .CommandText = "usp_InsertLogSolutions"
End With
 
Set param = New ADODB.Parameter
With param
    .Name = "@Length"
    .Type = adSingle
    .Size = 8
End With
cmd.Parameters.Append param
   
Set param = New ADODB.Parameter
With param
    .Name = "@Segment1"
    .Type = adSmallInt
    .Size = 2
End With
cmd.Parameters.Append param
 
Set param = New ADODB.Parameter
With param
    .Name = "@Segment2"
    .Type = adSmallInt
    .Size = 2
End With
cmd.Parameters.Append param
 
Set param = New ADODB.Parameter
With param
    .Name = "@Segment3"
    .Type = adSmallInt
    .Size = 2
End With
cmd.Parameters.Append param
 
Set param = New ADODB.Parameter
With param
    .Name = "@Segment4"
    .Type = adSmallInt
    .Size = 2
End With
cmd.Parameters.Append param

Set param = New ADODB.Parameter
With param
    .Name = "@Segment5"
    .Type = adSmallInt
    .Size = 2
End With
cmd.Parameters.Append param

Set param = New ADODB.Parameter
With param
    .Name = "@DiamClass"
    .Type = adUnsignedTinyInt
    .Size = 1
End With
cmd.Parameters.Append param

Set param = New ADODB.Parameter
With param
    .Name = "@Species"
    .Type = adUnsignedTinyInt
    .Size = 1
End With
cmd.Parameters.Append param

Set param = New ADODB.Parameter
With param
    .Name = "@SolutionID"
    .Type = adInteger
    .Size = 4
End With
cmd.Parameters.Append param

For intSolSet = 1 To intNumSolSet
    For intRowOffset = 257 To 456
    
        With rngSolSet(intSolSet)
            
            cmd.Parameters("@Length") = .Range("A1").Offset(intRowOffset, 0).Value
            cmd.Parameters("@Segment1") = .Range("B1").Offset(intRowOffset, 0).Value
            cmd.Parameters("@Segment2") = .Range("C1").Offset(intRowOffset, 0).Value
            cmd.Parameters("@Segment3") = .Range("D1").Offset(intRowOffset, 0).Value
            cmd.Parameters("@Segment4") = .Range("E1").Offset(intRowOffset, 0).Value
            cmd.Parameters("@Segment5") = .Range("F1").Offset(intRowOffset, 0).Value
            cmd.Parameters("@Segment5") = .Range("F1").Offset(intRowOffset, 0).Value
            cmd.Parameters("@DiamClass") = intDiamClass(intSolSet)
            cmd.Parameters("@Species") = intSpecies(intSolSet)
            cmd.Parameters("@SolutionID") = intSolnID
            
        End With
        cmd.Execute
        
    Next intRowOffset
Next intSolSet
 
cnn.Close
Set cmd = Nothing
Set param = Nothing
Set cnn = Nothing

Open in new window


The SQL Stored procedure looks like this...
ALTER PROCEDURE [dbo].[usp_InsertLogSolutions]
	@Length Float,
	@Segment1 smallint =0,
	@Segment2 smallint =0,
	@Segment3 smallint = 0,
	@Segment4 smallint =0,
	@Segment5 smallint = 0,
	@DiamClass tinyint = 0,
	@Species tinyint = 0,
	@Date datetime = null,
	@SolutionID int
	

AS

Set @Date = Coalesce(@Date, GetDate())
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	Insert Into MerchSolns (Length, Segment1, Segment2, Segment3, Segment4, Segment5,
	DiamClass, Species,	Date, SolutionID)
		Values (@Length, @Segment1, @Segment2, @Segment3, @Segment4, @Segment5,
		@DiamClass, @Species, @Date, @SolutionID)

Open in new window



Is it something obvious I'm missing?
0
Comment
Question by:StudmillGuy
  • 8
  • 4
12 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39612391
Where in your code is intSolnID set to a value?
0
 

Author Comment

by:StudmillGuy
ID: 39612402
It was set in the code above, but I've tried hard coding various values for it.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39612408
If you're getting that error message, then I would wonder if the value is being assigned to the variable correctly.

To test it, change the SP line 11 declaration to below and execute, and see what gets inserted
	@SolutionID int = 99999

Open in new window

0
 

Author Comment

by:StudmillGuy
ID: 39612410
At the moment, it appears that there's something magical about the name @SolutionID or its position in the order of the fields.  When I changed the name to @Test throughout the table, SP and VBA and inserted it first, it worked.
0
 

Author Comment

by:StudmillGuy
ID: 39612452
Hmmmm.  I had made some other changes before I got your suggestion.  I have changed the field name to Test throughout and it is now working.  I did add the = 9999, in the SP, but it is inserting the correct value now that the field is named Test.
0
 

Author Comment

by:StudmillGuy
ID: 39612458
Hmmmm.  I hadn't mentioned that the date thing wasn't working before, but now it's working too, i.e. inserting the current date and time if one isn't supplied.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:StudmillGuy
ID: 39612471
Now I have changed the field name from Test to SolutionNum and it is working.
0
 

Author Closing Comment

by:StudmillGuy
ID: 39612494
Well, the solution I accepted, isn't really the solution, but it put me on the right track to fixing the problem.  Somehow all of the changing I did to the SP, the table, and the VBA, corrected the problem, so I suspect it was some kind of syntax error.  I also renamed the [/i]Date[/i] column to [/i]DateIns[/i] and made it the last column.  Now everything is working properly.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39612522
Glad to hear it.  Thanks for the grade, good luck with your project.  -Jim
0
 

Author Comment

by:StudmillGuy
ID: 39612538
You might keep hovering.  I'm bound to run into more difficulty :).
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39612547
As I'm 8k away from a million in the MS SQL Server zone, I'll take all you can throw at me.
0
 

Author Comment

by:StudmillGuy
ID: 39612552
My guardian angel.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now