Back To Top

MS Access VBA Check if Query Exists for versions 2000, 2002, 2003, 2007

Go here To Check if Table exists in VBA.
Go here To Check if Field exists in VBA.
Go here To Get Query's SQL in VBA


The following code snippet will use VBA to check if a Query exists within a given database.
The VBA Check if Query exists Sample is available for download here in a zipped MS Access Database.




Function
ifQueryExists(QueryName As String ) As Boolean
  
Dim
Db As Database 'DAO Vars
  
Dim QDF As DAO.QueryDef
  
'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library
   'Set references by Clicking Tools and Then References in the Code View window
   'Checks if Query exists.
   'USAGE: ifQueryExists "QueryName"

  
On Error GoTo NoQuery 'If there is no Query capture the error.

  
Set Db = CurrentDb()

  
'If Query is there return True

  
For Each QDF In Db.QueryDefs
     
If QDF.Name = QueryName Then

        
ifQueryExists = True

        
Db.Close
        
Set Db = Nothing
         Exit For
      End If
   Next

   Exit Function

NoQuery:
  
'If Query is not there close out and set function to false
  
Db.Close
  
Set Db = Nothing

  
ifQueryExists = False
   Exit Function
End Function


For the above VBA If Query Exists Function in a usable Form with Button, download the Sample database in ZIP format. The sample includes 1 table with 100 records each and 1 Query to search for.


You may post this tutorial on your website or in a forum. If you do please maintain a Link to Eraseve AP.



For further MS Access tutorials go here: MS Access tutorials

Go here To Check if Table exists in VBA.
Follow the Set Autonumber Field with VBA Link to Set a Field to Autonumber.

 

Did you find what you were looking for?
What would you suggest?
mail@eraserve.com
Your Name:
Your Message:
Your Email:

    

 

Custom Search