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:eek:ffice:eek:ffice" /><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>
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Jolly_Nolly

Board Regular
Joined
Sep 22, 2008
Messages
136
Hi LucianoOliveira
Welcome on board.

Can you post your code so that we can assist you.

Nolly
 

LucianoOliveira

New Member
Joined
May 1, 2009
Messages
3
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
 

Jolly_Nolly

Board Regular
Joined
Sep 22, 2008
Messages
136
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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,552
Messages
5,487,523
Members
407,604
Latest member
sama9000

This Week's Hot Topics

Top