【发布时间】:2017-10-19 10:38:16
【问题描述】:
我构建了一个函数来循环遍历表单上的所有控件并应用事件(如果它是文本框/组合框/列表框),该函数还测试控件是否是子表单并为子表单控件运行相同的函数。我遇到的问题是,如果子表单中有另一个子表单,我无法循环通过控件。
Public Function FE_LoopThroughAllControlsNumLockOn(frm As Form)
Dim ctl As control
For Each ctl In frm
If ctl.ControlType = acSubform Then
Call FE_LoopThroughAllControlsNumLockOn(frm(ctl.Name).Form) 'Error here on subform within subform
ElseIf xIsControlForEventNumLock(ctl.ControlType) = True Then
ctl.OnGotFocus = "=FM_NUM_ON()"
End If
Next ctl
Set ctl = Nothing
End Function
Function xIsControlForEventNumLock(vControlType As AcControlType) As Boolean
Select Case vControlType
Case Is = acComboBox: xIsControlForEventNumLock = True
Case Is = acListBox: xIsControlForEventNumLock = True
Case Is = acTextBox: xIsControlForEventNumLock = True
Case Else: xIsControlForEventNumLock = False
End Select
End Function
如果我尝试以下方法,它会起作用:
Debug.Print Forms!frmHR_Details!frm_HRDetails2.Form!HRSubForm2.Form!sID
但这没有,为什么?
Debug.Print Forms("frmHR_Details").Form("frm_HRDetails2").Form.Form("HRSubForm2").Form.sID.Value
或者有没有办法做到这一点:
set ctl = Eval("Forms!frmHR_Details!frm_HRDetails2.Form!HRSubForm2.Form!sID")
【问题讨论】:
-
试试
frm(ctl.Name).Form,而不是ctl.Form。 -
还是不行
-
好吧,因为子表单不是真正的表单,并且函数需要表单对象来循环其控件,所以不会工作。
标签: vba loops ms-access subforms