VB5, VB6Make Sure the Child Forms are Really Dead
Option Explicit
'API declarations for destroying child forms
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

Private Declare Function GetWindowLong Lib "User32" _
   Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal _
   nIndex As Long) As Long
Private Const GWL_STYLE = (-16)

'Collection of children
Private chChildren() As Long

Private Function DestroyChildren() As Boolean
Dim i As Integer, iNumChildren As Integer
DestroyChildren = True
iNumChildren = -1
'Find the number of child forms
On Error Resume Next
iNumChildren = UBound(chChildren)
On Error GoTo 0
If iNumChildren = -1 Then Exit Function
'Rip through the child forms, send a close message
'to each, wait and then check for them again.
For i = 0 To iNumChildren
   If chChildren(i) = 0 Then
   Else
      Call SendMessage(chChildren(i), WM_CLOSE, 0, 0)
      DoEvents
      If GetWindowLong(chChildren(i), GWL_STYLE) = _
         0 Then
         chChildren(i) = 0
      Else
         DestroyChildren = False
         Exit For
      End If
   End If
Next i
End Function
Listing 5 When the parent form closes, the DestroyChildren procedure uses the SendMessage API to send each child form the WM_CLOSE message. Because child forms might validate data or otherwise halt their own closing, the GetWindowLong API checks for their demise. Use SendMessage to close child forms, then check to be sure they actually died.