0

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.

  • Can you not just save the Database (ie so it is up to date on the file system) and then issue the log-off command ... Access (along with all other applications) will be closed as part of the log-off process? – JohnM Jul 04 '23 at 06:21
  • Sorry, I forgot to mention, it is front end. The Back end is Maria DB. So data is already saved. The question is only to close front end and log off. Thanks for reply – Viney Kumar Jul 04 '23 at 06:36

2 Answers2

0

All you need is this code line in your button's click event:

Private Sub ButtonLogOff_Click()

    Shell "Shutdown /l /f"
    
End Sub
Gustav
  • 53,498
  • 7
  • 29
  • 55
  • Will it not corrupt the file as it is not properly closed ( "Docmd.Quit" command ) ? I forgot to mention it is front end application. Otherwise "shut down /l " worked like charm. – Viney Kumar Jul 04 '23 at 06:39
  • It doesn't turn off the computer, only closes open applications, then logs the user out - as if the clicked logout. Also, before calling that code, do your own clean-up. – Gustav Jul 04 '23 at 10:13
0

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
JohnM
  • 2,422
  • 2
  • 8
  • 20
  • Thank you very much. It works as intended. It quits MS access and then logs off after specified time. Can we just hide the script running?, it shows for a moment then computer logs off. – Viney Kumar Jul 04 '23 at 12:58
  • The VBScript itself doesn't have a window ... cmd does but that window is (should be) hidden ... are you sure the window is not related to some other app / process that is briefly showing a dialog before it is force-closed? You could try to swap the relevant line for this `sCmd = sCmd & "CreateObject(""WScript.Shell"").Run ""cmd.exe /c shutdown /l"", 0"` but I doubt will make a difference? – JohnM Jul 04 '23 at 13:26
  • No it is the cmd window only, but shows only for a moment. Otherwise no problem. works beautifully after changing the sleep to 5000 (Access takes time to quit!) – Viney Kumar Jul 04 '23 at 13:37
  • Did you try replacing the relevant line per my comment above ... it just adds ', 0' towards the end of the line, this explicitly tells cmd not to have a window ... see also [SO answer](https://stackoverflow.com/questions/32297699/hide-command-prompt-window-when-using-exec) and other answers – JohnM Jul 04 '23 at 14:18
  • Yes, I tried and it works nicely. No cmd window is shown. It just exits the access file and logs off normally after specified period. Thank a lot. – Viney Kumar Jul 05 '23 at 12:08