Search code examples
sessionms-accessvbawinhttpobject-lifetime

MS Access: Way to maintain persistent session across multiple Form Events?


Hopefully this is a simple question, though the solution may not be. In MS Access, is it possible to instantiate a VBA class in the application or database scope?

What I want to do is persist a WinHttp instance with the same lifetime as the application or database so that as various Form Event handlers call a web service, they can do so using a persistent session object.

If not, it seems like I'll always need to grab and persist the session state, particularly the JSESSIONID, before the current instance goes out of scope and then quietly put it back on the next instantiation. This is coming up because I really can't afford the authentication overhead on the client or the server.

What would be the recommended way to maintain a physical or virtual persistent session across multiple Form Events?


Solution

  • Yes, this certainly is possible. Just dim it in a module outside of any sub or function (without the New keyword)

    Public winHttpObj As Object
    
    Public Sub CreateWinHttpObj
       Set winHttpObj = CreateObject("WinHttp.WinHttpRequest.5.1")
    End Sub
    

    You can refer to it from the same module, or from any other module by using the ModuleName.Variablename notation

    If it's desirable is a whole different question.