473,242 Members | 2,405 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,242 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 6548
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.