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
475 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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 …
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 will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

679 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