Results 1 to 3 of 3

Thread: If query exists

  1. #1
    Join Date
    Jun 2006

    Unanswered: If query exists

    I'm trying to implement what should be a simple line of code, however, I can't figure out an easy way to do it. I'm not very familiar with VBA or SQL but I've been placed in charge of administering a small database for my team. The expression that I would like to implement goes something like this:

    Private Sub Form_Load()
    'Ensure qryExitingQuery does not exist
    If 'The query qryExistingQuery exists Then
    DoCmd.DeleteObject acQuery, "qryExistingQuery"
    End If
    End Sub

    The DeleteOject command works just fine, but the part in red I need some help with. Any suggestions?

  2. #2
    Join Date
    Nov 2003
    Try this:

    ' DAO 3.x required.  
    Dim qdf As QueryDef
    For Each qdf In CurrentDb.QueryDefs
        If qdf.Name = "qryExistingQuery" Then
           DoCmd.SetWarnings False
           DoCmd.DeleteObject acQuery, "qryExistingQuery"
           DoCmd.SetWarnings True
           Exit For
        End If
    Next qdf
    Set qdf = Nothing
    Self Taught In ALL Environments.....And It Shows!

  3. #3
    Join Date
    Jun 2006
    Oh, that's marvelous. Thanks CyberLynx.

    I've got another question I'm having trouble finding an answer to.

    Is there a way to determine whether or not a user has a particular query open? Obviously, if someone is viewing the query, I don't want to delete it, but will create a new query and test all queries on the search click instead of the load event. I've been fooling around with the .IsOpen command but can't seem to figure out the correct syntax, or whether or not this is even a valid object.

    Any ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts