Loop through controls within multiple frames

richh

Board Regular
Joined
Jun 24, 2007
Messages
245
Office Version
  1. 365
  2. 2016
Is there any way to loop through the frames and cycle through the controls within them? I'm not as familar with frames as I thought...

I'm trying to verify that data has been entered into my controls before going forward. the loop will highlight the requred controls and exit the code if a required field is blank.

Dim f As Frame
Dim ctl As Control

'not sure about the first two lines of this code
For Each f In Me.Controls
For Each ctl In f.Controls
If ctl.Tag = "r" Then
' This is a required entry
If ctl.Value = "" Then
' Required entry is blank so set the backcolor to yellow
ctl.BackColor = vbYellow
' Capture the name of the cotrol to display to user
strMsg = strMsg & ctl.Name & vbCrLf
Else
' Required entry is populated so set the backcolor to white
ctl.BackColor = vbWhite
End If
End If
Next
Next

' Alert the user if required entries are missing
If Len(strMsg) > 0 Then
MsgBox "Please enter the required data", vbInformation, "Required Entry"
GoTo logVisit_Click_Exit
End If


Thanks anyone who can lend their assistance.
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Next time, please use code tags when posting your code. Otherwise it makes it more difficult to read through it. Try...

Code:
    Dim Ctrl1 As Control
    Dim Ctrl2 As Control
    
    'not sure about the first two lines of this code
    For Each Ctrl1 In Me.Controls
        If TypeName(Ctrl1) = "Frame" Then
            For Each Ctrl2 In Ctrl1.Controls
                If Ctrl2.Tag = "r" Then
                    ' This is a required entry
                    If Ctrl2.Value = "" Then
                        ' Required entry is blank so set the backcolor to yellow
                        Ctrl2.BackColor = vbYellow
                        ' Capture the name of the cotrol to display to user
                        strMsg = strMsg & Ctrl2.Name & vbCrLf
                    Else
                        ' Required entry is populated so set the backcolor to white
                        Ctrl2.BackColor = vbWhite
                    End If
                End If
            Next
        End If
    Next

'etc
'
'

Although, a more efficient way of doing so would be to loop through your frames using their names.

Hope this helps!
 
Last edited:
Upvote 0
You are a knight in shining +10 charsma armor. Thank you Domenic!
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top