473,385 Members | 1,620 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,385 software developers and data experts.

Keep form from closing

I have a form that has some fields with validation rules, which are Not Is Null. I have a private function to validate records (here is a snipet)
Expand|Select|Wrap|Line Numbers
  1. Private Function ValidateRecord() As Boolean
  2. ValidateRecord = False
  3. Select Case Outcome
  4.     Case "Interpretation"
  5.         If IsNull(Me.[Date Provided]) Then
  6.             MsgBox [Date Provided].ValidationText
  7.         Exit Function
  8.         End If
  9. End Select
  10. ValidateRecord = True
  11. End Function
and a form_before update sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. If ValidateRecord = False Then
  3. Cancel = True
  4. End If
  5. End Sub
The form has a save and close button. I was wondering if there was a way to keep the form from closing if those fields are null. Right now, you click the button, the validation text pops up (it pops up twice too), and then the form closes. Can we keep the form open so they can fix the problem?

I thought about maybe having the (If...Then, Inputbox)code in the Form_BeforeUpdate instead. If I do that, is there a way to do that for date textboxes and comboboxes where you choose the value instead of typing it?

Thanks,
Jun 16 '14 #1

✓ answered by jimatqsi

Sorry, Elaine, you can use the Unload event for that purpose. I don't know if your CancelEvent code will work, but I think so.

16 1743
jimatqsi
1,271 Expert 1GB
There is an OnClose event that you can use here. Just check those objects in the OnClose event and cancel the event (cancel=true) if conditions are not right.

Jim
Jun 16 '14 #2
The OnClose event for the form doesn't have (Cancel As Integer). So do I just do

Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me.Providers) Then
  2. MsgBox "Enter a Provider!", vbOKOnly, "Error"
  3. DoCmd.CancelEvent
  4. End If
Edit: that didn't work
Jun 16 '14 #3
twinnyfo
3,653 Expert Mod 2GB
Elaine,

I think you have a 95% solution with your first post. Just add caode to the button that closes the Form.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdClose_Click() 
  2.     If ValidateRecord Then
  3.         'ValidateRecord is True
  4.         'Someone clicked the Close button without adding
  5.         'All the Required Data--Put your validatino code here
  6.  
  7.     Else
  8.         'If it is False, then we can close the Form
  9.         Docmd.Close acForm, Me.Form.Name
  10.     End If
  11. End Sub
This prevents you from even beginning the "Close Form" processes until you are absolutely sure that the required data is present.
Jun 16 '14 #4
jimatqsi
1,271 Expert 1GB
Yes, but the users can click the X on the window frame, which will not get routed through the Close button on her form.

Jim
Jun 16 '14 #5
jimatqsi
1,271 Expert 1GB
Sorry, Elaine, you can use the Unload event for that purpose. I don't know if your CancelEvent code will work, but I think so.
Jun 16 '14 #6
twinnyfo
3,653 Expert Mod 2GB
Jim, @ your Post Number 5, that's another good reason to keep that "x" close button off the form. I like to control what happens on my forms....
Jun 16 '14 #7
Thanks for the response, the Unload event worked.
Jun 16 '14 #8
jimatqsi
1,271 Expert 1GB
Yes, twinny, but it's not your form, it's Elaine's form :)
Jun 16 '14 #9
twinnyfo
3,653 Expert Mod 2GB
It's always a good time to share my tips and tricks!

:-)
Jun 16 '14 #10
NeoPa
32,556 Expert Mod 16PB
Twinny, there are ways of closing a form that don't require clicking on the red X that would confound your approach (Ctrl-F4 for instance) :-(

