check if query exists

L

Legacy 15162

Guest
I want to write an if statement in code that will check to see if a query exists....if it does then delete and create a new query.

stmaxdate = "SELECT tblSFT.Storeno, tblSFT.Year, tblSFT.Period, tblSFT.Week, Max(tblSFT.Report_Date) AS MaxOfReport_Date" & _
" FROM tblSFT" & _
" GROUP BY tblSFT.Storeno, tblSFT.Year, tblSFT.Period, tblSFT.Week"

CurrentDb.CreateQueryDef "tmpmaxdate", stmaxdate
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,318
Office Version
  1. 365
Platform
  1. Windows
You could use something like this:
Code:
Function IsQuery(qryName As String) As Boolean
' returns true if there is a query called qryName

Dim db As Database
Dim qdf As QueryDef

Set db = CurrentDb

    For Each qdf In db.QueryDefs
        IsQuery = (qdf.Name = qryName)
        If IsQuery Then Exit Function
    Next
    
End Function
 

Stubby Holder

New Member
Joined
Jul 29, 2003
Messages
26
or, just delete it anyway, & skip the error if it doesn't exist.

On Error Resume Next
DoCmd.DeleteObject acQuery, "tmpmaxdate"
On Error GoTo 0
 

Forum statistics

Threads
1,147,665
Messages
5,742,500
Members
423,733
Latest member
hamann666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top