Jesufer Vn
Jesufer Vn

Reputation: 13760

How can I close a single form instance in Access using VBA?

My application is designed to open several instances of the same form 'Request'. In the plot, by clicking in a specific button within the form a series of update actions may occur, followed by a close call of the current instance.

I've been able to manage the update operation for the current record. However, by calling

Docmd.Close

or

Docmd.Close acForm, Me.Name, acSaveNo

the close event is triggered on the first opened instance of the form, and not necessarily the instance that triggered the operation.

Is there a way to close the specific instance by using VBA code?

Upvotes: 1

Views: 1382

Answers (1)

Gustav
Gustav

Reputation: 55806

Either identify the forms as you open these by their hWnd property, and/or add them to a collection where you identify them by a string of your choice.

The code could go like this:

Option Compare Database
Option Explicit

Private colForms As Collection
Private blnFrmsInit As Boolean

Function MyFormOpen(strFrmName As String, strInstanceName As String) 

    Dim frm As Form

    If blnFrmsInit = False Then
        Set colForms = New Collection
        blnFrmsInit = True
    End If
    Set frm = New Form_lfrmSpecialtyPreferences
    frm.Caption = strInstanceName
    colForms.Add frm, strInstanceName
End Function

Function MyFormClose(strInstanceName As String)
    colForms.Remove strInstanceName
End Function

Upvotes: 1

Related Questions