vbams-accessms-access-2016

How can I quit ms Access and log off computer with vba


We are using Remote desktop and MS access runtime to run accde database. it is more of data entry type with small user base of 5-10 users only, all using their individual accde files. the access file open at startup, user do their work and there is an exit button which quits access file. Now question is :

Is there any way to quit ms access application ( Docmd.Quit ) and logging off current user ( shell command "cmd.exe shutdown /l" ) at the same time? The problem is, if we quit application then shell command is not executed.

i have tried docmd.Quit and shell (cmd.exe shutdown /l) when user clicks exit. The application quits but log off is not done. I just want both things so that user uses only ms access and nothing else as it is not needed. Printing is done via network printing.


Solution

  • This creates a separate VBScript file with a short delay (in milliseconds so 3000 = 3 seconds ... you can adjust for your needs) so that you can quit Access during the delay, the VBScript then calls the shutdown command with the /l switch.

    Sub LogOff()
        Dim sCmd As String
        sCmd = "WScript.Sleep 3000" & vbNewLine
        sCmd = sCmd & "CreateObject(""WScript.Shell"").Run ""cmd.exe /c shutdown /l"""
        
        Dim sFilePath As String
        sFilePath = Environ$("tmp") & "\shutdown_script.vbs"
        
        With CreateObject("Scripting.FileSystemObject")
            If .FileExists(sFilePath) Then
                .DeleteFile sFilePath, True
            End If
            With .OpenTextFile(sFilePath, 2, True, 0) ' 2 = IOMode.ForWriting; 0 = TristateFalse (ASCII)
                .Write sCmd
                .Close
            End With
            
            CreateObject("WScript.Shell").Run sFilePath, 0, False
        End With
    End Sub
    

    ... you would then call LogOff before quitting Access eg

    LogOff
    DoCmd.Quit acQuitSaveAll