2017-07-04 3 views
1

コントロールオブジェクト(acTextBoxなど)がVBAのページコレクションのメンバーであるかどうかを特定するにはどうすればよいですか?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 
+0

フォームに関する*ページコレクション*とはどういう意味ですか?この[Access 2003の機能](https://msdn.microsoft.com/en-us/library/office/aa210669(v = office.11​​).aspx)を意味しますか?この興味深いフォームのスクリーンショットを共有できる場合そして、この関数はどこで呼び出されていますか?イベントをトリガするイベント(OnOpen、OnClick、AfterUpdate) – Parfait

+0

私はv2010で働いていますが、それはまさに私が意味することです。コントロールは、タブコントロールオブジェクトの異なるページにあります。 Tabindexの値の範囲は、タブページ内のコントロールの数に制限されています(そうでなければ、単に連続的に番号を付けるだけです)。だからこそ私はコントロールがあるページ(Pages(Index))を知りたいのですが、 – Art

答えて

1

特定のタブページ上のコントロールにアクセスするためにTabControl.Pages()コレクションを使用することを検討してください。

以下に例を示します。まず、各タブページ内のすべてのコントロールを介して、アクティブコントロールループの対応するページを見つけます。現在のページを割り当て、すべてのコントロールをループします。フォーム上のタブの有無にかかわらず、すべてのコントロールが一意でなければならないので、名前によるチェックは信頼できます。

... 
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 
0

あなたの答えをありがとう、これは正しい方向に私を助けました。私は、親プロパティが常に1つのレベル(この場合はPageオブジェクト)であるコントロールを参照していることを発見しました。最後のコントロールに達したときに、親コントロールコレクション内で最初に利用可能なコントロールに戻る機能を挿入する必要があります。これまでのところ私が望んでいた結果は得られなかったので、私は今のようにこの手順に固執するでしょう。

Private Sub Form_Load() 
    KeyPreview = True 
End Sub 

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) 
    Dim ctl As Control 
    If KeyCode = vbKeyReturn Then 
     'Disregard textboxes with multiple lines 
     Set ctl = Me.ActiveControl 
     If ctl.ControlType = acTextBox Then 
      If ctl.EnterKeyBehavior Then 
       Exit Sub 
      End If 
     End If 
     GotoNextTab 
    End If 
End Sub 

Public Function GotoNextTab() 
' This function will move the focus to the next control if 
' a) This control is a specified type 
' b) The control is visible and enabled 
' c) The control has a tabstop set to true 
' 
' If the controls are within a page (TabControl), the focus is cycled within the controls on that page. 
' The function disregards sections as it is assumed that there is no need to cycle controls on a form header or footer 
' The focus does not cycle within subforms. 

On Error Resume Next 

    Dim ctlNext as Control 
    Dim ctlCurrent As Control 
    Dim frmCurrent As Form 
    Dim lngNextTab As Long 
    Set frmCurrent = Screen.Activeform 
    Set ctlCurrent = Forms(frmCurrent.Name).ActiveControl 
    If ctlCurrent.Parent.ControlType = acOptionGroup Then 
     Set ctlCurrent = ctlCurrent.Parent 
    End If 
    lngNextTab = ctlCurrent.TabIndex 

    Do Until lngNextTab > ctlCurrent.Parent.Controls.Count 
     lngNextTab = lngNextTab + 1 
     For Each ctlNext In ctlCurrent.Parent.Controls 
      Select Case ctlNext.ControlType 
       Case acCheckBox, _ 
        acComboBox, _ 
        acCommandButton, _ 
        acListBox, _ 
        acSubform, _ 
        acTabCtl, _ 
        acTextBox, _ 
        acToggleButton, _ 
        acOptionGroup 
         If ctlNext.TabIndex = lngNextTab Then 
          'Make sure that the focus can be set here! 
          If ctlNext.TabStop = True And ctlNext.Visible = True And ctlNext.Enabled = True Then 
           ctlNext.SetFocus 
           Exit Function 
          Else 
           Exit For 'restart for/next and search the next value 
          End If 
         End If 
      End Select 
     Next ctlNext 
    Loop 
End Function 
関連する問題