问题
http://allenbrowne.com/ser-35.html
The code below is Allen Browne's code to open multiple instances of the same form. Problem is that I need each forms data to be based on the drop down in 'that' form, not the first. He has a table as the record source on the form, I have the following SQL embedded in the form. I think the key is the where statement, or maybe remove the where statement and use filtering. Right now all forms opened are based on the first forms data.
My SQL Where Statement -
WHERE (((tbl_buyer_column.aels_id)=[forms]![frm_baseline]![ael]))
Allen's Code
Option Compare Database
Option Explicit
Public clnClient As New Collection
Function OpenAClient()
Dim frm As Form
Set frm = New Form_frm_baseline
frm.Visible = True
frm.Caption = "New Form Opened " & Now()
clnClient.Add Item:=frm, Key:=CStr(frm.Hwnd)
Set frm = Nothing
End Function
Function CloseAllClients()
Dim lngKt As Long
Dim lngI As Long
lngKt = clnClient.Count
For lngI = 1 To lngKt
clnClient.Remove 1
Next
End Function
回答1:
You're on the right track. The trick here is to keep track of the forms as you open them too. This is how I do this. You can see when I create the new instance I pass in a parameter that specified the recordset I want to use on that form. You could change it to be a filter just as well.
This is code in a standard module
Public multiInstanceDic As Dictionary
'returns the window handle (long)
Public Function OpenNewMyFormSheetInstance(queryForRecordSource As String, Optional inputCaption As String) As Long
If multiInstanceDic Is Nothing Then
Set multiInstanceDic = New Dictionary
End If
Dim frm As Form
Set frm = New Form_MyForm
frm.Caption = inputCaption
frm.SetRecordSource queryForRecordSource
multiInstanceDic.Add frm.Hwnd, frm 'required to keep form alive after function exits
frm.SetFocus
OpenNewDynamicDataSheetInstance = frm.Hwnd
End Function
Public Function GetMyFormInstance(frmHandle As Long) As Form_MyForm
Set GetDynamicDataSheetInstance = multiInstanceDic(frmHandle)
End Function
I use it like this throughout my app
Dim createdWindowHandle As Long
createdWindowHandle = Windows.OpenNewMyFormInstance("VW_SomeView", "A wonderful informative caption")
Then whenever I need to change something else about that form that was not handled when I created it I have the hwnd
handy because I returned it to the caller when the form was created.
Dim dMyForm As Form_MySheet
Set dMyForm = Windows.GetMyFormInstance(createdWindowHandle)
dMyForm.[change any public property]
This all works pretty well for me. I can have many instances of this same form open at once all with different data. This particular form, when passed a query/recordset, will create bound controls dynamically. But, I don't think you need that. You just need to show two forms with different filters. Totally possible.
来源:https://stackoverflow.com/questions/24804738/multiple-instances-of-same-form-with-isolated-recordsets