Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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
0
GouGou
Asked:
GouGou
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now