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
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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
 

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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