Determine if query "x" exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickvans
    New Member
    • Aug 2007
    • 62

    Determine if query "x" exists

    Hello all,

    I have (hopefully) an easy question for you all. I'm running Access '03 and have created a pile of VBA code to create a form and some queries related to each control in the form. (Basically, it uses structured naming conventions to create a form with a bunch of combo boxes that allow the user to easily add and remove controls... anyway--)

    I am using the statement

    Code:
    With CurrentDb
    .QueryDefs.Delete ("qryName")
    Set qrydef = .CreateQueryDef("qryName", strSQL)
    .Close
    End With
    to delete existing queries and make new ones, however my code returns an error message when the query doesn't exist when it tries to delete it.

    Is there a function that can tell me if a query with a specific name exists that I can use in an "If" statement? Ideally, it would be something like:

    Code:
    If qryExists("qryName") Then
    .QueryDefs.Delete("qryName")
    End If
    Any help would be greatly appreciated!

    Thanks!
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by nickvans
    Hello all,

    I have (hopefully) an easy question for you all. I'm running Access '03 and have created a pile of VBA code to create a form and some queries related to each control in the form. (Basically, it uses structured naming conventions to create a form with a bunch of combo boxes that allow the user to easily add and remove controls... anyway--)

    I am using the statement

    Code:
    With CurrentDb
    .QueryDefs.Delete ("qryName")
    Set qrydef = .CreateQueryDef("qryName", strSQL)
    .Close
    End With
    to delete existing queries and make new ones, however my code returns an error message when the query doesn't exist when it tries to delete it.

    Is there a function that can tell me if a query with a specific name exists that I can use in an "If" statement? Ideally, it would be something like:

    Code:
    If qryExists("qryName") Then
    .QueryDefs.Delete("qryName")
    End If
    Any help would be greatly appreciated!

    Thanks!
    [CODE=vb]
    Dim qdf As DAO.QueryDef

    For Each qdf In CurrentDb.Query Defs
    If qdf.Name = "qryName" Then
    CurrentDb.Query Defs.Delete "qryName"
    Exit For
    End If
    Next[/CODE]

    Comment

    • nickvans
      New Member
      • Aug 2007
      • 62

      #3
      Originally posted by ADezii
      [CODE=vb]
      Dim qdf As DAO.QueryDef

      For Each qdf In CurrentDb.Query Defs
      If qdf.Name = "qryName" Then
      CurrentDb.Query Defs.Delete "qryName"
      Exit For
      End If
      Next[/CODE]

      Works like a charm! Thanks Adezii.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by nickvans
        Works like a charm! Thanks Adezii.
        You are quite welcome.

        Comment

        Working...