具有独立记录集的相同形式的多个实例 [英] Multiple Instances of same form with isolated recordsets

查看:21
本文介绍了具有独立记录集的相同形式的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://allenbrowne.com/ser-35.html

下面的代码是 Allen Browne 的代码,用于打开同一表单的多个实例.问题是我需要每个表单数据都基于那个"表单中的下拉菜单,而不是第一个.他有一个表作为表单上的记录源,我在表单中嵌入了以下SQL.我认为关键是 where 语句,或者删除 where 语句并使用过滤.现在所有打开的表单都是基于第一个表单数据.

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.

我的 SQL Where 语句 -

My SQL Where Statement -

WHERE (((tbl_buyer_column.aels_id)=[forms]![frm_baseline]![ael]))

WHERE (((tbl_buyer_column.aels_id)=[forms]![frm_baseline]![ael]))

艾伦的密码

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

推荐答案

您走对了.这里的诀窍是在打开表单时跟踪它们.这就是我这样做的方式.您可以看到,当我创建新实例时,我传入了一个参数,该参数指定了我想在该表单上使用的记录集.您也可以将其更改为过滤器.

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.

这是标准模块中的代码

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")

然后,每当我需要更改创建表单时未处理的其他内容时,我都会使用 hwnd,因为我在创建表单时将其返回给调用者.

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.

这篇关于具有独立记录集的相同形式的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