471,601 Members | 1,226 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

How To Prevent a Form from Closing w/ Null Value

hi everybody-

i have a form with 2 fields on it that i want the user to fill out
before he or she can save the record, close the record, or move to the
next record, etc...

here's the code i tried. if i just enter minutes and click close, the
form closes. it seems to bypass my nested if statement.
Private Sub cmdExitForm_Click()

Const NO_DATE = "You did not enter a Date"
Const NO_MINUTES = "You did not enter any Minutes"

If (Me.Date = Null) Then
MsgBox NO_DATE, vbExclamation
Me.Date.SetFocus
Else
If (Me.TimeSpent = Null) Then
MsgBox NO_MINUTES, vbExclamation
Me.TimeSpent.SetFocus
End If
End If

DoCmd.Close

thanks everybody. i'm a newbie here and i truly appreciate all of the
help and advice i've received in my previous posts.
Nov 12 '05 #1
6 6388
Use the BeforeUpdate event of the *form* instead of the Click of a button.

That event will fire if the user moves record, filters or sorts the form,
etc, etc.

Your command button can then contain:
RunCommand acCmdSaveRecord
DoCmd.Close acForm, Me.Name
with the appropriate error handling to cope with case where the record
cannot be saved.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Miguelito Bain" <mi***********@hotmail.com> wrote in message
news:1b**************************@posting.google.c om...
hi everybody-

i have a form with 2 fields on it that i want the user to fill out
before he or she can save the record, close the record, or move to the
next record, etc...

here's the code i tried. if i just enter minutes and click close, the
form closes. it seems to bypass my nested if statement.
Private Sub cmdExitForm_Click()

Const NO_DATE = "You did not enter a Date"
Const NO_MINUTES = "You did not enter any Minutes"

If (Me.Date = Null) Then
MsgBox NO_DATE, vbExclamation
Me.Date.SetFocus
Else
If (Me.TimeSpent = Null) Then
MsgBox NO_MINUTES, vbExclamation
Me.TimeSpent.SetFocus
End If
End If

DoCmd.Close

thanks everybody. i'm a newbie here and i truly appreciate all of the
help and advice i've received in my previous posts.

Nov 12 '05 #2
mi***********@hotmail.com (Miguelito Bain) wrote in message news:<1b**************************@posting.google. com>...
hi everybody-

i have a form with 2 fields on it that i want the user to fill out
before he or she can save the record, close the record, or move to the
next record, etc...

here's the code i tried. if i just enter minutes and click close, the
form closes. it seems to bypass my nested if statement.
Private Sub cmdExitForm_Click()

Const NO_DATE = "You did not enter a Date"
Const NO_MINUTES = "You did not enter any Minutes"

If (Me.Date = Null) Then
MsgBox NO_DATE, vbExclamation
Me.Date.SetFocus
Else
If (Me.TimeSpent = Null) Then
MsgBox NO_MINUTES, vbExclamation
Me.TimeSpent.SetFocus
End If
End If

DoCmd.Close

thanks everybody. i'm a newbie here and i truly appreciate all of the
help and advice i've received in my previous posts.


You have to use the isnull function i.e.

If isnull(me.date) then etc

incidently you shouldn't call a date (or any field) "date", access will get confused

HTH

Neil
Nov 12 '05 #3
ne***********@boroughmuir.edin.sch.uk (NeilAnderson) wrote in message news:<83**************************@posting.google. com>...
mi***********@hotmail.com (Miguelito Bain) wrote in message news:<1b**************************@posting.google. com>...
hi everybody-

i have a form with 2 fields on it that i want the user to fill out
before he or she can save the record, close the record, or move to the
next record, etc...

here's the code i tried. if i just enter minutes and click close, the
form closes. it seems to bypass my nested if statement.
Private Sub cmdExitForm_Click()

Const NO_DATE = "You did not enter a Date"
Const NO_MINUTES = "You did not enter any Minutes"

If (Me.Date = Null) Then
MsgBox NO_DATE, vbExclamation
Me.Date.SetFocus
Else
If (Me.TimeSpent = Null) Then
MsgBox NO_MINUTES, vbExclamation
Me.TimeSpent.SetFocus
End If
End If

DoCmd.Close

thanks everybody. i'm a newbie here and i truly appreciate all of the
help and advice i've received in my previous posts.


You have to use the isnull function i.e.

If isnull(me.date) then etc

incidently you shouldn't call a date (or any field) "date", access will get confused

HTH

Neil

Thanks Neil!

The "If IsNull" worked. But after I click the OK button, the form
still closes.

I want to prevent the form from closing if either one or both of the
fields are blank.

Should I use a While Loop to test to make sure both values have been
entered in before allowing the form to close.

Is there like an opposite test to IsNull. Like if I want to check to
see if the 2 fields have valid data entered into them, then it is OK
to cloase the form. Make sense?

Thanks A LOT!
Nov 12 '05 #4
"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message news:<40***********************@freenews.iinet.net .au>...
Use the BeforeUpdate event of the *form* instead of the Click of a button.

That event will fire if the user moves record, filters or sorts the form,
etc, etc.

Your command button can then contain:
RunCommand acCmdSaveRecord
DoCmd.Close acForm, Me.Name
with the appropriate error handling to cope with case where the record
cannot be saved.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Miguelito Bain" <mi***********@hotmail.com> wrote in message
news:1b**************************@posting.google.c om...
hi everybody-

i have a form with 2 fields on it that i want the user to fill out
before he or she can save the record, close the record, or move to the
next record, etc...

here's the code i tried. if i just enter minutes and click close, the
form closes. it seems to bypass my nested if statement.
Private Sub cmdExitForm_Click()

Const NO_DATE = "You did not enter a Date"
Const NO_MINUTES = "You did not enter any Minutes"

If (Me.Date = Null) Then
MsgBox NO_DATE, vbExclamation
Me.Date.SetFocus
Else
If (Me.TimeSpent = Null) Then
MsgBox NO_MINUTES, vbExclamation
Me.TimeSpent.SetFocus
End If
End If

DoCmd.Close

thanks everybody. i'm a newbie here and i truly appreciate all of the
help and advice i've received in my previous posts.

Thanks Allen!

I truly appreciate your time and suggestion. I haven't tried it yet,
but I'm about to. I tried Neil's idea. It worked part ways. I'm going
to try your idea now.

Thanks again!

Miguelito <><
Nov 12 '05 #5
Gentlemen -- a polite request. If you are going to "bottom post", please
edit the preceding messages so we don't have to scroll through all of them
to get to the pertinent matter of the current post. See
http://www.mvps.org/access/netiquette.htm for other good suggestions on
effective use of the newsgroups.

If you feel _compelled_ to not edit the original to leave just enough to
give context to your answer, please post your response at the _top_.

Larry Linson

Nov 12 '05 #6

Your problem is that you attempt to close the form regardless of whether
the
if tests are true or false. Try:

Private Sub cmdExitForm_Click()

****Const NO_DATE = "You did not enter a Date"
****Const NO_MINUTES = "You did not enter any Minutes"
****
****If (isnull(Me.Date)) Then
********MsgBox NO_DATE, vbExclamation
********Me.Date.SetFocus
exit sub ' GET OUT
****end if

' test both possibilities - do not use else in this situation

****If (isnull(Me.TimeSpent)) Then
********MsgBox NO_MINUTES, vbExclamation
********Me.TimeSpent.SetFocus
********exit sub ' GET OUT
****End If

' if you get here, you know it is safe to close the form
****DoCmd.Close
end sub

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tom Esker | last post: by
8 posts views Thread by Mickey Swanson | last post: by
2 posts views Thread by Snuyt | last post: by
6 posts views Thread by **Developer** | last post: by
3 posts views Thread by sravan_reddy001 | last post: by
19 posts views Thread by zacks | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.