tags:

views:

43

answers:

4

I have some code which re-arranges some items on a form, but only one SQL query. All my tables aren't locked before the code runs but for some reason I get an error when running:

DoCmd.RunSQL ("Select * Into MasterTable From Year07 Where 'ClassName' = '7A'")

Error:

The database engine could not lock table because it is already in use by another person or process. (Error 3211) To complete this operation, a table currently in use by another user must be locked. Wait for the other user to finish working with the table, and then try the operation again.

Any ideas what I can do to stop the table being locked?

A: 

I have seen this when you re-open a database after Access has crashed. Typically for me a reboot has fixed this.

Nate Bross
I've tried restarting, but the problem hasn't gone away :(
Tim
A: 

What version of MSAccess? Not sure about newer ones, but for Access 2003 and previous, if you were sure nobody was in the database, you could clear up locks after a crash by deleting the .ldb file.

Bill
It's access 2007. I've tried deleted the .ldb but as soon as I open the original .accdb again I get the same problem.
Tim
+4  A: 

Is MasterTable included in your form's Record Source? If so, you can't replace it, or modify its structure, while the form is open.

Apart from the table lock issue, there is a logic error in the SELECT statement.

Where 'ClassName' = '7A'

The string, ClassName, will never be equal to the string, 7A. Therefore your SELECT can never return any records. If ClassName is the name of a field in your Year07 table, discard the quotes which surround the field name.

Where ClassName = '7A'
HansUp
Yes the MasterTable is in the form's record source. Do you know how I can get around this without having to close the form, perform the action and then re-open the form? I've also changed the 'ClassName' issue, although in an SQL datasheet view it didn't make a different. Thanks again :)
Tim
My best guess is you should substitute `Select * From Year07 Where ClassName = '7A'` wherever MasterTable appears in your form's Record Source. IOW, you may not even need MasterTable as an actual table. But that is only my guess. You may get a better guess if you edit your question to show us the Record Source.
HansUp
The idea is to have many tables containing different data but to only display one at a time (chosen by a combo box). When the combo box is changed, the values in the MasterTable change to show only what is needed. I wasn't sure in access how to do this properly, but know now. Thanks! :)
Tim
Huh. I misread this answer yesterday -- Hans says the same thing I do in his first paragraph and then quickly moves onto something else. In reading, the first part didn't register for me, or I never would have posted my own answer. +1
David-W-Fenton
You said it better, David ... especially the point that Make Table queries don't belong in production applications.
HansUp
+3  A: 

I'm guessing, but if you're using a form that is bound to MasterTable, you can't run a query to replace it with a new MasterTable while you've got it open in the form.

I would suggest that you get rid of the MakeTable query (SELECT INTO) and instead use a plain append query (INSERT). You'll want to clean out the old data before appending the new, though.

Basically, a MakeTable query is, in my opinion, something that does not belong in a production app, and any process that you've automated with a MakeTable query should be replaced instead with a persistent temp table that is cleared before the new data is appended to it.

David-W-Fenton
+1 "a MakeTable query is, in my opinion, something that does not belong in a production app"
onedaywhen