How would I go about checking for the existence of a query in Access VBA? I've tried something like the following but it does not seem to work properly:
If Not db.QueryDefs(strQueryName) Is Nothing Then
blnExists = True
End If
For Each AllQueries In CurrentDb.QueryDefs
QueryName = AllQueries.NAME
If QueryName = strQueryName Then
DoCmd.DeleteObject acQuery, QueryName
End If
Next AllQueries
Or..
this function returns TRUE if it exists, FALSE if it doesnt
Function ffindQuery(StrQueryName As String) As Boolean
Dim db As DAO.Database
Dim i As Integer 'counter
ffindQuery = False 'assumes false
Set db = DBEngine.Workspaces(0).Databases(0) 'can also be dbegine(0)(0) or currentdb()
db.QueryDefs.Refresh
For i = 0 To db.QueryDefs.count - 1 'index starts at 0 to count -1
If StrQueryName = db.QueryDefs(i).Name Then
'Query Exists if true
ffindQuery = True
Exit For
End If
Next i
Set db = Nothing
Hope it helps!
Randall Vollen
National City Bank Corp.
Just because you have an answer - doesn't mean it's the best answer.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.