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) - Private Function ValidateRecord() As Boolean
-
ValidateRecord = False
-
Select Case Outcome
-
Case "Interpretation"
-
If IsNull(Me.[Date Provided]) Then
-
MsgBox [Date Provided].ValidationText
-
Exit Function
-
End If
-
End Select
-
ValidateRecord = True
-
End Function
and a form_before update sub - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If ValidateRecord = False Then
-
Cancel = True
-
End If
-
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,
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
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
The OnClose event for the form doesn't have (Cancel As Integer). So do I just do -
If IsNull(Me.Providers) Then
-
MsgBox "Enter a Provider!", vbOKOnly, "Error"
-
DoCmd.CancelEvent
-
End If
Edit: that didn't work
Elaine,
I think you have a 95% solution with your first post. Just add caode to the button that closes the Form. - Private Sub cmdClose_Click()
-
If ValidateRecord Then
-
'ValidateRecord is True
-
'Someone clicked the Close button without adding
-
'All the Required Data--Put your validatino code here
-
-
Else
-
'If it is False, then we can close the Form
-
Docmd.Close acForm, Me.Form.Name
-
End If
-
End Sub
This prevents you from even beginning the "Close Form" processes until you are absolutely sure that the required data is present.
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
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.
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....
Thanks for the response, the Unload event worked.
Yes, twinny, but it's not your form, it's Elaine's form :)
It's always a good time to share my tips and tricks!
:-)
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 : - Private blnAllowClose As Boolean
-
-
Private Sub cmdClose_Click()
-
With Me
-
If {Validation failed} Then Exit Sub
-
blnAllowClose = True
-
Call DoCmd.Close(ObjectType:=acForm, ObjectName:=.Name, Save:=acSaveNo)
-
End With
-
End Sub
-
-
Private Sub Form_Unload(Cancel As Integer)
-
Cancel = Not blnAllowClose
-
'Optional code
-
Call MsgBox(Prompt:="Please use the Command Button to close the form" _
-
Buttons:=vbOKOnly Or vbInformation _
-
Title:=Me.Name)
-
End Sub
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
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 ;-)
Here is the Code to deal with this issue. -
If Shift = acAltMask And KeyCode = vbKeyF4 Then 'KeyDown Procedure
-
KeyCode = 0 'Set Form KeyPreview Property to YES
-
End If
-
HTH
Thanks, burrina!
Another tool for my kit!
Your Very Welcome. FYI, for 2010 it is different. -
'Disable Ctrl-F4 Hot Key in Access 2010.
-
Select Case KeyCode
-
-
Case vbKeyF4
-
'MsgBox "The F2 key was Pressed"
-
KeyCode = 0
-
Case Else
-
'MsgBox "No match!" 'testing
-
End Select
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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?...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |