如何识别控件对象(例如 acTextBox)是否是 VBA 中页面集合的成员?
当控件对象获得焦点时,将调用一个函数,根据所使用的键类型将焦点移动到下一个 control.tabindex。我设法使用 Sendkeys“{TAB}” 来完成此操作,但我想更改此设置,因为此方法会不断禁用键盘上的 NUMLOCK。
到目前为止,下面的函数可以工作,但是该过程考虑了表单中的所有控件。它应该只考虑调用该函数的同一部分或页面内的控件。为了实现这一点,我需要知道事件控件是否位于页面和/或部分内,如果是,则该页面/部分的名称或索引。我找不到执行此操作的代码。
Public Function GotoNextTab()
Dim ctlNext, ctlCurrent As Control
Dim frmCurrent As Form
Dim lngNextTab As Long
Set frmCurrent = Screen.Activeform
Set ctlCurrent = Forms(frmCurrent.Name).ActiveControl
lngNextTab = Val(ctlCurrent.TabIndex) + 1
Do Until lngNextTab = frmCurrent.Controls.Count
For Each ctlNext In frmCurrent.Controls
Select Case ctlNext.ControlType
Case acCheckBox, _
acComboBox, _
acCommandButton, _
acListBox, _
acOptionButton, _
acSubform, _
acTabCtl, _
acTextBox, _
acToggleButton
If ctlNext.TabIndex = lngNextTab Then
If ctlNext.TabStop = True Then
'Make sure that the focus can be set here!
If ctlNext.Visible = True And ctlNext.Enabled = True Then
ctlNext.SetFocus
Exit Function
Else
'Focus could not be moved, so increase lngNextTab
lngNextTab = lngNextTab + 1
End If
Else
'This was the last tab, so exit
Exit Function
End If
End If
End Select
Next ctlNext
Loop
End Function
最佳答案
考虑使用TabControl.Pages()集合来 Access 特定选项卡页面上的控件。
下面的例子是:首先循环遍历每个标签页中的所有控件,找到事件控件对应的页面;然后分配当前页面并循环遍历其所有控件。按名称检查是可靠的,因为表单上带有或不带有选项卡的每个控件都必须是唯一的。
...
Dim currpage As Page
Dim tabCtrl As Control
Dim pagename As String
' FIND CURRENT PAGE OF ACTIVE CONTROL
For Each currpage In Forms!myForm!TabCtl.Pages
For Each tabctrl In currpage.Controls
If tabctrl.Name = ctlCurrent.Name
pagename = currpage.Name
End If
Next tabctrl
Next currpage
If Len(pagename) > 0 Then
' ASSIGN CURRENT TAB PAGE
Set currpage = Forms!myForm!mytabCtl.Pages(pagename)
' LOOP THROUGH ALL CONTROLS ON CURRENT PAGE
For Each tabctrl In currpage.Controls
'...PROCESS EACH CTRL OF CURRENT TAB PAGE
Next tabctrl
Set tabctrl = Nothing
Set currpage = Nothing
End If
关于ms-access - 查找 VBA 控件对象的部分或页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908599/