In which form section is my button? (1 Viewer)

John Sh

Member
Local time
Today, 22:22
Joined
Feb 8, 2021
Messages
410
My form has a detail section and a header section with command buttons in both sections.
How do I determine in which section is a particular button?
It seems "Forms.section.name" is the required syntax but I cannot get it to return anything except an error message, " object does not support..."
 

Edgar_

Active member
Local time
Today, 07:22
Joined
Jul 8, 2023
Messages
430
Your controls have a .Section property.
MsgBox MyCommand.Section will return an integer that corresponds to the section it's in:
0acDetailForm detail section
1acHeaderForm header section
2acFooterForm footer section
3acPageHeaderForm page header section
4acPageFooterForm page footer section

I cited the docs because I needed the enum names but this can be found if you dig in your debugger. Took a few seconds.
 
Last edited:

John Sh

Member
Local time
Today, 22:22
Joined
Feb 8, 2021
Messages
410
Your controls have a .Section property.
MsgBox MyCommand.Section will return an integer that corresponds to the section it's in:
0acDetailForm detail section
1acHeaderForm header section
2acFooterForm footer section
3acPageHeaderForm page header section
4acPageFooterForm page footer section

I cited the docs because I needed the enum names but this can be found if you dig your debugger. Took a few seconds.
Thank you Edgar. I can get back to some coding now. I was being thrown by the "Form.section.property"
When it sort of is and it sort of isn't a "Form" property.
John
 

Edgar_

Active member
Local time
Today, 07:22
Joined
Jul 8, 2023
Messages
430
It appears that forms don't have a .Sections collection, only a .Section property, but since we know it's 5 sections from the docs cited previously, we can loop like this:
Code:
Private Sub Form_Load()
    On Error Resume Next
    Dim i As Long
    Dim ctrl As Object
    Dim msg As String
    For i = 0 To 4
        msg = _
        msg & vbCrLf & _
        "-----------" & _
        "Section " & i & _
        " (" & Me.Section(i).Name & ")" & _
        "-----------"
        For Each ctrl In Me.Section(i).Controls
            msg = msg & vbCrLf & ctrl.Name
        Next ctrl
    Next i
    MsgBox msg
End Sub

I added the On Error Resume Next to avoid getting an error if it does not find some section, it can be handled in other ways, but this is just for proof.

EDIT: modified the code to show a message box instead of a debug.print
 
Last edited:

John Sh

Member
Local time
Today, 22:22
Joined
Feb 8, 2021
Messages
410
It appears that forms don't have a .Sections collection, only a .Section property,
Thank you again.
I am writing an interactive help module which relies on the previous button / control when the "Help" button is clicked.
I only have form header and detail sections so am using the following code.
Code:
Dim ctl As Control
    Dim nSect As Integer
    Dim sSect As String
    Set ctl = Screen.PreviousControl
    nSect = ctl.Section
    sSect = IIf(nSect = 0, "Detail", "Header")

Once I know where the button is I can link it to the correct bookmark in the help document.
I could get rid of nsect and put ctl.section into the iif(
Many thanks for your input.
John
 

LarryE

Active member
Local time
Today, 05:22
Joined
Aug 18, 2021
Messages
591
Here is a Public function that opens each loaded form in design mode, determines which controls in the Detail section and Header section are command buttons and displays the form name and control name:
Code:
Public Function LoopFormControls()
Dim CurrentForm As Form
Dim Obj As Object
Dim Dbs As Object
Set Dbs = CurrentProject
Dim ActiveCntrl As Control
For Each Obj In Dbs.AllForms
    If Obj.IsLoaded Then
        DoCmd.OpenForm Obj.Name, acDesign
        Set CurrentForm = Screen.ActiveForm
        For Each ActiveCntrl In CurrentForm.Section(0).Controls 'Detail Section
            If ActiveCntrl.ControlType = acCommandButton Then
                MsgBox CurrentForm.Name & "   " & ActiveCntrl.Name
            End If
        Next
        For Each ActiveCntrl In CurrentForm.Section(1).Controls 'Header Section
            If ActiveCntrl.ControlType = acCommandButton Then
                MsgBox CurrentForm.Name & "   " & ActiveCntrl.Name
            End If
        Next
    End If
Next
DoCmd.OpenForm Screen.ActiveForm.Name, acNormal
Exit Function
End Function
 

Users who are viewing this thread

Top Bottom