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
487 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 66

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 66

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 66

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 66

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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

628 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