Hi,
I'm new to adp files and fairly new to stored procedures, although I've been coding in ADO for years. I'm having this horrible problem that's been stumping me for hours.
I have a stored procedure which takes the current user login (in my case, "Geek Katie"), and a user option (in this case, "Employee Search") and returns the integer value in the OptionValue column in a table.
- When I run this in Management Studio, it works perfectly.
- When I run this by double-clicking the stored procedure in the ADP file, it runs perfectly, but returns nothing. Unless I add a select statement to the stored procedure, in which case it works perfectly.
- When I run this through ADO (through some rather convoluted code that I've set up so that I can reuse code a lot), it gets and stores the correct parameters (I've confirmed this by examining the "comm" object while stepping through), and it runs... but then it returns Null. Every time. WHY is it returning Null???
Here's all the relevant code. I've snipped a bit, but left everything relevant.
Here's the stored procedure:
Here's the function that I've been testing (the one that successfully calls the procedure with the correct parameters, but returns Null):
And here's the "StoredProcedure" class module that's being called:
FYI, I've successfully run the Run_SP_Command_No_Return method against this database in the past. This is the first time I've tried to get Run_SP_Command_Reader_Value to work, and it's not working.
Here's what I know:
When stepping through, during the Run_SP_Command_Reader_Value method, after the "comm.Execute" line, when I examine the comm object, I see that there are three parameters. @OptionType = "Employee Search" - which is correct. @StaffLogin = "Geek Katie" - which is correct. @OptionValue is null, which is not correct.
In the Lookup_UserOptions table, there is currently a single record. In that record, StaffLogin = Geek Katie , OptionType = Employee Search , and OptionValue = 1 . Therefore, when I call this procedure with the parameters "Geek Katie" and "Employee Search", it should return 1. And it does. Unless I'm calling from the ADP file.
I have tried changing the OptionValue from sql_variant (which it was originally) to int. I have tried changing the parameters to use CreateParameter instead of setting each parameter setting individually. I have tried creating return values in the stored procedure, and adding a select statement so it returns a recordset, and updating the code so that it reads from the recordset instead of reading an output parameter. I have tried changing the input parameters to "N'Geek Katie'" and "N'Employee Search'". All with the same results. It always returns Null.
Please tell me what I'm doing wrong - I'd very much appreciate it!
Katie
I'm new to adp files and fairly new to stored procedures, although I've been coding in ADO for years. I'm having this horrible problem that's been stumping me for hours.
I have a stored procedure which takes the current user login (in my case, "Geek Katie"), and a user option (in this case, "Employee Search") and returns the integer value in the OptionValue column in a table.
- When I run this in Management Studio, it works perfectly.
- When I run this by double-clicking the stored procedure in the ADP file, it runs perfectly, but returns nothing. Unless I add a select statement to the stored procedure, in which case it works perfectly.
- When I run this through ADO (through some rather convoluted code that I've set up so that I can reuse code a lot), it gets and stores the correct parameters (I've confirmed this by examining the "comm" object while stepping through), and it runs... but then it returns Null. Every time. WHY is it returning Null???
Here's all the relevant code. I've snipped a bit, but left everything relevant.
Here's the stored procedure:
Code:
USE [UnionMembership]
GO
/****** Object: StoredProcedure [dbo].[sp_gen_All_GetUserOption] Script Date: 07/21/2010 19:02:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_gen_All_GetUserOption]
@OptionType varchar(30),
@StaffLogin varchar(30),
@OptionValue int = 0 output
WITH EXECUTE AS CALLER
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
SET NOCOUNT ON;
-- Note: if the select statement returns empty, then @OptionValue will be Null.
set @OptionValue = (SELECT OptionValue FROM Lookup_UserOptions WHERE (Lookup_UserOptions.StaffLogin = @StaffLogin) AND (Lookup_UserOptions.OptionType = @OptionType))
END TRY
BEGIN CATCH
EXECUTE [dbo].[uspLogError];
END CATCH;
END
GO
Here's the function that I've been testing (the one that successfully calls the procedure with the correct parameters, but returns Null):
Code:
Public spSetSelected As New StoredProcedure
Public Function GetUserOption(sOptionName As String) As Variant
On Error GoTo Err_GetUserOption
Dim sCurrUser As String, vUserOption As Variant
sCurrUser = GetCurrentUser
spSetSelected.OpenConnectionLocal
spSetSelected.sp_name = "sp_gen_All_GetUserOption"
spSetSelected.add_param "@StaffLogin", adVarChar, adParamInput, sCurrUser, 30
spSetSelected.add_param "@OptionType", adVarChar, adParamInput, sOptionName, 30
spSetSelected.add_param "@OptionValue", adInteger, adParamInputOutput
vUserOption = spSetSelected.Run_SP_Command_Reader_Value("@OptionValue")
spSetSelected.CloseStoredProcedure
GetUserOption = vUserOption
Debug.Print vUserOption
Exit_GetUserOption:
Exit Function
Err_GetUserOption:
MsgBox Err.Description, , "GetUserOption: " & Err.Number
Resume Exit_GetUserOption
End Function
And here's the "StoredProcedure" class module that's being called:
Code:
Option Compare Database
Option Explicit
Private comm As New ADODB.Command
Private cnSQL As New ADODB.Connection
Private m_sp_name As String
Private Sub Class_Initialize()
Set comm = New Command
End Sub
Public Sub OpenConnectionLocal()
Set cnSQL = CurrentProject.Connection
End Sub
Public Sub OpenConnection(sServer As String, sDatabase As String, sUID As String, sPwd As String, bolTrusted As Boolean) 'snipped - not relevant
End Sub
'add each parameter to the comm object
Public Sub add_param(param_name As String, param_type As DataTypeEnum, param_direction As ParameterDirectionEnum, Optional param_value As Variant, Optional param_size As Variant)
Dim param As New Parameter
param.Name = param_name
param.type = param_type
param.Direction = param_direction
If Not IsMissing(param_value) Then
param.Value = param_value
End If
If Not IsMissing(param_size) Then
If IsNumeric(param_size) Then
param.size = CLng(param_size)
End If
End If
comm.Parameters.Append param
End Sub
Public Function Run_SP_Command_Reader() As ADODB.Recordset
'Snipped - not relevant
End Function
'For recordsets that return values: run the procedure returning the resulting value
Public Function Run_SP_Command_Reader_Value(sOutputParamName As String) As Variant
Dim vReturnValue As Variant
comm.Execute
vReturnValue = comm.Parameters(sOutputParamName).Value
Run_SP_Command_Reader_Value = vReturnValue
End Function
'run the procedure returning the results to the passed recordset
Public Sub Run_SP_Command_No_Return()
comm.Execute
End Sub
Public Sub CloseStoredProcedure()
Set comm = Nothing
cnSQL.Close
Set cnSQL = Nothing
End Sub
Private Sub begin_setup()
If m_sp_name = "" Then Stop
With comm
.CommandText = m_sp_name
.CommandType = adCmdStoredProc
.ActiveConnection = cnSQL
End With
End Sub
Public Property Get sp_name() As String
sp_name = m_sp_name
End Property
Public Property Let sp_name(ByVal vNewValue As String)
m_sp_name = vNewValue
Call begin_setup
End Property
FYI, I've successfully run the Run_SP_Command_No_Return method against this database in the past. This is the first time I've tried to get Run_SP_Command_Reader_Value to work, and it's not working.
Here's what I know:
When stepping through, during the Run_SP_Command_Reader_Value method, after the "comm.Execute" line, when I examine the comm object, I see that there are three parameters. @OptionType = "Employee Search" - which is correct. @StaffLogin = "Geek Katie" - which is correct. @OptionValue is null, which is not correct.
In the Lookup_UserOptions table, there is currently a single record. In that record, StaffLogin = Geek Katie , OptionType = Employee Search , and OptionValue = 1 . Therefore, when I call this procedure with the parameters "Geek Katie" and "Employee Search", it should return 1. And it does. Unless I'm calling from the ADP file.
I have tried changing the OptionValue from sql_variant (which it was originally) to int. I have tried changing the parameters to use CreateParameter instead of setting each parameter setting individually. I have tried creating return values in the stored procedure, and adding a select statement so it returns a recordset, and updating the code so that it reads from the recordset instead of reading an output parameter. I have tried changing the input parameters to "N'Geek Katie'" and "N'Employee Search'". All with the same results. It always returns Null.
Please tell me what I'm doing wrong - I'd very much appreciate it!
Katie