Setfocus is not working after a looping through the textbox

LucianoOliveira

New Member
Joined
May 1, 2009
Messages
3
I have an "Input data" form and all I'd like to do was to add a code so when the user press the button Ok it'd check each texbox and if it's empty show a error message and setfocus back to the empty textbox.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I'm a new vba user, however I managed to check the textboxes using a looping through the controls "For Each cCount...", but for any reason the cCount.Setfocus doesn't work.<o:p></o:p>
<o:p></o:p>
Please, any help would be greatly appreciated.<o:p></o:p>
<o:p></o:p>
Cheers<o:p></o:p>
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi Nolly, thanks for your quick repply

This is the part of the code that I was refering:

For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" Then
If cCont = vbNullString Then
MsgBox "Please you still have empty fields"
cCont.SetFocus
'Cancel = True
End If
End If
Next cCont

I hope you can help me
Thanks
 
Upvote 0
Your code is fine except it keeps looking through all the text boxes and showing the message box for each empty text box before reaching the end of the code.

Just add this one line and it will stop at the first empty textbox.

Code:
For Each cCont In Me.Controls
    If TypeName(cCont) = "TextBox" Then
        If cCont = vbNullString Then
        MsgBox "Please you still have empty fields"
        cCont.SetFocus
        Exit Sub [COLOR=SeaGreen]'##Add this line##[/COLOR]
        End If
    End If
Next cCont
I hope this is what you needed.
Nolly
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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