473,769 Members | 5,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_BeforeUpdat e(Cancel As Integer)
If BadValue Then DoCmd.CancelEve nt
End Sub

Private Sub Site_BeforeUpda te(Cancel As Integer)
If BadValue Then DoCmd.CancelEve nt
End Sub

The DoCmd.CancelEve nt 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 5874
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_BeforeUpdat e(Cancel As Integer)
If BadValue Then DoCmd.CancelEve nt
End Sub

Private Sub Site_BeforeUpda te(Cancel As Integer)
If BadValue Then DoCmd.CancelEve nt
End Sub

The DoCmd.CancelEve nt 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(Can cel 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.RunComman d acCmdSaveRecord
Set db = CurrentDb
Set rst = db.OpenRecordse t("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
6588
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 tried. if i just enter minutes and click close, the form closes. it seems to bypass my nested if statement.
22
7581
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 otherwise it cancels the close. However, the user can still right click on the task bar and close my application. Is there any way of preventing this. Thanks Alex
7
7897
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
410
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 don't know what I'm talking about take a look at MS CRM. Open a form, make some changes and try to close the form without saving first. Instead of closing the form you will be asked if you'd like to save first or discard the changes. I have often...
9
2284
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) as a popup using either window.open or window.showModalDialog. Since I want to warn the users of my application when they try to close page1.aspx and have changed the values in the meantime I thought about using the “onbeforeunload” event of the...
2
2333
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 form, just hide the form? (it is not the main form) thanks, Snuyt
2
3530
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 List" which causes "Program not responding"...press "Cancel" and no issues, press "End now" and sure enough it ends. My question is; is there any method to prevent the "Application not responding" message from appearing? Current code:
4
3133
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 cancels the close event if the BeforeUpdate code fires '4/16/06 see...
3
13059
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 not be used. is there any way to do this? is it posiible to do the same thing with anycode?
7
5238
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 click. also when designing, going to design mode saves the record. So I tried: OnClose : Me.Undo. My own cancel button uses it and it works ok,
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.