By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

How To Prevent a Form from Closing w/ Null Value

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a

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.