title: If Query Already Exists Then Delete the Entire Query
body: <p>I have a form that creates two queries, exports them to Excel and then deletes them. However, when I hit an error on my exporting, it doesn't make it the deletions. How would I go about checking to see if they already exist? And if they do, delete them so I can re-create them with the new/updated data?</p>
  
  <p>Code so far:</p>
  
  <pre><code>Dim qdfNewQry As Object
  Dim qdfNewWS As Object
  
  '//----- qdfNewQry
  If Not IsNull(DLookup("myExportQry", "MSysObjects", "Name='myExportQry'")) Then
      CurrentDb.QueryDefs.Delete qdfNewQry.Name
      Set qdfNewQry = CurrentDb.CreateQueryDef("myExportQry", exportQry)
  Else
      Set qdfNewQry = CurrentDb.CreateQueryDef("myExportQry", exportQry)
  End If
  
  '//----- qdfNewWS
  If Not IsNull(DLookup("myExportWS", "MSysObjects", "Name='myExportWS'")) Then
      CurrentDb.QueryDefs.Delete qdfNewWS.Name
      Set qdfNewWS = CurrentDb.CreateQueryDef("myExportWS", exportWS)
  Else
      Set qdfNewWS = CurrentDb.CreateQueryDef("myExportWS", exportWS)
  End If
  </code></pre>
  
  <p>I'm getting the error "The expression you entered as a query parameter produced this error: 'myExportQry'" on the line <code>If Not IsNull(DLookup("myExportQry", "MSysObjects", "Name='myExportQry'")) Then</code></p>
  
  <p>I'm pretty lost it seems. Any help/advice/corrections would be greatly appreciated!</p>
  
  <p><strong>EDIT1:</strong></p>
  
  <p>Just for clarification, I'm wanting to delete the <strong>entire</strong> query. Other alternative solutions would also be welcome!</p>