473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 6561
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Morgan | last post by:
In an SQL statement which concatenates several fields I get a null value returned if any one of the fields are null. Is this to be expected? For example : SELECT tblMember.memberAddress + '...
4
by: Tom Esker | last post by:
I've got Javascript in a form that adds up all of the numbers in a column of form fields and displays a total. It works great if every field has an initial value of 0, but if any of them are null,...
4
by: deko | last post by:
I have a pop-up form in which user-defined parameters are entered. The parameters are entered into a subform datasheet which is bound to a table. I need to check the product of two parameters to...
8
by: Mickey Swanson | last post by:
Is there any way to determing why a form is closing? As in if the user cliced the x button or if a system shutdown has started. thanks for any help Mickey Swanson
2
by: Snuyt | last post by:
Hallo, when a form is closed by clicking the closing button (cross in the right upper corner of the form), it is also disposed. Can I prevent this, and instead of closing (and disposing) the...
6
by: **Developer** | last post by:
I've been looking but can't find out how in a form Closing event to know if the closing is because the form's "X" had been clicked or the main form's "X" was clicked. That is, I need to know if...
7
by: bjose21 | last post by:
Hello, I've been searching the web for quite some time to resolve the problem of "1/1/1900" returning in a datetime field in SQL that resulted from a blank (not NULL) value being passed to it...
1
by: David Shorthouse | last post by:
Hey folks, I am attempting to pass null as the input value from a series of textboxes if the user does not input a value prior to submit. To try and do this, I am using a vbscript function on...
3
by: sravan_reddy001 | last post by:
i want to prevent a form from closing.. to do this i want to handle the formClosing or FormClosed events. from here i want to prevent the form from closing. New instance of same form should...
19
by: zacks | last post by:
I have a .NET 2.0 MDI application where the child form has a Tab Control. Each of the Tab in the Tab Control has a Validating event to handle what it should do when the user changes tabs. But...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.