My objective is to check a list of queries, about 350 and determine which of them are functioning, rather than go and copy and paste each one I've written some vba which I think should do the job, unfortunately it currently isnt.
The basic idea is to create a field called working, in a table which contains the sql I'm running as a string. Open this table as a recordset, and run each line using the Docmd.RunSql statement. If the statement fails it should generate an error, then by making my error handler set working to false I can determine which queries are functioining.
The issue I havent been able to figure out yet is how I can check queries which aren't action queries. I thought that DoCmd.OpenSQL should work for this but I've had no luck thus far. Here is what I have.
Code:
Option Compare Database
Private Sub Command0_Click()
Dim rst As DAO.Recordset
Dim strSql As String
Dim db As DAO.Database
Dim ID As Integer
Set db = CurrentDb
Set rst = db.OpenRecordset("dan_test")
'Turn off annoying warnings and set the working variable to true
DoCmd.SetWarnings False
DoCmd.RunSQL ("UPDATE dan_test set Working = True ; ")
'Try the first case, if it succeeds do nothing, if it fails go to error handler
rst.MoveFirst
'Check the remaining cases, if it works do nothing, if it fails go to error handler
Do While Not rst.EOF
strSql = (rst.Fields("strNewValue"))
'If code is a select statement it should be treated differently
If Left(strSql, 6) = "select" Then
DoCmd.OpenQuery strSql
Else
DoCmd.RunSQL (strSql)
End If
On Error GoTo Err:
rst.MoveNext
Loop
MsgBox "DONE"
Exit Sub
'Error Handler updates the table changing the working field to false, and prints the Id number to verify accuracy
Err:
Debug.Print Error$(Err) & crlf & "ROW:" & rst!tblChangesID
rst.Edit
rst!working = False
rst!strNote = Error$(Err)
rst.Update
Err.Clear
Resume Next
Set rst = Nothing
DoCmd.SetWarnings True
End Sub