Message Box not returning focus to the right field. (1 Viewer)

Lkwdmntr

Registered User.
Local time
Today, 09:08
Joined
Jul 10, 2019
Messages
277
Hello Again,

I am doing some test on my project, that's why I have a few posts going. With this one. I am trying to make sure the user puts in all the required fields on my form. For each field that is required, I have this code.
Private Sub FirstName_LostFocus()

If IsNull(Me.FirstName) Then
MsgBox "You must enter a Name."
Me.FirstName.SetFocus
Else
Me.MiddleInitial.SetFocus
End If

End Sub​

I realize that a person could just click into any field and miss the event, so I have several statements in the Save Button code that checks all the required fields.

If IsNull(Me.FirstName) Then
MsgBox "You must enter a Name.", vbOKOnly
Me.FirstName.SetFocus
Else
End If

When I tab over the field, it does show the message, but it doesn't set the focus back on the field.

This is probably an easy fix. I haven't tested the Save button yet, but if anyone foresees an issue, let me know, please.

Thanks
 

missinglinq

AWF VIP
Local time
Today, 12:08
Joined
Jun 20, 2003
Messages
6,423
There are a number of ways to approach this, all variations of Paul's suggested method, such as

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

 If Nz(Me.Control1,"") = "" Then
   MsgBox "Control1 Must Not Be Left Blank!"
   Cancel = True
   Control1.SetFocus
   [COLOR="red"]Exit Sub[/COLOR]
 End If
 
If Nz(Me.Control2, "") = "" Then
   MsgBox "Control2 Must Not Be Left Blank!"
   Cancel = True
   Control2.SetFocus
   [COLOR="red"]Exit Sub[/COLOR]
 End If

End Sub
You could loop through some or all Controls and do the same thing. If the number of Controls makes the above too time consuming, given your situation, this will loop through all Textboxes and all Comboboxes and check that they're populated

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim ctl As Control
Dim CName As String


For Each ctl In Me.Controls
    Select Case ctl.ControlType
        Case acTextBox, acComboBox
            If Nz(ctl, "") = "" Then
              CName = ctl.Controls(0).Caption
              MsgBox "Following field is required: " & vbCrLf & vbCrLf & CName
              Cancel = True
              ctl.SetFocus
              [COLOR="red"]Exit Sub[/COLOR]
             End If
    End Select
Next ctl

End Sub
You could also use the Tag Property to mark certain Controls, and then loop through all Controls but only check on/address the status of these 'marked' Controls.

To set the Tag Property for multiple Controls, all at once:
  1. Go into Form Design View
  2. Holding down <Shift> and Left clicking on each Control in turn.
  3. Go to Properties – Other and enter Marked in the Tag Property (just like that, no Quotation Marks)
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim ctl As Control
Dim CName As String


For Each ctl In Me.Controls
  If ctl.Tag = "marked" Then
     If Nz(ctl, "") = "" Then
       CName = ctl.Controls(0).Caption
       MsgBox "Following field is required: " & vbCrLf & vbCrLf & CName
       Cancel = True
       ctl.SetFocus
       [COLOR="red"]Exit Sub[/COLOR]
     End If
   End If
Next ctl

End Sub

Notice than any time you're validating more than one Control...you need to add the Exit Sub command...otherwise execution will simply 'fall thru' from one Control to the next, not allowing the user to correct a blank Control.

Also, note that doing Validation on a single Control can only be done to insure that the data entered is of the correct type…i.e. numeric, if appropriate, consists of a certain number of characters, etc. It must then be used in the Control’s BeforeUpdate event, in order for Focus to remain on the errant Control if the validation fails.

Linq ;0)>
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 16:08
Joined
Feb 19, 2013
Messages
16,553
if IsNull(Me.FirstName) Then

it may be that firstname is not null but a zls (zero length string)

try

if nz(Me.FirstName,"")="" Then

edit: beaten to it by linq
 

Users who are viewing this thread

Top Bottom