It is only necessary to ensure that the Form_Unload() event doesn't complete successfully unless it finds a setting that indicates whatever validation is required has already completed successfully. To do this consider the structure of the following code :
Expand|Select|Wrap|Line Numbers
  1. Private blnAllowClose As Boolean
  2.  
  3. Private Sub cmdClose_Click()
  4.     With Me
  5.         If {Validation failed} Then Exit Sub
  6.         blnAllowClose = True
  7.         Call DoCmd.Close(ObjectType:=acForm, ObjectName:=.Name, Save:=acSaveNo)
  8.     End With
  9. End Sub
  10.  
  11. Private Sub Form_Unload(Cancel As Integer)
  12.     Cancel = Not blnAllowClose
  13.     'Optional code
  14.     Call MsgBox(Prompt:="Please use the Command Button to close the form" _
  15.                 Buttons:=vbOKOnly Or vbInformation _
  16.                 Title:=Me.Name)
  17. End Sub
Jun 29 '14 #11
twinnyfo
3,653 Expert Mod 2GB
NeoPa,

So there is a keyboard shortcut to close forms?!?!?!?! ARGH! Well, I learned something new today, but now I have to make sure my folks don't learn this trick and add code to prevent it!

Thanks!

Twinnyfo
Jul 2 '14 #12
NeoPa
32,556 Expert Mod 16PB
You are certainly not the first member (or even expert) to have been caught out by this one Twinny ;-)
Jul 2 '14 #13
Here is the Code to deal with this issue.

Expand|Select|Wrap|Line Numbers
  1. If Shift = acAltMask And KeyCode = vbKeyF4 Then        'KeyDown Procedure
  2.   KeyCode = 0    'Set Form KeyPreview Property to YES
  3.  End If 
  4.  

HTH
Jul 2 '14 #14
twinnyfo
3,653 Expert Mod 2GB
Thanks, burrina!

Another tool for my kit!
Jul 2 '14 #15
Your Very Welcome. FYI, for 2010 it is different.

Expand|Select|Wrap|Line Numbers
  1. 'Disable Ctrl-F4 Hot Key in Access 2010.
  2. Select Case KeyCode
  3.  
  4.         Case vbKeyF4
  5.             'MsgBox "The F2 key was Pressed"
  6.             KeyCode = 0
  7.         Case Else
  8.             'MsgBox "No match!" 'testing
  9.     End Select
Jul 3 '14 #16
NeoPa
32,556 Expert Mod 16PB
Hi Burrina.

While your code may be useful as illustrating how keys can be captured (When the properties of the form are set up correctly), I would remind readers that the point is not so much to handle individual exceptions, but more to handle the concept as a whole. This is done perfectly well using the Form_Unload() event procedure as already suggested by Jim. Even if you were to capture all available ways of closing a form (And handling Ctrl-F4 is by no means that.) then you could never guarantee that new ones couldn't be introduced in future.

Thank you for your offerings anyway. As I say, it illustrates what code can do. It's certainly worth including how a form should be configured in order to allow keystrokes to be captured though.
Jul 3 '14 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: news.microsoft.com | last post by:
Hi there, I was just curious if anyone knew how to create a .Net event that is similar to Form.Closing event, where in I can set the Cancel property of the event argument to true and it will...
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
3
by: Gavin | last post by:
Hi All I have a project that has 2 forms, the main form displays various sql data items and the second form is for editing / updating 1 particular table. I would like to refresh a dataset on...
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...
3
by: Bob | last post by:
I haver a user login form (winforms app using vs2005 in VB.NET). After succesfull validayion of user I want to open a first form and close the loging form that was used, If I write If...
2
by: billkirim | last post by:
I ve got a problem with opera browser. When i am closing a form tag </form> which is in a div tag seems to automatic close my div tag. <div id="something"> <form .......> ->first form ...
2
by: polocar | last post by:
Hi, suppose that you have a C# form with two buttons, that are the classical "btnOk" and "btnCancel" (besides them, of course in the form there can be many other controls). When the user clicks...
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...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
1
by: Aleksey Timonin | last post by:
Hi guys, I show my Form in dialog mode. So I have "Ok" button on it with DialogResult = Ok. What is the right way and how can I abort form closing from the button click event. Thanks a lot...
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.