Pass empty range to a stored procedure

Viewed 26

I am using an Excel template to update a SQL Server table. Each individual cell input in my template has a name defined and is passing to a stored procedure that inserts a record into my table. I've found if a named range is empty it throws an error. Sometimes a field is purposefully blank so I'll need to be able to insert a NULL or empty string.

Here is the VBA:

Public Sub Example()

  Application.ScreenUpdating = False

  Dim connection As ADODB.connection
  Dim command As ADODB.command
  Dim parameter As ADODB.parameter
 
  Set connection = GetConnection()
  Set command = GetStoreProcCommand("[dbo].[usp_InsertRecord]")

  Set parameter = command.CreateParameter("@Field1", adVarChar, adParamInput, 250)
  command.parameters.Append parameter
  command.parameters("@Field1").Value = wkbSheet.Range("Field1NameRange").Value 

  command.Execute
  connection.Close
End Sub

I've found that if I set the value to NULL when I create the parameter, i.e.

  Set parameter = command.CreateParameter("@Field1", adVarChar, adParamInput, 250, NULL)

but populate my named range, the NULL will be passed to the stored procedure.

So I think the issue is here

  command.parameters("@Field1").Value = wkbSheet.Range("Field1NameRange").Value

but I don't know why its breaking or how to fix it.

Thanks!

0 Answers
Related