Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check of existence of a Query

Status
Not open for further replies.

WynneSMI

Programmer
Dec 16, 2002
76
US
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

Thanks!
 
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.
 
forgot the

End Function

after the set db = nothing :)

hehe

Randall Vollen
National City Bank Corp.

Just because you have an answer - doesn't mean it's the best answer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top