?
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
Medium Priority
?
491 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 2000 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

719 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