gilweber3
asked on
Passing NULL parameters to Stored Proc in Excel VBA
I'm trying to call a stored procedure that contains NULL parameters using Excel VBA.
The code works in SQL Server Management Studio -
Exec SP_AML_WIPS_Account_Data_L ookup '5670934',NULL,NULL
Here's my code vba code -
Dim intDDA As Variant
Dim intTIN As Variant
Dim intPN As Variant
intDDA = ThisWorkbook.Worksheets("W IPS Data Lookup").Range("E3").Value
intTIN = ThisWorkbook.Worksheets("W IPS Data Lookup").Range("E5").Value
intPN = ThisWorkbook.Worksheets("W IPS Data Lookup").Range("E7").Value
If intDDA = 0 Then
intDDA = Null
Else
intDDA = ThisWorkbook.Worksheets("W IPS Data Lookup").Range("E3").Value
End If
If intTIN = 0 Then
intTIN = "Null"
Else
intTIN = ThisWorkbook.Worksheets("W IPS Data Lookup").Range("E5").Value
End If
If intPN = 0 Then
intPN = "Null"
Else
intPN = ThisWorkbook.Worksheets("W IPS Data Lookup").Range("E7").Value
End If
With ActiveWorkbook.Connections ("WIPs_Acc ount_Data" ).OLEDBCon nection
.CommandText = "EXECUTE dbo.SP_AML_WIPS_Account_Da ta_Lookup '" & intDDA & "'" & "," & "'" & intTIN & "'" & "," & " '" & intPN & "'" 'Three parameters
End With
ActiveWorkbook.Connections ("WIPs_Acc ount_Data" ).Refresh
When I check the value of .CommantText, it reads
?.commandtext
EXECUTE dbo.SP_AML_WIPS_Account_Da ta_Lookup '6681365,'',''
I need it read as **EXECUTE dbo.SP_AML_WIPS_Account_Da ta_Lookup '6681365,NULL, NULL
I can't get NULL to be included in my EXECUTE statement, what am I missing, thanks for your help.
The code works in SQL Server Management Studio -
Exec SP_AML_WIPS_Account_Data_L
Here's my code vba code -
Dim intDDA As Variant
Dim intTIN As Variant
Dim intPN As Variant
intDDA = ThisWorkbook.Worksheets("W
intTIN = ThisWorkbook.Worksheets("W
intPN = ThisWorkbook.Worksheets("W
If intDDA = 0 Then
intDDA = Null
Else
intDDA = ThisWorkbook.Worksheets("W
End If
If intTIN = 0 Then
intTIN = "Null"
Else
intTIN = ThisWorkbook.Worksheets("W
End If
If intPN = 0 Then
intPN = "Null"
Else
intPN = ThisWorkbook.Worksheets("W
End If
With ActiveWorkbook.Connections
.CommandText = "EXECUTE dbo.SP_AML_WIPS_Account_Da
End With
ActiveWorkbook.Connections
When I check the value of .CommantText, it reads
?.commandtext
EXECUTE dbo.SP_AML_WIPS_Account_Da
I need it read as **EXECUTE dbo.SP_AML_WIPS_Account_Da
I can't get NULL to be included in my EXECUTE statement, what am I missing, thanks for your help.
ASKER
Thanks for your help on this but still no go.
When I debug .commandtext I get
?.commandtext
EXECUTE dbo.SP_AML_WIPS_Account_Da ta_Lookup 6681365,,
I need .commandtext to be
EXECUTE dbo.SP_AML_WIPS_Account_Da ta_Lookup 6681365, NULL, NULL
Just can't get the NULL parameters into the EXECUTE statement.
Thanks anyway.
When I debug .commandtext I get
?.commandtext
EXECUTE dbo.SP_AML_WIPS_Account_Da
I need .commandtext to be
EXECUTE dbo.SP_AML_WIPS_Account_Da
Just can't get the NULL parameters into the EXECUTE statement.
Thanks anyway.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Outstanding, that did the trick. Thanks for taking a look at this.
I would change:
intDDA = ThisWorkbook.Worksheets("W
to
intDDA = "'" & ThisWorkbook.Worksheets("W
same to other 2 parameters
and then change:
.CommandText = "EXECUTE dbo.SP_AML_WIPS_Account_Da
to:
.CommandText = "EXECUTE dbo.SP_AML_WIPS_Account_Da
I hope this answer will help you solving your problem.
Best Regards,
Nuno Rogado