Making sure form fields are filled in or error (1 Viewer)

hardhitter06

Registered User.
Local time
Yesterday, 19:50
Joined
Dec 21, 2006
Messages
600
Hello,

I have this code executed when I hit my Save button. It works fine but I just added an additional field SFSID that isn't required each time. If I leave it blank, I still get the error message and can't save my record. I'm not sure how I would exclude one field with my given code. Could some one assist? Thank you.

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctrl As Control

For Each ctrl In Me.Controls
 If ctrl.ControlType = acTextBox Then
  If IsNull(ctrl) Then
    MsgBox ctrl.Name & " Cannot Be Left Empty!"
    Cancel = True
    ctrl.SetFocus
    Exit Sub
  End If
  End If
Next
 MsgBox "Record Saved!"
 End Sub
 

Taruz

Registered User.
Local time
Today, 00:50
Joined
Apr 10, 2009
Messages
168
Hi.. ;)

Try this..:

Code:
Dim ctrl As Control

For Each ctrl In Me.Controls
 If ctrl.ControlType = acTextBox [COLOR="Red"][I]and ctrl.name <> "SFSID"[/I][/COLOR] Then
  If IsNull(ctrl) Then
    MsgBox ctrl.Name & " Cannot Be Left Empty!"
    Cancel = True
    ctrl.SetFocus
    Exit Sub
  End If
  End If
Next
 MsgBox "Record Saved!"
 

pr2-eugin

Super Moderator
Local time
Today, 00:50
Joined
Nov 30, 2011
Messages
8,494
I would just add one IF in the code and skip withouht any messages.. like
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctrl As Control

For Each ctrl In Me.Controls
    If ctrl.ControlType = acTextBox Then
        If IsNull(ctrl) Then
            If ctrl.Name="Name_of_the_additional_field" Then
                'do nothing
            Else
                MsgBox ctrl.Name & " Cannot Be Left Empty!"
                Cancel = True
                ctrl.SetFocus
                Exit Sub
            End If
        End If
    End If
Next
MsgBox "Record Saved!"
End Sub
A simple solution.. in my eyes..

EDIT: Well Taruz, has a better solution with just a small change unlike me.. ;)
 

Users who are viewing this thread

Top Bottom