views:

62

answers:

4

I have four make table queries that need to be ran in a certain order and on a daily time schedule. I tried creating a macro that would open them in order but it required human interaction to exit out of the warnings and it failed to move on to the 2nd query. Any ideas?

A: 

First thought: have you tried adding

On Error Resume Next

to your macro? Not the cleanest way of doing things, but it will get the job done.

JohnK813
Warning messages? Not an error...
astander
Ah, misread the question. I have a similar setup that gives me errors, not warnings, so I must have been thinking of that.
JohnK813
I shudder whenever I see "On error resume next" other than with a line handling the errors immediately after the statement.
Tony Toews
A: 

I suggest you use a little code and Execute. To quote http://stackoverflow.com/questions/1872554/ms-access-how-to-automatically-select-yes-in-warning-message-boxes/1873705#1873705.


It is generally best to use Execute in such cases in order to trap errors:

Dim db As Database, qdf As QueryDef, strSQL As String

Set db = CurrentDb
Set qdf = db.QueryDefs("Query17")
qdf.Execute dbFailOnError
Debug.Print qdf.RecordsAffected

Or

strSQL="UPDATE SomeTable SET SomeField=10"
db.Execute strSQL, dbFailOnError
Debug.Print db.RecordsAffected

Trapping errors with dbFailOnError and an error trap is more or less essential and there are a number of other useful aspects to the Execute Statement

Remou
Error trapping was not the question. The warning messages was the issue here, correct?
astander
Without any warnings, queries can fail silently, so it is as well to note the errors.
Remou
You should never turn of SetWarnings. For a link to the code for my SQLRun function that wraps up a SQL Execute with error handling (and other features) see http://stackoverflow.com/questions/1872554/ms-access-how-to-automatically-select-yes-in-warning-message-boxes/1877234#1877234 .
David-W-Fenton
@David W Fenton perhaps you mean this comment for a different post? There is no mention of set warnings in this post, furthermore, the link is to the thread which mentions your function.
Remou
+1  A: 

Don't run make table. Leave the tables there but just clear out the records using a delete query.

Also see the TempTables.MDB page at my website which illustrates how to use a temporary MDB in your app. This avoids bloating your database file.

Tony Toews
Also, temp tables should not be stored in the back end or in the front end, but in a separate temp database.
David-W-Fenton
A: 

I agree with Tony Toews in that you should probably avoid MakeTable. When I first used Access, I would use MakeTable queries to update data in tables. I soon found out that this was a really wonky way of doing things.

I would suggest that you convert your MakeTable queries to append queries and make a separate "delete query" for each of the tables that wipes out each item in your original tables (I've also created a "universal" delete query in the pass with a corresponding method that takes in a table name and then rewrites the SQL in the universal delete method to match that table. However, it's probably easier to just create separate delete queries).

I might then write a procedure to execute the queries progamatically. I'm pretty sure the below code should work (though I usually use DAO and create local variables for db (the currentDb) and qdf (the query def), in this example I just run it all from CurrentDb(), which I believe will work).

Sub UpdateMyTables()
    'Turn Warnings Off              '
    DoCmd.SetWarnings False

    'Wipe Data from Tables        '
    CurrentDb().QueryDefs("qryDelete Table1").Execute
    CurrentDb().QueryDefs("qryDelete Table2").Execute

    'Update the tables with the new data from append       '
    CurrentDb().QueryDefs("qryAppend Table1").Execute
    CurrentDb().QueryDefs("qryAppend Table2").Execute

    'Turn Warnings back On        '
    DoCmd.SetWarnings True
End Sub
Ben McCormack
Using .execute means that docmd.setwarnings isn't required. I also strongly discourage use of docmd.setwarnings in VBA code due to it's masking of additional errors.
Tony Toews
Is that so? Perhaps I just always put it in without knowing that. I was probably in the exact situation as the question's author and thus that's how I learned to use `DoCmd`. Just curious, what kind of errors might be masked by using `DoCmd.SetWarnings False`?
Ben McCormack
Actually it's the docmd.runsql errors that are masked by using documd.runsql. So by not using documd.runsql you don't need documd.setwarnings.
Tony Toews