Results 1 to 7 of 7
  1. #1
    userman is offline Novice
    Windows 11 Access 2019
    Join Date
    Mar 2025
    Posts
    1

    how to 'for each ctl (as control)' in me!sufformcontrolname.form

    dear everyone,
    please tell me code that is collect for 'for each ctl' in subform

    this procedure is error,I have tried various fixes, but none have worked. I want to enumerate the control names within the subform included in the main form.

    Public Sub RobustSubformControlProcessing()
    Dim ctl As Control
    Dim frm As Form
    Dim subFormCtl As Control
    Dim subForm As Form
    Dim strSubFormName As String

    ' Check if main form is open
    On Error Resume Next
    Set frm = Forms!MainForm
    On Error GoTo 0

    If frm Is Nothing Then
    MsgBox "Main form is not open.", vbExclamation
    Exit Sub
    End If

    ' Check if subform control exists
    On Error Resume Next
    Set subFormCtl = frm.Controls("SubformControlName")
    On Error GoTo 0

    If subFormCtl Is Nothing Then


    MsgBox "Subform control not found.", vbExclamation
    Exit Sub
    End If

    ' Get the source object name of the subform control
    On Error Resume Next
    strSubFormName = subFormCtl.SourceObject
    On Error GoTo 0

    If Len(strSubFormName) = 0 Then
    MsgBox "The source object of the subform is not set.", vbExclamation
    Exit Sub
    End If

    ' Try multiple approaches to get the form object
    On Error Resume Next
    Set subForm = subFormCtl.Form

    If subForm Is Nothing Then
    ' Try alternative method
    On Error Resume Next
    Set subForm = Forms(strSubFormName)
    On Error GoTo 0
    End If
    On Error GoTo 0

    If subForm Is Nothing Then
    MsgBox "Subform is not properly loaded.", vbExclamation
    Exit Sub
    End If

    ' Process all controls in the subform
    For Each ctl In subForm.Controls
    Debug.Print ctl.Name
    ' Add your control processing logic here
    Next ctl
    End Sub

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    I struggle to read your code due to lack of indentation. Please use the code tags (highlight code and click the # button)

    but to answer your question to which yo already have the answer to loop through controls in a form

    Code:
    dim frm as form
    dim ctl as control
    set frm =sufformcontrolname.form
    
    for Each ctl in frm
        ‘Do something
    next ctl

  3. #3
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    you also try this, make sure you replace the subform name on the code:
    Code:
    Public Sub RobustSubformControlProcessing()
        Dim ctl As Control
        Dim frm As Form
        Dim subFormCtl As Control
        Dim strSubFormName As String
        
        ' put the correct subform name here
        strSubFormName = "Form1"
        
        ' Check if main form is open
        On Error Resume Next
        Set frm = Forms!MainForm
        
        If Err Then
            MsgBox "Main form is not open.", vbExclamation
            Exit Sub
        End If
        
        ' Check if subform control exists
        Set subFormCtl = frm.Controls(strSubFormName)
        
        If Err Then
            MsgBox "Subform control not found.", vbExclamation
            Exit Sub
        End If
        
        If Not (TypeOf subFormCtl Is Access.subForm) Then
            MsgBox strSubFormName & " is not a subform.", vbExclamation
            Exit Sub
        End If
        
        ' Process all controls in the subform
        For Each ctl In subFormCtl.Controls
            Debug.Print ctl.Name
            ' Add your control processing logic here
        Next ctl
    End Sub

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Presenting a message box if the main form is not open seems a bit odd to me. With respect to a regular user, the only way I can see that being the case is if you allow them access to the nav pane, which you should not be doing. If the db admin is modifying/testing the subform that's different as he/she should have access to the nav pane and might wish to open the subform for design/testing. In that case I know I would not want to be pestered with a message. I'd just exit code if it meant that failing to do so would start raising error messages.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Code:
        Dim ctl As Control
        Dim ctl2 As Control
        For Each ctl In Me.Controls
            Debug.Print ctl.Name
            If ctl.ControlType = 112 Then
                For Each ctl2 In Controls(ctl.Name).Controls
                    Debug.Print ctl2.Name & "*"
                  
                Next
            End If
    
        Next
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I find TypeName(ctl) is easier to work with, as opposed to having to remember or look up the numbers for the various controls.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Quote Originally Posted by Micron View Post
    I find TypeName(ctl) is easier to work with, as opposed to having to remember or look up the numbers for the various controls.
    I agree but was in a hurry to get out of the office.

    I just ran this to get the number and then modified it as posted.

    Code:
    For Each ctl In Me.Controls
            Debug.Print ctl.Name ; ctl.Controltype
    Next
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Ctl + A does not work on text field
    By adnancanada in forum Forms
    Replies: 2
    Last Post: 07-11-2017, 01:58 PM
  2. Getting the max and min from ctl.ItemData(varItem)
    By Thompyt in forum Programming
    Replies: 1
    Last Post: 02-25-2017, 09:54 AM
  3. Replies: 4
    Last Post: 02-18-2016, 12:06 PM
  4. using ctl
    By slimjen in forum Forms
    Replies: 5
    Last Post: 06-24-2014, 08:21 AM
  5. Hide each control, control group or subform?
    By BRV in forum Programming
    Replies: 2
    Last Post: 12-09-2011, 09:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums