D
Dave
I am trying to call a stored procedure from Access, but I am having problems
with optional parameters. If I execute the stored procedure from the SQL Mgmt
console it executes fine and inserts data into the table. When I try the same
thing from my VBA code no data is inserted. Here is the VBA code:
Public Sub Create_ScoreCard_Report_Data(ByVal FiscalYear As String, _
ByVal UserName As String, _
Optional ByVal PhysicianId As String =
"", _
Optional ByVal Department As String =
"", _
Optional ByVal Division As String = "", _
Optional ByVal Speciality As String = "")
'@FiscalYear AS NVARCHAR(4),
'@UserName as nvarchar(20),
'@PhysicianId AS NVARCHAR(255)= NULL,
'@Department AS NVARCHAR(255)= NULL,
'@Division AS NVARCHAR(10) = NULL,
'@Speciality AS NVARCHAR(255) = NULL --,
On Error GoTo Error_Create_ScoreCard_Report_Data
Set adoCmd = New ADODB.Command
Set adoParam = New ADODB.Parameter
Set adoRS = New ADODB.Recordset
With adoCmd
'set the connection last
Set .ActiveConnection = CurrentProject.Connection
'create the return parameter
'Set adoParam = .CreateParameter("@RecordsCreated", adInteger,
adParamReturnValue)
'.Parameters.Append adoParam
'create and append the input parameters
Set adoParam = .CreateParameter("@FiscalYear", adVarChar,
adParamInput, Len(FiscalYear), FiscalYear)
.Parameters.Append adoParam
Set adoParam = .CreateParameter("@UserName", adVarChar,
adParamInput, Len(UserName), UserName)
.Parameters.Append adoParam
If Len(PhysicianId) > 0 Then
Set adoParam = .CreateParameter("@PhysicianId", adVarChar,
adParamInput, Len(PhysicianId), PhysicianId)
.Parameters.Append adoParam
End If
If Len(Department) > 0 Then
Set adoParam = .CreateParameter("@Department", adVarChar,
adParamInput, Len(Department), Department)
.Parameters.Append adoParam
End If
If Len(Division) > 0 Then
Set adoParam = .CreateParameter("@Division", adVarChar,
adParamInput, Len(Division), Division)
.Parameters.Append adoParam
End If
If Len(Speciality) > 0 Then
Set adoParam = .CreateParameter("@Speciality", adVarChar,
adParamInput, Len(Speciality), Speciality)
.Parameters.Append adoParam
End If
'specify a stored prcoedure
.CommandType = adCmdStoredProc
'Brackets must surround stored procedure names with spaces
.CommandText = "sp_Create_ScoreCard_Report_Data"
'execute the the stored procedure
.Execute
'cleanup the ADO objects
Set adoParam = Nothing
Set adoRS = Nothing
Set adoCmd = Nothing
End With
Exit Sub
Error_Create_ScoreCard_Report_Data:
Debug.Print Err.Description
'cleanup the ADO objects
Set adoParam = Nothing
Set adoRS = Nothing
Set adoCmd = Nothing
End Sub
For example, if I run the store procedure from SQL Mgmt console specifying 1
optional parameter:
EXEC @return_value = [dbo].[sp_Create_ScoreCard_Report_Data]
@FiscalYear = N'2009',
@UserName = N'userid',
@Department = '022'
Data is inserted into the table as expected.
But when I try to call the VBA method above using the same parameter values,
no data is created. I can only surmise that the issue is that the VBA needs
to have something for the null/empty parameters.
The stored procedure is defined with default value of NULL for the optional
parameters.
Any help would be appreciated.
with optional parameters. If I execute the stored procedure from the SQL Mgmt
console it executes fine and inserts data into the table. When I try the same
thing from my VBA code no data is inserted. Here is the VBA code:
Public Sub Create_ScoreCard_Report_Data(ByVal FiscalYear As String, _
ByVal UserName As String, _
Optional ByVal PhysicianId As String =
"", _
Optional ByVal Department As String =
"", _
Optional ByVal Division As String = "", _
Optional ByVal Speciality As String = "")
'@FiscalYear AS NVARCHAR(4),
'@UserName as nvarchar(20),
'@PhysicianId AS NVARCHAR(255)= NULL,
'@Department AS NVARCHAR(255)= NULL,
'@Division AS NVARCHAR(10) = NULL,
'@Speciality AS NVARCHAR(255) = NULL --,
On Error GoTo Error_Create_ScoreCard_Report_Data
Set adoCmd = New ADODB.Command
Set adoParam = New ADODB.Parameter
Set adoRS = New ADODB.Recordset
With adoCmd
'set the connection last
Set .ActiveConnection = CurrentProject.Connection
'create the return parameter
'Set adoParam = .CreateParameter("@RecordsCreated", adInteger,
adParamReturnValue)
'.Parameters.Append adoParam
'create and append the input parameters
Set adoParam = .CreateParameter("@FiscalYear", adVarChar,
adParamInput, Len(FiscalYear), FiscalYear)
.Parameters.Append adoParam
Set adoParam = .CreateParameter("@UserName", adVarChar,
adParamInput, Len(UserName), UserName)
.Parameters.Append adoParam
If Len(PhysicianId) > 0 Then
Set adoParam = .CreateParameter("@PhysicianId", adVarChar,
adParamInput, Len(PhysicianId), PhysicianId)
.Parameters.Append adoParam
End If
If Len(Department) > 0 Then
Set adoParam = .CreateParameter("@Department", adVarChar,
adParamInput, Len(Department), Department)
.Parameters.Append adoParam
End If
If Len(Division) > 0 Then
Set adoParam = .CreateParameter("@Division", adVarChar,
adParamInput, Len(Division), Division)
.Parameters.Append adoParam
End If
If Len(Speciality) > 0 Then
Set adoParam = .CreateParameter("@Speciality", adVarChar,
adParamInput, Len(Speciality), Speciality)
.Parameters.Append adoParam
End If
'specify a stored prcoedure
.CommandType = adCmdStoredProc
'Brackets must surround stored procedure names with spaces
.CommandText = "sp_Create_ScoreCard_Report_Data"
'execute the the stored procedure
.Execute
'cleanup the ADO objects
Set adoParam = Nothing
Set adoRS = Nothing
Set adoCmd = Nothing
End With
Exit Sub
Error_Create_ScoreCard_Report_Data:
Debug.Print Err.Description
'cleanup the ADO objects
Set adoParam = Nothing
Set adoRS = Nothing
Set adoCmd = Nothing
End Sub
For example, if I run the store procedure from SQL Mgmt console specifying 1
optional parameter:
EXEC @return_value = [dbo].[sp_Create_ScoreCard_Report_Data]
@FiscalYear = N'2009',
@UserName = N'userid',
@Department = '022'
Data is inserted into the table as expected.
But when I try to call the VBA method above using the same parameter values,
no data is created. I can only surmise that the issue is that the VBA needs
to have something for the null/empty parameters.
The stored procedure is defined with default value of NULL for the optional
parameters.
Any help would be appreciated.