Select Queries Staying Open

Learn2010

Registered User.
Local time
Today, 15:21
Joined
Sep 15, 2010
Messages
415
I am involved in a project that is using a SQL back end. I am having issues with "unable to save" messages when I quit the application. I have noticed that if I close a form to look at something, on many occasions the select query behind the form or subform is open. Is there a way to close all forms, subforms, and queries with VBA without having to identify the exact names, when I use either

DoCmd.Quit
or
Application.Quit?

Thank you.
 
Well, for this, you might use something like this:

Code:
Sub CloseQueries()
    Dim obj As AccessObject, dbs As Object
    Set dbs = Application.CurrentData
    ' Search for open AccessObject objects in AllQueries collection.
    For Each obj In dbs.AllQueries
        If obj.IsLoaded Then
            'Close the object.
            DoCmd.Close acQuery, obj.Name
        End If
    Next obj
End Sub

You can then do the same with AllForms rather than AllQueries.
 
This worked great. I was able to make a few adjustments and now it seems to be going well.

Thank you.
 
To take a different tack, when you open a form are you separately opening its source query? If so you don't need to.
 
In the beginning it wasn't. Someone else was brought in to do the SQL part and this began showing up. Why they did it that way I don't know. I am now dealing with the leftovers in the project. I will see if I can find that.

Thanks for your help.
 
No problem. The solution Frothingslosh provided will certainly work, but personally I'd rather stop the issue from happening in the first place rather than work around it after the fact.
 

Users who are viewing this thread

Back
Top Bottom