The locking happens only when I am trying to execute DoCmd.OutputTo. Opening and closing the report doesn't raise this Error. It dropped the table successfully.We can't answer that directly - but can tell you how to determine that answer. Among the properties of a report are two places to look.
On the property sheet for the report, "Other" tab, check the lock setting. "No locks" should be chosen for any report.
The next place to look is the .Recordsource, which involves an expressed or implied query. The query can also have a lock setting (again on ITS property sheet). Again, for reports and queries that drive reports, you should choose "No locks."
One possibility is if you have any VBA code behind the scenes of the report and that code attempts in ANY WAY to update, insert, or delete information in any table. It happens sometimes, like when people want to track report usage in a logging table. Be sure to use minimum locking on any such updates.
A question also comes to mind: Is this a shared database for which another person might be using the back end file? If so, the other person might be holding the lock.
If it is a standalone single-user DB, is there something else that is open at the time? (Since you are using VBA to do this, actually there has to be something open...) Could that something else be doing the locking?
If none of those help, come back.
I'm not sure how the DoEvents could unlock the table. I already tried inserting the DoEvents after the DoCmd.OutputTo line, nothing changed. DoCmd.OutputTo command still holding the table. I've seen some suggestion from the net as what you've suggested, neither it solved their problem.insert a DoEvents after the report output
Any chance you could post a db copy here, with just enough stuff and instructions on how to replicate the problem?
Private Sub Command0_Click()
DoCmd.OpenReport "report1", acViewReport
'DoEvents 'doesn't help
DoCmd.Close acReport, "report1"
DoCmd.RunSQL ("DROP TABLE TABLE1")
End Sub
I doubt it would work. I guess you forgot to mention where the DoCmd.OutputTo should took place.Move the entire process to a form click button and it works but the report won't remain open even if it's modal property is set to True, which I find surprising. If you need to see the report, then I guess you'd have to split the process up, possibly as in suggestion b) above
Note that whatever testing you do it seems that you must close the db when the error is generated before trying anything new, otherwise the lock will remain even if you would solve the problem. Here's what I tried that works (form command button click)
Code:Private Sub Command0_Click() DoCmd.OpenReport "report1", acViewReport 'DoEvents 'doesn't help DoCmd.Close acReport, "report1" DoCmd.RunSQL ("DROP TABLE TABLE1") End Sub
Sorry about that. And I was completely sober too. At least I learned something from this - we can add our names to the list of those who had the exact same issue and never found a solution for the outputto method.I guess you forgot to mention where the DoCmd.OutputTo should took place.