[AccessD] ...close open forms

Shamil Salakhetdinov shamil at users.mns.ru
Wed Jan 18 08:53:13 CST 2006


William,

It's unclear what do you mean by saving data before closing form - do you
have unbound forms?

For bound forms this code should work well:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long
Private Const WM_CLOSE = &H10

Public Sub CloseForms( _
   ByRef robjApp As Access.Application, _
   Optional ByRef rcolFormNames2Skip As Collection = Nothing)
Dim lngIdx As Long
Dim efrm As Access.Form
    With robjApp.Forms
        If .Count > 0 Then
            For lngIdx = .Count - 1 To 0 Step -1
               Set efrm = .Item(lngIdx)
               If Not SkipThisForm(rcolFormNames2Skip, efrm) Then
                  ' force to save data in bound form before
                  '  closing it
                  efrm.SetFocus
                  DoEvents
                  SendKeys "+{Enter}", True
                  ' close form
                  SendMessage efrm.hWnd, WM_CLOSE, 0, 0
                  DoEvents
               End If
            Next lngIdx
        End If
    End With
End Sub

Private Function SkipThisForm( _
         ByRef rcolFormNames2Skip As Collection, _
         ByRef rfrm As Access.Form) As Boolean
Dim evar As Variant
    If Not rcolFormNames2Skip Is Nothing Then
       For Each evar In rcolFormNames2Skip
          If StrComp(CStr(evar), rfrm.Name, vbTextCompare) = 0 Then
             SkipThisForm = True
             Exit Function
          End If
       Next evar
    End If
    SkipThisForm = False
End Function

It can be called this way from Switchboard form:

Dim col As New Collection
    col.Add Me.Name
    CloseForms Access.Application, col


You can also implement a special predefined public method in all your forms,
which can be called to save data before closing the form etc....

Shamil

----- Original Message ----- 
From: "William Hindman" <wdhindman at bellsouth.net>
To: "Access Developers discussion and problem solving"
<accessd at databaseadvisors.com>
Sent: Wednesday, January 18, 2006 5:01 PM
Subject: [AccessD] ...close open forms


> ...I need to close any open forms ...other than designated forms ...and
save
> their data ...before opening a called form ...from the called form.
>
> ...ie call a form open from a switchboard ...the called form checks for
any
> other forms open, other than the sb, and closes them, saving their data,
> before it opens.
>
> William
>
>
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com




More information about the AccessD mailing list