Question

I have a form MFForm that relies on a temp table MFTable. I want to refresh the contents of this table whenever the form opens.

To do this, I'm using the following VBA code in MFForm:

Private Sub Form_Open(Cancel As Integer)

CurrentDb.execute "drop table MFTable", dbFailOnError
CurrentDb.execute "select * into MFTable from MFQuery", dbFailOnError

End Sub

But when I double-click the form to open it, I get the following error on the drop table command:

Run-time error '32111:

The database engine could not lock table 'MFTable'
because it is already in use by another person or process.

I think the problem is that as soon as I click on the form to run it, the temp table goes into use, even before the Form_Open() function executes. How can I make sure that the table's contents are refreshed whenever the form opens?

Was it helpful?

Solution

Here I assume your table's schema is not constantly changing, and clear the table instead of dropping it.

CurrentDb.Execute "delete * from MFTable", dbFailOnError
CurrentDb.Execute "insert into MFTable select * from MFQuery", dbFailOnError
Me.Requery

If your schema is changing constantly then you will need to execute that code before the form is opened. I have not tested this but you may be able to make your form a subform of another form and have that other form drop and recreate the table before the subform opens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top