Error catching code for creating and/or deleting queries that may already exist

  • Thread starter Thread starter Pete Straman Straman via AccessMonster.com
  • Start date Start date
P

Pete Straman Straman via AccessMonster.com

ometimes I have to run my program over because I have no error catching
code. For the code below my program stops running because the queries have
already been created. I need an if statement to either skip over making the
query if it has already been made or to delete and recreate it. what should
be in the if statement to accomplish this task.
thanks in advance.

My code is listed below.

Pete S.

Private Sub cmdRecordCount_Click()
Dim db060004 As Database
Dim qdfNew As QueryDef

Set dbs060004 = OpenDatabase("060004 Tara.mdb")

With dbs060004

' Create 060004 Original Record Count Query
Set qdfNew = .CreateQueryDef("060004_Original", "SELECT Count([060004
Original].facilityid)" _
+ " AS [060004 Original]FROM [060004 Original];")
.Close

End With

MsgBox "Record Count and 'trend_rpt' queries have been created."
End Sub
 
1. You should add error handing.

2. To hack around your current problem:

On Error Resume Next
Docmd.DeleteObject acQuery, "QueryName"
On Error Goto 0

Please visit the Access VBA coding newsgroups in the future for VBA
questions.
 
I put in an if statement and a function to check if the query already
exists. If it does I give a msgbox saying that it already exists and they
click OK to keep going.
So have I added error handling or is do you mean something else by error
handling?

Pete.Straman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top