ms-access - 查找 VBA 控件对象的部分或页面

标签 ms-access vba

如何识别控件对象(例如 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/

相关文章:

excel - VBA 正在绕过我的 for 循环而不执行它

excel - 我在管理器中命名的范围的类型不匹配

ms-access - “Id”不是此表中的索引 - MS ACCESS

c++ - CDaoDatabase断言错误

database - 如何通过表单向Ms Access中的表添加新记录?

java - 如何使用 Java 从 MS ACCESS 数据库检索列描述

excel - 关闭源文件时,无法在 for 循环中重用内容

vba - 使用 VBA excel 多个类名从网站提取数据

sql - 对于 SQL 中最后一个表中的每个元素,如何返回一个表中实体的值,该值小于但最接近另一个表中的值?

vba - 在VB中连接字符串和整数