SetFocus on UserForm not working as expected

snjpverma

Well-known Member
Joined
Oct 2, 2008
Messages
1,584
Office Version
  1. 365
Platform
  1. Windows
User FORM:
1583236822401.png


I am using the below code to validate that none of the controls on my UserForm are blank when I click on Add to List.
However, I want the cursor to be on the 1st blank textbox. So, ideally in above scenario since all txtbox are blank, the 1st one i.e. Film Name should be the set Focus.
Currently my cursor gets placed on the 3rd Txtbox i.e. "Release date".
I fail to understand the reason behind it. In the Tab Order I have set the order properly as shown in below picture.
What exactly determines the order of CTL in below line of code?
VBA Code:
For Each ctl In Filmframe.Controls

Tab Order Screenshot:

1583237031643.png


Full Fucntion Code:
Rich (BB code):
Private Function everythingfilledin() As Boolean

Dim ctl As MSForms.Control
Dim anythingmissing As Boolean

everythingfilledin = True
anythingmissing = False

    For Each ctl In Filmframe.Controls
        If TypeOf ctl Is MSForms.TextBox Or TypeOf ctl Is MSForms.ComboBox Then
            If ctl.Value = "" Then
            ctl.BackColor = rgbPink
            Controls(ctl.Name & "Label").ForeColor = rgbRed
            If Not anythingmissing Then ctl.SetFocus
            anythingmissing = True
            everythingfilledin = False
            End If
        End If
    Next ctl
End Function
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi,
see if these changes to your code will do what you want

VBA Code:
Private Function everythingfilledin() As Boolean
    Dim ctl As MSForms.Control
    
    For Each ctl In Filmframe.Controls
        If TypeOf ctl Is MSForms.TextBox Or TypeOf ctl Is MSForms.ComboBox Then
            everythingfilledin = CBool(Len(ctl.Value) > 0)
            If Not everythingfilledin Then
                ctl.BackColor = rgbPink
                ctl.SetFocus
                Exit Function
            Else
                ctl.BackColor = rgbWhite
            End If
        End If
    Next ctl
End Function

Dave
 
Upvote 0
Hi,
see if these changes to your code will do what you want

VBA Code:
Private Function everythingfilledin() As Boolean
    Dim ctl As MSForms.Control
   
    For Each ctl In Filmframe.Controls
        If TypeOf ctl Is MSForms.TextBox Or TypeOf ctl Is MSForms.ComboBox Then
            everythingfilledin = CBool(Len(ctl.Value) > 0)
            If Not everythingfilledin Then
                ctl.BackColor = rgbPink
                ctl.SetFocus
                Exit Function
            Else
                ctl.BackColor = rgbWhite
            End If
        End If
    Next ctl
End Function

Dave
Still going to the 3rd textbox.
 
Upvote 0
Focus should be set on the control that is empty & code exited
& works ok on dummy form I just created

If you can, place copy of your workbook in a dropbox or if unable to do this, post all your forms code which may give forum more of an idea what is going on

Dave
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,981
Members
448,934
Latest member
audette89

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