Hi, I have a Make Table Query in one source database that produces a
Table in a different destination database. I want to be able to run
the source query from the destination database. I have read on the
newsgroups you have to do this using db.Execute, this fails though as
the Table already exists. I want it to overwrite the table each time,
probably a very simple task but is there a command for deleting a
table in vba? I have also read that it is best to run a delete query
then append query to repopulate but can this be done from a different
database?
Number of points: you can manipulate a mdb other than the CurrentDB, but
you have to open a handle to it first:
Set dbOther = DBEngine.OpenDatabase(c_strOtherMDBPath, true, true)
dbOther.Execute "DROP TABLE TempTable", dbFailOnError
More to the point though, is that deleting and recreating tables leads to
a lot of file bloat unless you are regularly compacting too. It's putting
the other tables at risk of corruption too. You might be better advised
to create a special temporary mdb just for the temp file, and deleting it
at the end of the session.
Set dbTemp = DBEngine.CreateDatabase(c_strTemp, etc)
With dbTemp
.Execute strCreateTable, dbFailOnError
.Execute strExportDate, dbFailOnError
' etc
.Close
End With
Kill c_strTemp
Another approach, if you need to keep the temp table until the next time
it's refreshed, would be to empty it and append the new records; as
oppposed to deleting and starting afresh. There's less mangling of the
mdb file; bloat will happen but should be less; you don't have to
redefine the PK and other indexes (which don't get made after a make-
table query); and you don't have to take down any relationships.
Try this:
With dbOther
.Execute "DELETE FROM TempTable", dbFailOnError
.Execute "INSERT INTO TempTable SELECT etc etc ", dbFailOnError
End With
Sorry for the long post, but deleting and creating whole tables is a big
deal for a database and is practically always not just unneccessary but
undesirable. Think of it like changing the engine in a car when you only
need to change the oil...
Hope that helps
Tim F