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>
 

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
 

Forum statistics

Threads
1,081,513
Messages
5,359,226
Members
400,521
Latest member
smarty1995

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top