A
Anita
I have a "Last Updated" subform that is embedded in several data entry forms.
In case a user has several data entry forms open at once, I need to capture
which particular data entry form is being updated so that the Last Updated
info is correctly assigned to the right record.
My idea was to create a collection that captures the name of all the open
data entry forms, populate a list box with this collection and allow the user
to manually pick the right form name.
I got the collection to work, but when I try to open "frmSelectParentForm"
and populate it using the .Rowsource method, my code starts breaking.
The main problem I'm encountering is that frmSelectParentForm will not stay
open -- despite the fact that I haven't written any code to close it. After
the subroutines run that populate the listbox's rowsource with values, the
form closes before I can select a value.
Any ideas for why this is occurring?
**********Main procedure******************
Public Function Store_Form_Name(Optional FormName As String)
Static MyParentFormName As String
If Not FormName = "" Then
MyParentFormName = FormName
ElseIf MyOpenFormsCollection.Count > 1 Then
DoCmd.OpenForm "frmSelectParentForm" ****opens subform that's closing
unexpectedly
Form_frmSelectParentForm.Populate_List_Box
MyParentFormName = "Need to select"
Else
MyParentFormName = MyOpenFormsCollection.Item(1)
End If
Store_Form_Name = MyParentFormName
End Function
***********Collection Function*****************************
Public Function MyOpenFormsCollection() As Collection
Dim counter As Integer
Dim colOpenFormNames As New Collection
Dim OpenFormName As Variant
Dim intForms As Integer
Dim frm As Form
intForms = Forms.Count ' Number of open forms.
If intForms > 0 Then
For counter = 0 To intForms - 1
Set frm = Forms(counter)
With colOpenFormNames
If Not (frm.Name = "frmSelectParentForm") Then
.Add Item:=frm.Name
End If
End With
Next counter
End If
Set MyOpenFormsCollection = colOpenFormNames
End Function
*********frmSelectParentForm .Rowsource subroutine**************
Public Sub Populate_List_Box()
'Created November 2006 - Currently in testing
Me.lboxMyOpenForms.RowSource = GetOpenFormNames
Me.lboxMyOpenForms.SetFocus
If Me.lboxMyOpenForms.ListIndex = -1 Then
MsgBox "Click the appropriate form.", , "Data Entry Form not selected"
Else
Call btnLast_Update_Source_OK_Click
End If
End Sub
*******GetOpenFormNames subroutine that finds the Rowsource values****
Public Function GetOpenFormNames()
Dim MyList As String
Dim OpenFormName As Variant
MyList = ""
For Each OpenFormName In MyOpenFormsCollection
MyList = MyList & OpenFormName & ";"
Next OpenFormName
GetOpenFormNames = MyList
End Function
In case a user has several data entry forms open at once, I need to capture
which particular data entry form is being updated so that the Last Updated
info is correctly assigned to the right record.
My idea was to create a collection that captures the name of all the open
data entry forms, populate a list box with this collection and allow the user
to manually pick the right form name.
I got the collection to work, but when I try to open "frmSelectParentForm"
and populate it using the .Rowsource method, my code starts breaking.
The main problem I'm encountering is that frmSelectParentForm will not stay
open -- despite the fact that I haven't written any code to close it. After
the subroutines run that populate the listbox's rowsource with values, the
form closes before I can select a value.
Any ideas for why this is occurring?
**********Main procedure******************
Public Function Store_Form_Name(Optional FormName As String)
Static MyParentFormName As String
If Not FormName = "" Then
MyParentFormName = FormName
ElseIf MyOpenFormsCollection.Count > 1 Then
DoCmd.OpenForm "frmSelectParentForm" ****opens subform that's closing
unexpectedly
Form_frmSelectParentForm.Populate_List_Box
MyParentFormName = "Need to select"
Else
MyParentFormName = MyOpenFormsCollection.Item(1)
End If
Store_Form_Name = MyParentFormName
End Function
***********Collection Function*****************************
Public Function MyOpenFormsCollection() As Collection
Dim counter As Integer
Dim colOpenFormNames As New Collection
Dim OpenFormName As Variant
Dim intForms As Integer
Dim frm As Form
intForms = Forms.Count ' Number of open forms.
If intForms > 0 Then
For counter = 0 To intForms - 1
Set frm = Forms(counter)
With colOpenFormNames
If Not (frm.Name = "frmSelectParentForm") Then
.Add Item:=frm.Name
End If
End With
Next counter
End If
Set MyOpenFormsCollection = colOpenFormNames
End Function
*********frmSelectParentForm .Rowsource subroutine**************
Public Sub Populate_List_Box()
'Created November 2006 - Currently in testing
Me.lboxMyOpenForms.RowSource = GetOpenFormNames
Me.lboxMyOpenForms.SetFocus
If Me.lboxMyOpenForms.ListIndex = -1 Then
MsgBox "Click the appropriate form.", , "Data Entry Form not selected"
Else
Call btnLast_Update_Source_OK_Click
End If
End Sub
*******GetOpenFormNames subroutine that finds the Rowsource values****
Public Function GetOpenFormNames()
Dim MyList As String
Dim OpenFormName As Variant
MyList = ""
For Each OpenFormName In MyOpenFormsCollection
MyList = MyList & OpenFormName & ";"
Next OpenFormName
GetOpenFormNames = MyList
End Function