Check for Query & Delete

Lynn_AccessUser

Registered User.
Local time
Today, 08:02
Joined
Feb 4, 2003
Messages
125
I have a form that on the close event I would like to check to see if a query exists.

For example:

Private Sub Form_Close()

If QueryName exists then
DoCmd.DeleteObject acQuery, QueryName
DoCmd.Close
DoCmd.OpenForm "FormName"
Else
DoCmd.Close
DoCmd.OpenForm "FormName"
End If

Thanks!
 
Lynn,

Something like the following should move you forward.

Code:
'declare variables
Dim obj As AccessObject
Dim dbs As Object
Dim strQryName As String

'assign values
Set dbs = Application.CurrentData
strQryName = "QueryName"

'see if query exists and, if so,
' delete it if it's not open
    For Each obj In dbs.AllQueries
       If obj.Name = strQryName Then
            If obj.IsLoaded = False Then
                  DoCmd.DeleteObject acQuery, obj.Name
            End If
       End If
    Next obj

Regards,
Tim
 

Users who are viewing this thread

Back
Top Bottom