Click here to Skip to main content
15,891,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

SQL
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:

SQL
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
VB
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
Posted
Updated 27-Nov-14 0:28am
v2

1 solution

MS Access does not support[^] something like CONTEXT_INFO[^] in MS SQL Server.

Every time when *.mdb database is opened, the database engine creates *.ldb file which contains several info about connection, logged users, etc. You can read it using VBA[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900