Connecting Tech Pros Worldwide Forums | Help | Site Map

Determine if query "x" exists

Member
 
Join Date: Aug 2007
Posts: 62
#1: Jan 30 '08
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

Expand|Select|Wrap|Line Numbers
  1. With CurrentDb
  2. .QueryDefs.Delete ("qryName")
  3. Set qrydef = .CreateQueryDef("qryName", strSQL)
  4. .Close
  5. 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:

Expand|Select|Wrap|Line Numbers
  1. If qryExists("qryName") Then
  2. .QueryDefs.Delete("qryName")
  3. End If
Any help would be greatly appreciated!

Thanks!



ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#2: Jan 31 '08

re: Determine if query "x" exists


Quote:

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

Expand|Select|Wrap|Line Numbers
  1. With CurrentDb
  2. .QueryDefs.Delete ("qryName")
  3. Set qrydef = .CreateQueryDef("qryName", strSQL)
  4. .Close
  5. 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:

Expand|Select|Wrap|Line Numbers
  1. If qryExists("qryName") Then
  2. .QueryDefs.Delete("qryName")
  3. End If
Any help would be greatly appreciated!

Thanks!

Expand|Select|Wrap|Line Numbers
  1. Dim qdf As DAO.QueryDef
  2.  
  3. For Each qdf In CurrentDb.QueryDefs
  4.   If qdf.Name = "qryName" Then
  5.     CurrentDb.QueryDefs.Delete "qryName"
  6.      Exit For
  7.   End If
  8. Next
Member
 
Join Date: Aug 2007
Posts: 62
#3: Jan 31 '08

re: Determine if query "x" exists


Quote:

Originally Posted by ADezii

Expand|Select|Wrap|Line Numbers
  1. Dim qdf As DAO.QueryDef
  2.  
  3. For Each qdf In CurrentDb.QueryDefs
  4.   If qdf.Name = "qryName" Then
  5.     CurrentDb.QueryDefs.Delete "qryName"
  6.      Exit For
  7.   End If
  8. Next


Works like a charm! Thanks Adezii.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#4: Jan 31 '08

re: Determine if query "x" exists


Quote:

Originally Posted by nickvans

Works like a charm! Thanks Adezii.

You are quite welcome.
Reply


Similar Microsoft Access / VBA bytes