简体   繁体   English

如何检查是否在ms Access中打开了子窗体

[英]how to check if subform is opened in ms access

I have a main form MYMAIN with two subforms in it MYSUBONE and MYSUBTWO. 我有一个主表单MYMAIN,其中有两个子表单MYSUBONE和MYSUBTWO。

I have "on current" events in each subform that update textbox in the other subform. 我在每个子窗体中都有“当前”事件,这些事件会更新另一个子窗体中的文本框。

My problem arises when the forms are being loaded. 表单加载时出现我的问题。 The "on current" event is triggered when the subform "MYSUBONE" is loaded (BEFORE "MYSUBTWO" is loaded) and it tries to update a textbox in MYSUBTWO which is still not yet loaded. 加载子窗体“ MYSUBONE”(加载“ MYSUBTWO”之前)时,将触发“ on current”事件,并尝试更新仍未加载的MYSUBTWO中的文本框。 So error is triggered in the event procedure. 因此,在事件过程中会触发错误。

How do I check in my "on current" event procedure (in VBA?) for MYSUBONE to check if the MYSUBTWO subform is not yet loaded. 如何为MYSUBONE签入“当前”事件过程(在VBA中?)以检查MYSUBTWO子表单是否尚未加载。

on-current-mysubone if mysubtwo is not loaded then update mysubtwo.textbox = ... end if on-current-mysubone如果未加载mysubtwo,则更新mysubtwo.textbox = ...如果结束

I tried the "Isloaded" function in the sample database "Northwind" but doesn't seem to work. 我在示例数据库“ Northwind”中尝试了“已加载”功能,但似乎不起作用。 how do i check if subform is not yet loaded? 我如何检查子窗体是否尚未加载?

Or could I just ignore error and use something like "if error, exit function"? 还是我可以忽略错误并使用“如果出错,退出功能”之类的东西?

One possible way to solve this problem would be to just ensure you know what order the subforms are loaded in. You can accomplish this by unbinding the subform controls and then manually loading them. 解决此问题的一种可能方法是仅确保您知道子表单的加载顺序。您可以通过解除绑定子表单控件然后手动加载来实现此目的。 Here is how to do it: 这是操作方法:

  1. Add your subform controls to the parent form as normal. 像往常一样将您的子窗体控件添加到父窗体。
  2. Make sure you have your link fields, etc. set up how you want them. 确保您具有链接字段,等等。设置所需的方式。
  3. In the subform properties>data tab, delete the value in the "Source Object" Field. 在子窗体属性>“数据”选项卡中,删除“源对象”字段中的值。

Add VBA to manually bind the controls when the parent form opens: 添加VBA以在父窗体打开时手动绑定控件:

Private Sub Form_Open(Cancel As Integer)
    Me.sfB.SourceObject = "FormB"
    Me.sfA.SourceObject = "FormA"
End Sub

Don't know if it is yet useful, but for what's worth... I'm displaying two grids with parent-child relationship. 不知道它是否仍然有用,但是对于什么值得...我正在显示两个具有父子关系的网格。 However, second grid displays at once all child records associated to all parent records in first grid. 但是,第二个网格立即显示与第一个网格中的所有父记录关联的所有子记录。 When one record is selected in first grid, I want to locate first corresponding child record in the second grid. 当在第一个网格中选择一个记录时,我想在第二个网格中找到第一个对应的子记录。 I faced the same trouble as reference to second grid's (subform's) recordset fails when initially opening main form, as second grid has not yet been displayed. 我遇到了同样的麻烦,因为在最初打开主表单时,由于未显示第二个网格,因此引用第二个网格的(子表单的)记录集失败。 My workaround consisted on catching error 2455 and ignoring it, so it gives chance to continue until second grid is fully displayed. 我的解决方法包括捕获错误2455并忽略它,因此它提供了继续进行操作直到完全显示第二个网格的机会。

Private Sub Form_Current()
Dim rsResults As Recordset
Dim stFilter As String

On Error GoTo ErrHdlr

'Locate first results record associated to current requisition
Set rsResults = Me.Parent.sfResults.Form.Recordset
stFilter = obRecord.GetFilter(Me.Recordset, vrPKFields, vrPKTypes)
rsResults.FindFirst (stFilter)

Exit Sub

ErrHdlr:
If Err.Number = 2455 Then
   Exit Sub 'When this sub form is first displayed, Current event
               'tries to reference another subform on the same parent.
               'However, such second subform is not yet open and
               'reference fails. This is the error number raised,
               'so it is ignored on purpose. After second subform
               'is displayed, code works correctly.
               'It had to be handled this way because there's no way
               'to know in advance whether subform is already open.
Else
   msgbox Err.number & " " & err.description
endif
exit sub

Subforms are actually opened before the main form is opened (including all their OnCurrent events). 子窗体实际上是在打开主窗体之前打开的(包括其所有OnCurrent事件)。 I don't think you can guarantee which order they are loaded in either. 我认为您无法保证它们以哪种顺序加载。

I'd turn the problem around and do the update from the main form. 我会解决问题,并从主表单进行更新。 If you really have to do the update from the Subform, move the update to a separate function in MySubOne . 如果确实需要从MySubOne进行更新,请将更新移至MySubOne的单独函数。 Then in the main form's OnCurrent, call the function in MySubOne . 然后在主窗体的OnCurrent,调用该函数MySubOne This will guarantee that both MySubOne and MySubTwo are already loaded. 这将确保MySubOneMySubTwo已被加载。

“是否有办法检查检索到的子表单记录计数是否为0”

if forms!myMainForm!mySubForm.form.RecordsetClone.RecordCount = 0 then....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM