How to close access database that was opened with Shell?

rberke used Ask the Experts™
We have an Excel application that gets data from MS Access.

6 of our computers do not have access installed, so those computers use the access Run Time minimum (version is art2kmin) along with the Shell function.

It has been working well for several years but there is one annoying problem.  Once the database gets open it stays open.  How can I close the database when access is done with it.  

I tried SendMessage(, wm_close)   but that did not work.

I also don't want to kill all Access Tasks because the user might have other databases opened at the same time.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CLOSE = &H10
Private lNotepadhWnd As Long

Sub test()
    Dim dummy2 As Long
    Dim db As Object
' very small program for sql
    Path = "S:\XYZ program files\Back End Billing.mdb"
    pathArt2k = Replace("C:\Program Files\Microsoft Office\ART\Office\msaccess.exe `S:\XYZ program files\Back End Billing.mdb`", "`", Chr(34))
        On Error Resume Next
    Set db = CreateObject(Path)
    If db Is Nothing Then

        dummy2 = Shell(pathArt2k, vbMaximizedFocus)
        If dummy2 = 0 Then dummy2 = Shell(pathArt2kB, vbMaximizedFocus)
        If dummy2 = 0 Then
            MsgBox "Could not open Assess Database"
            Set db = GetObject(Path)
        End If
    End If

sqlPre = "select * from [out] where apben = 222229 "
Set rsoutpre = db.currentdb.OpenRecordset(sqlPre)
MsgBox rsoutpre.RecordCount
Set rsoutpre = Nothing

Set db = Nothing

If dummy2 > 0 Then Call SendMessage(dummy2, WM_CLOSE, 0, 0)
End Sub

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Will Application.Quit or DoCmd.Quit work in the runtime version???



You made it easy for me.  Changed the last code as follows.  Thanks

' MsgBox rsoutpre.RecordCount
Set rsoutpre = Nothing

Set db = Nothing

End Sub
Eric ShermanAccountant/Developer

Glad I could help.


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial