This is separate enough from my previous question I think it warrants its own spot. That question was answered sufficiently. This is different. I'm using CONTEXT_INFO to pass user information to an activity logging trigger in SQL from an ADP project. It works great. I'm trying to implement the same strategy from an MDB but it's not working. It appears that although an ADP maintains an active connection with the backend while the ADP is open, I guess an MDB does not. Can anyone confirm this? And if so, can anyone suggest a way to force a permanent connection while the MDB is open so that CONTEXT_INFO will work? I prefer this strategy over another someone suggested but I will go the other way if I have no choice. It seems to me this should work since CONTEXT_INFO was originally intended for use between web apps and SQL Server.
Here is the "set" code in the backend:
ALTER PROCEDURE [Audit].[SetUsersContextInfoForSession_SP]
(
@UserInitials VARCHAR(5),
@CI VARCHAR(100) OUTPUT
)
AS
BEGIN TRY
SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @ucv VARBINARY(MAX)
SET @ucv = (SELECT UsersContextInfo FROM dbo.tblUsers WHERE UserInitials = @UserInitials)
SET CONTEXT_INFO @ucv
SET @CI = CAST(CONTEXT_INFO() AS VARCHAR(100))
END TRY
BEGIN CATCH
I also have a "get" routine for testing only:
ALTER PROCEDURE [Audit].[GetUsersContextInfoForSession_SP]
(
@UserIntitials VARCHAR(5) OUTPUT
)
AS
BEGIN TRY
SET NOCOUNT ON
SET XACT_ABORT ON
SET @UserIntitials = (SELECT UserInitials FROM dbo.tblUsers WHERE UsersContextInfo = CONTEXT_INFO())
END TRY
BEGIN CATCH
And then here's the code in Access:
'set the context info for this user's session
Dim cmdSetCI As ADODB.Command
Set cmdSetCI = New ADODB.Command
With cmdSetCI
.ActiveConnection = sConnectionForStoredProcedures
.CommandType = adCmdStoredProc
.CommandText = "Audit.SetUsersContextInfoForSession_SP"
.Parameters.Append .CreateParameter("@UserIntitials", adVarChar, adParamInput, 5, strCurrentUserInitials)
.Parameters.Append .CreateParameter("@CI", adVarChar, adParamOutput, 30, strCurrentUserInitials)
.Execute
MsgBox .Parameters.Item("@CI").Value
End With
'test to see if it worked
Dim cmdGetCI As ADODB.Command
Set cmdGetCI = New ADODB.Command
With cmdGetCI
.ActiveConnection = sConnectionForStoredProcedures
.CommandType = adCmdStoredProc
.CommandText = "Audit.GetUsersContextInfoForSession_SP"
.Parameters.Append .CreateParameter("@UserIntitials", adVarChar, adParamOutput, 5, strCurrentUserInitials)
.Execute
MsgBox .Parameters.Item("@UserIntitials").Value
End With
Again, the first block of code in Access works fine. And so does the second one in the ADP project. It returns what I expect. But in the MDB, the second block of code fails because the routine it calls returns @UserIntials = NULL
Thanks
AR