Microsoft KB Archive/95644

{|
 * width="100%"|

ACC1x: Form Handle Not Released When Closed with Access Basic

 * }

Q95644

-

The information in this article applies to:


 * Microsoft Access versions 1.0, 1.1

-

SYMPTOMS
After closing a form (using the Close macro action) that was opened through Access Basic code, the file handle for the table that the form is based on is not released. Because the file handle is not released, no functions that require exclusive use on that table can be executed (for example, deleting the table). When a function that needs exclusive use of that table is executed, the following error will occur:

Couldn't lock table ' '; currently in use.

CAUSE
You are not guaranteed that the user interface (UI) will be synchronized with the underlying Microsoft Access code in all cases. In this case, the record locking is done behind the scenes. Microsoft Access releases the table when it hits its idle loop (that is, the UI optimization).

RESOLUTION
After the form is opened in Access Basic code, insert a DoEvents command before the function that needs exclusive use of the table, to cause Microsoft Access to yield execution to the operating system, so that it can process events. When the DoEvents command occurs it allows Microsoft Access to process the pending unlock sequence.

STATUS
This behavior no longer occurs in Microsoft Access version 2.0.

MORE INFORMATION
The following steps provide example code that reproduces this error. There is a comment in the Access Basic code identifying where to insert a DoEvents command to resolve this error.

Steps to Reproduce Problem
  Open the sample database NWIND.MDB. Create a module and enter the following code:

     '************************************************************* 'Declarations section of the module. '*************************************************************

Option Explicit

'================================================================     ' Create and save the following TempCust function in the Module '================================================================     Function TempCust DoCmd SelectObject a_table, &quot;Customers&quot;, True DoCmd CopyObject, &quot;Cust2&quot; DoCmd OpenForm &quot;Customers 2&quot;, a_normal, &quot;&quot;, &quot;&quot;, a_edit, a_dialog '**** This is where the DoEvents should be inserted to fix **** DoCmd SelectObject a_table, &quot;Cust2&quot;, True DoCmd DoMenuItem 1, 1, 4 End Function  Open the Customers form in Design view, and change the RecordSource property to Cust2.  Create a command button on this form with the following properties:

     Object: Command Button ---     ControlName: Close Form Caption: Close Form OnPush: ExitForm  Save the modified Customers form as Customers 2.  Create and save the following macro:

     Macro Name   Action ---     ExitForm     Close   Create and save the following macro:

     Macro Name     Action     Argument OpenCustForm  RunCode    TempCust </li> From the Database window, run the OpenCustForm macro that you just created. The Customers 2 form will open.</li>  Click the Close Form macro button on this form. You will receive a dialog box that asks:

<pre class="FIXEDTEXT">     Delete Table 'Cust2'? </li>  Choose OK. You will receive the following error message:

Couldn't lock table 'Cust2'; currently in use.

</li></ol>

Keywords : kbprg

Issue type : kbprb

Technology :