[mso] Re: Access 2002: Checking For Nulls On Form Close

  • From: "Jim Pettit" <j_e_pettit@xxxxxxxxxxx>
  • To: <mso@xxxxxxxxxxxxx>
  • Date: Thu, 10 Jun 2004 06:48:31 -0700

Cathy--

Since you don't want to make the bound table fields do the work, your
best bet is to simply check the value of the fields before leaving the
form. Your best bet is to attach the code to the form's BeforeUpdate
event. An example follows:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Len(Me.txtRequiredFieldOne & "") = 0 Then
        MsgBox "Please fill in Field One", vbOKOnly + vbExclamation,
"Can't Do That!"
        Me.txtRequiredFieldOne
        Cancel = True
        Me.Undo
        Exit Sub
    End If
    If Len(Me.txtRequiredFieldTwo & "") = 0 Then
        MsgBox "Please fill in Field Two", vbOKOnly + vbExclamation,
"Can't Do That!"
        Me.txtRequiredFieldTwo
        Cancel = True
        Me.Undo
        Exit Sub
    End If
    If Len(Me.txtRequiredFieldThree & "") = 0 Then
        MsgBox "Please fill in Field Three", vbOKOnly + vbExclamation,
"Can't Do That!"
        Me.txtRequiredFieldThree
        Cancel = True
        Me.Undo
    End If
End Sub

My example will work with a few fields, but if you've a whole bunch of
fields to check, my code would get unwieldy and difficult to maintain;
in such a case, you'd probably want to do something fancier. Let us know
if you need more examples.

This may be splitting hairs, but I just want to make sure you're
covered: you stated, "...if 3 of the fields are null", rather than, "if
any of the 3 fields are null". If by that you mean that you'd only want
to prevent the form from closing if all 3 of the fields are null, your
code could look like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Len(Me.txtRequiredFieldOne & "") + Len(Me.txtRequiredFieldTwo &
"") + Len(Me.txtRequiredFieldThree & "") = 0 Then
        MsgBox "Please fill in the fields", vbOKOnly + vbExclamation,
"Can't Do That!"
        Me.txtRequiredFieldOne
        Cancel = True
        Me.Undo
    End If
End Sub

Out of curiosity, what 'other problems' would be caused by setting the
underlying table fields to not allow nulls?

--Jim

-----Original Message-----
From: mso-bounce@xxxxxxxxxxxxx [mailto:mso-bounce@xxxxxxxxxxxxx] On
Behalf Of Cathy.Evans@xxxxxxxxx
Sent: Thursday, June 10, 2004 6:19 AM
To: mso@xxxxxxxxxxxxx
Subject: [mso] Access 2002: Checking For Nulls On Form Close



Thought I'd recently seen some code to check for null values on form
close, but not finding.  I'm needing to do a check on form close that if
3 of the fields are null, a message will pop up telling user which
field(s) need to be completed before closing.  The form should not close
till the data criteria has been met.  Can't set the table field to
required, that option caused other problems in my form, so I'm thinking
the next option is to check the certain fields for null values before
closing form.  Any ideas? Thank you, Cathy




------------------------------------------------------------------------
-----------------------------
The information transmitted is intended only for the person
or entity to which it is addressed and may contain confidential and/or
privileged material. If you are not the intended recipient 
of this message you are hereby notified that any use, review,
retransmission, dissemination, distribution, reproduction or any action
taken in reliance upon this message is prohibited. If you 
received this in error, please contact the sender and delete the 
material from any computer.  Any views expressed in this message are
those of the individual sender and may not necessarily reflect 
the views of the company.  
------------------------------------------------------------------------
-------------------------------

*************************************************************
You are receiving this mail because you subscribed to mso@xxxxxxxxxxxxx
or MicrosoftOffice@xxxxxxxxxxxxxxxx

To send mail to the group, simply address it to mso@xxxxxxxxxxxxx

To Unsubscribe from this group, send an email to 
mso-request@xxxxxxxxxxxxx with the word "unsubscribe" (without the
quotes) in the subject line.

Or, visit the group's homepage and use the dropdown menu.  This will
also allow you to change your email settings to digest or vacation (no
mail). //www.freelists.org/webpage/mso

To be able to use the files section for sharing files with the group,
send a request to mso-moderators@xxxxxxxxxxxxx and you will be sent an
invitation with instructions.  Once you are a member of the files group,
you can go here to upload/download files:
http://www.smartgroups.com/vault/msofiles
*************************************************************
*************************************************************
You are receiving this mail because you subscribed to mso@xxxxxxxxxxxxx or 
MicrosoftOffice@xxxxxxxxxxxxxxxx

To send mail to the group, simply address it to mso@xxxxxxxxxxxxx

To Unsubscribe from this group, send an email to 
mso-request@xxxxxxxxxxxxx with the word "unsubscribe" (without the quotes) in 
the subject line.

Or, visit the group's homepage and use the dropdown menu.  This will also allow 
you to change your email settings to digest or vacation (no mail).
//www.freelists.org/webpage/mso

To be able to use the files section for sharing files with the group, send a 
request to mso-moderators@xxxxxxxxxxxxx and you will be sent an invitation with 
instructions.  Once you are a member of the files group, you can go here to 
upload/download files:
http://www.smartgroups.com/vault/msofiles
*************************************************************

Other related posts: