473,387 Members | 1,711 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 Form from closing?

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 make sure they are valid
before updating them (or closing the popup form), so I use this code:

Private Function BadValue() As Boolean
If Me!Die * Me!Site > 32000 Or Me!Die * Me!Site < 0 Then
MsgBox "Die and/or Site out of range." & vbCrLf & vbCrLf & _
"The product of Die and Site must be" & vbCrLf & _
"between 0 and 32,000 for any given Job File.", vbCritical, _
" Invalid Die or Site Value"
BadValue = True
Else
BadValue = False
End If
End Sub

Private Sub Die_BeforeUpdate(Cancel As Integer)
If BadValue Then DoCmd.CancelEvent
End Sub

Private Sub Site_BeforeUpdate(Cancel As Integer)
If BadValue Then DoCmd.CancelEvent
End Sub

The DoCmd.CancelEvent seems to work - the value is not updated if BadValue
returns True.

But the problem is users generally enter a value and then immediately click
the close cross in the upper right corner of the popup form. This results
in an error message:

"You can't save this record at this time."

Is there any way to avoid this error? I really don't want to create my own
"Close" command button, if I can avoid it. I would rather continue to use
the standard close box to let users close the popup form. Can the form be
prevented from trying to close if BadValue returns True?

Thanks in advance.
Nov 13 '05 #1
4 5818
jv
In the Form's Unload event, you can put
cancel=BadValue

deko wrote:
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 make sure they are valid
before updating them (or closing the popup form), so I use this code:

Private Function BadValue() As Boolean
If Me!Die * Me!Site > 32000 Or Me!Die * Me!Site < 0 Then
MsgBox "Die and/or Site out of range." & vbCrLf & vbCrLf & _
"The product of Die and Site must be" & vbCrLf & _
"between 0 and 32,000 for any given Job File.", vbCritical, _
" Invalid Die or Site Value"
BadValue = True
Else
BadValue = False
End If
End Sub

Private Sub Die_BeforeUpdate(Cancel As Integer)
If BadValue Then DoCmd.CancelEvent
End Sub

Private Sub Site_BeforeUpdate(Cancel As Integer)
If BadValue Then DoCmd.CancelEvent
End Sub

The DoCmd.CancelEvent seems to work - the value is not updated if BadValue
returns True.

But the problem is users generally enter a value and then immediately click
the close cross in the upper right corner of the popup form. This results
in an error message:

"You can't save this record at this time."

Is there any way to avoid this error? I really don't want to create my own
"Close" command button, if I can avoid it. I would rather continue to use
the standard close box to let users close the popup form. Can the form be
prevented from trying to close if BadValue returns True?

Thanks in advance.


Nov 13 '05 #2
> In the Form's Unload event, you can put
cancel=BadValue


I made the BadValue function public and tried that in both the subform
datasheet and the popup form itself - no luck.
Nov 13 '05 #3
jv
The unload event is the place to conditionally prevent form from
closing. You'll need to step through the code (using breakpoints) to
see why it isn't working and the to play with the code.

Nov 13 '05 #4
> The unload event is the place to conditionally prevent form from
closing.


With that assurance, I continued to troubleshoot. I took the code out of
the subform. Rather than using the Before_Update event on the subform
datasheet, I just test every set of values when the user attempts to close
the parent form. The table is small enough so it takes very little time.

Thanks for your help.

Private Sub Form_Unload(Cancel As Integer)
If BadValue Then Cancel = True
End Sub

Private Function BadValue() As Boolean
Dim lngDie As Long
Dim lngSite As Long
Dim db As DAO.Database
Dim rst As DAO.Recordset
DoCmd.RunCommand acCmdSaveRecord
Set db = CurrentDb
Set rst = db.OpenRecordset("tblPattern")
Do While Not rst.EOF
lngDie = rst!Die
lngSite = rst!Site
If lngDie * lngSite > 32000 Or lngDie * lngSite < 0 Then
MsgBox "Die and/or Site out of range for " & rst!JobFileName & _
vbCrLf & vbCrLf & "The product of Die and Site must be" & _
vbCrLf & "between 0 and 32,000 for any given Job File.", _
vbCritical, " Invalid Die or Site Value"
BadValue = True
Exit Do
Else
BadValue = False
End If
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set db = Nothing
End Function
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Miguelito Bain | last post by:
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...
22
by: alecjames1 | last post by:
I have a form which the user must complete before closing. I have disabled the window x button and use my own exit button. When selected it checks to see if the user has completed the entries...
7
by: Nikki | last post by:
Hi, Can anybody help me, i want to prevent windows to close my winform of ..NET application, when user presses Alt+F4
4
by: Daniel Walzenbach | last post by:
Hi, does anybody know the JavaScript the guys at Microsoft used in MS CRM to prevent people from closing a browser window and asking them (on the client) what they really want to do? If you...
9
by: Daniel Walzenbach | last post by:
Hi I am faced with the following problem: I have a page (let’s call this page page1.aspx) containing some TextBoxes and a hyperlink which opens another page (let’s call this page page2.aspx)...
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...
2
by: kevininstructor | last post by:
I want to prevent a user from closing an application while doing critical operations. The following code (concept came from MSDN) works except for when the user attempts to terminate via "Task...
4
by: tlyczko | last post by:
I have been looking on the NGs and I found this code to show if a subform has no records. I have this code in the MAIN form OnClose event: Private Sub btnClose_Click() '4/16/06 new code that...
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...
7
by: Michiel Rapati-Kekkonen | last post by:
Hi, I would like my record to be saved only when my own close button (with all it's checks) is used. I made the winodws close button disappear, but you still can close using the right mouse...
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: 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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.