I have a stored query in an Access 2007 database. I have Access 2010 on this machine. I am trying to do the following:
- export an Excel spreadsheet as a temp table in the database.
- add a column to the temp table and populate it with the filename
- update a linked table using the contents of the exported table.
The update is a stored query in the Access front end. When I run the update query from Access, it works fine. But when I run it from VBA using the code:
sub test()
filename=thisworkbook.name
Set db_fe = OpenDatabase("C:\Data\myDB.mdb")
If TableExists(db_fe, "tempCorrection") Then
DoCmd.RunSQL "drop table tempCorrection;"
End If
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "tempCorrection", "C:\Data\corrections.xls", True
DoCmd.RunSQL "alter table tempCorrection add column newColumn text;"
DoCmd.RunSQL "update tempCorrection set newColumn='" & filename & "';", dbFailOnError
db_fe.Execute "updateCorrections", dbFailOnError
DoCmd.RunSQL "drop table tempCorrection;"
end sub
then on the "db_fe.execute" line I get Run-time error '3078': "The Microsoft Access database engine cannot find the input table or query 'tempCorrection'. Make sure it exists and that its name is spelled correctly."
Here's what query updateCorrections looks like:
UPDATE production AS p
INNER JOIN tempCorrection AS t
ON
(p.filename=t.filename)
AND
(p.a1=t.a1)
AND
(p.a2=t.a2)
set p.a3=t.a3
Any ideas as to why I'm having trouble executing this query from VBA?
DoCmd.RunSQL "drop table tempCorrection;"runs correctly, that means the tabletempCorrectionis in the currently opened database, not in the other one. This is really sloppy coding -- you should never mix DoCmd.RunSQL with .Execute. Indeed, you should simply never use RunSQL at all...it's way to dangerous..RunSQLupdate query with.Execute, and replaced the.RunSQLALTER COLUMN with.tabledefsthen.createField. I'm still getting this error. should I post my changed code in an answer below?JRO.JetEngineobject and calling itsRefreshCachemethod using the connection object (e.g. see VBA in this answer ). Don't know how to do this using ye olde DAO, though ;)DBEngine.Idle dbRefreshCache