Please ammend this code

Form1 contains some textboxes and comboboxes which their Tag are "T" .
in the following code i want to show a warning msgbox when i press return key if any of these controls = ""


Private Sub Form_KeyPress(KeyAscii As Integer)
Dim Ctrl As Control

If KeyAscii = 13 Then
For Each Ctrl In Controls
    If Ctrl.Tag = "T" Then
        If Ctrl = "" Then
             MsgBox "Fill this control"
        Else
            SendKeys "{Tab}"
        End If
    End If
Next Ctrl
End If

End Sub

in this code I receive many msgboxes but I need only one msgbox and after i click OK the focus stay in this blank control to fill it.

Thanx
GouGouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bob_onlineCommented:
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim Ctrl As Control

If KeyAscii = 13 Then
For Each Ctrl In Controls
   If Ctrl.Tag = "T" Then
       If Ctrl = "" Then
            MsgBox "Fill this control"
            exit sub
       Else
           SendKeys "{Tab}"
       End If
   End If
Next Ctrl
End If

End Sub

0
DocMCommented:
Dim Ctrl As Control

If KeyAscii = 13 Then

For Each Ctrl In Controls
   If Ctrl.Tag = "T" Then
       If Ctrl = "" Then
            MsgBox "Fill this control"
       Else
           SendKeys "{Tab}"
           Exit For
       End If
   End If
Next Ctrl
End If
0
bob_onlineCommented:
I don't think you want the sendkeys "{Tab}" but am not entirely sure what you're doing.  There probably is no good reason to tab through all your controls while testing this out.  You can probably, before the exit sub I added, do ctrl.setfocus
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JacamarCommented:
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim Ctrl As Control

If KeyAscii = 13 Then
For Each Ctrl In Controls
   If Ctrl.Tag = "T" Then
       If Ctrl = "" Then
            MsgBox "Fill this control"
            Exit for
       Else
           SendKeys "{Tab}"
       End If
   End If
Next Ctrl
End If

End Sub
0
supunrCommented:
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim Ctrl As Control

If KeyAscii = 13 Then
For Each Ctrl In Controls
   If Ctrl.Tag = "T" Then
       If Ctrl = "" Then
            MsgBox "Fill this control"
            Ctrl.SetFocus
            Exit sub
       Else
           SendKeys "{Tab}"
       End If
   End If
Next Ctrl
End If

End Sub
0
DocMCommented:
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim Ctrl As Control

If KeyAscii = 13 Then
For Each Ctrl In Controls
  If Ctrl.Tag = "T" Then
      If trim(Ctrl) = "" Then
           MsgBox "Fill this control"
           Ctrl.SetFocus
           Exit sub
      Else
          SendKeys "{Tab}"
      End If
  End If
Next Ctrl
End If

End Sub
0
toni2002Commented:
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim ctrl As Control

For Each ctrl In Controls
    If KeyAscii = 13 Then
        If ctrl.Tag = "T" Then
           If ActiveControl.Text = "" Then
                MsgBox "Fill This Control"
                KeyAscii = 0
                Exit Sub
            Else
                SendKeys "{Tab}"
                Exit For
            End If
        End If
    End If
Next

End Sub

you must define which control you are in because u use Tag for many controls
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DocMCommented:
The code of Toni2002 do not check all the controls with
the Tag set to "T".
It only check for the active control, even if its tag is not set to "T".
If ActiveControl.Text = "" Then
               MsgBox "Fill This Control"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.