MSGBOX when a form field has been left empty.

rks062148

New member
Local time
Today, 12:06
Joined
Oct 19, 2012
Messages
9
There has been a bit of discussion regarding the following --

A text field on my main form must be tested that it has not been left blank. If it was, then a msgbox must be displayed notifying this fact, and returning the focus to this control. Here is my solution.

On my main form is a subform. As soon as the tab order moves my cursor into the subform, the main Form is tested for its completion - the Form BeforeUpdate function in the Form Properties.

I do not test at the control's BeforeUpdate level because once you loose focus of your text field it is difficult to get the cursor to backup to the field in question.

Here is my code - -
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Len(Me.Owner & vbNullString) = 0 Then
MsgBox "You must enter the owner's name.", vbCritical, "Missing Owner's name!"
Cancel = True
Me.Owner.SetFocus
End If
End Sub

This is not a complex code solution but the problem that posters are having seems to be getting the focus back onto the offending control.
 
Brilliant ! :D

Now you only need to clearly indicate whether this is your free cure for a common problem, or a call for help, because the cure does n ot work and "the posters" still "are having" a problem?
 
How about something that can handle more than one required field.

Put a Tag of "required"on each field's control that is required and loop through them and highlight those that aren't:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    On Error Resume Next
    Dim ctl As Control
    For Each ctl in Me.Controls
        With ctl
            If .Tag = "required" Then
                If Nz(.Value,"") = "" Then
                    .BackColor = RGB(255,255,191)
                    If Not Cancel Then .SetFocus
                    Cancel = True
                End If
            End If
        End With
    Next ctl
    If Cancel Then MsgBox "Your changes cannot be saved until the fields highlighted have been filled in.", vbCritical, "Missing Data!"
End Sub

And set the backcolors back to normal in the form's current event and perhaps also in each controls after update event.

But this post will quickly be lost to the midsts of time so why did I bother?
 

Users who are viewing this thread

Back
Top Bottom