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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,572
Members
448,972
Latest member
Shantanu2024

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
Back
Top