I have a Save button I programmed myself to do a load of complicated validation before saving the record. It's ALL done in code, including writing the record out to file via a recordset edit operation. (I think this is probably a mad thing to do anyway! See later.) So, I want to disable Ctrl+S, as that bypasses all my validation. Is there a way to do that?
A very pathetic question now ... I'm sure I should be able to do all my validation in a BeforeUpdate event for the form, so I can use a "Wizard's" Save button anyway, and presumably that would sort out Ctrl+S as well. BUT I can't fathom out how to use the 'Cancel' parameter in the Form_BeforeUpdate event code. It's a ByVal parameter, so it can't be set by me within the event code .... blah, blah, do I understand this even well enough to explain the problem????? How does 'it' know when to abort the save?
Your validation code should, indeed, be in the Form_BeforeUpdate event, which would solve all your problems, I suspect. When validation code is placed here, it doesn't matter how the user tries to save the record, the validation will be done.
And you can cancel the update, via code, when a validation fails, by simply using Cancel = True
If you have more than one field being validated, you need to add this line Exit Sub
so you end up with Cancel = True Exit Sub
Here's a typical multi-control validation: - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If IsNull(Me.Control1) Then
-
MsgBox "Control1 Must Not Be Left Blank!"
-
Cancel = True
-
Control1.SetFocus
-
Exit Sub
-
End If
-
If IsNull(Me.Control2) Then
-
MsgBox "Control2 Must Not Be Left Blank!"
-
Cancel = True
-
Control2.SetFocus
-
Exit Sub
-
End If
-
End Sub
-
Linq ;0)> 8 8030
Your validation code should, indeed, be in the Form_BeforeUpdate event, which would solve all your problems, I suspect. When validation code is placed here, it doesn't matter how the user tries to save the record, the validation will be done.
And you can cancel the update, via code, when a validation fails, by simply using Cancel = True
If you have more than one field being validated, you need to add this line Exit Sub
so you end up with Cancel = True Exit Sub
Here's a typical multi-control validation: - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If IsNull(Me.Control1) Then
-
MsgBox "Control1 Must Not Be Left Blank!"
-
Cancel = True
-
Control1.SetFocus
-
Exit Sub
-
End If
-
If IsNull(Me.Control2) Then
-
MsgBox "Control2 Must Not Be Left Blank!"
-
Cancel = True
-
Control2.SetFocus
-
Exit Sub
-
End If
-
End Sub
-
Linq ;0)>
Thanks, that's horribly simple! It seems very odd to me that when a sub has a ByVal parameter (what I think of as an 'in-parameter') I can reset it in the code of the sub, otherwise I guess I could have arrived at this myself. But now you've set me right ... I'm on the case. (I hated my Save code, it was a pig and a mess!)
The Form_BeforeUpdate sub does not have a ByVal parameter!
If you look at the header you'll see
Private Sub Form_BeforeUpdate(Cancel As Integer)
Access is, over all, a well thought out programing environment, of long standing. When Access does something automatically, like saving a record when the user moves off of the record or presses <Ctrl> + <S> or closes the form, it is seldom a good idea to try to redo this thru a custom button. It generally leads to problems (as you've seen) and it is annoying to experienced Access users, who generally know how Access databases are supposed to operate. And the end users are what we're all about!
Linq ;0)>
All true, and I am duly chastised. I can only quote (precisely) the following from the Access 'Help' system, which explicitly states Cancel is 'ByVal', not 'ByRef' ... as below! Is this the beginnings of an excuse for my bad behaviour?!
"BeforeUpdate Event
Occurs before data in a control is changed.
Syntax
Private Sub object_BeforeUpdate( ByVal Cancel As MSForms.ReturnBoolean)"
Read the access help like you read the bible. Its a set of guidelines, its not a set of truths. :)
Thanks to your previous help, the whole thing is much cleaned up now, and no more Ctrl+S problems! But I'm a bit puzzled ...
My new Save button for a new record needs to have some code in it to reset the form if the save is successful (but not if it isn't). So, I can't just use the 'bare save' macro the Wizard gives me. However, my code needs to know whether the BeforeUpdate routine cancelled the update. I tried assuming the ('ByRef'!!) parameter Cancel was available at form level, and wrote -
If not Cancel then
-
'go ahead and do the reset stuff
-
end if
-
But, Cancel came back false (default, I suppose) regardless of whether the BeforeUpdate sub had set it to true. So, I did the following ... -
Private Sub SaveNewButton_Click()
-
-
Dim cancelled As Integer
-
Call Form_BeforeUpdate(cancelled)
-
If Not cancelled Then
-
DoCmd.RunCommand acCmdSaveRecord
-
'now need to resort the records, and bookmark
-
'the one just added ...
-
Dim ContId As Long
-
ContId = Me.ContactID
-
Me.Requery
-
Dim rst As Recordset
-
Set rst = Me.RecordsetClone
-
rst.FindFirst "ContactID = " & ContId
-
Me.Bookmark = rst.Bookmark
-
ResetForm 'a sub call
-
'requery the organisation combo box
-
Me.Organisation.Requery
-
End If
-
-
End Sub
-
Yes, it 'works', but in fact the BeforeUpdate sub will run TWICE now. I know it does, because there was an advisory warning message in it that didn't cause Cancel/Exit Sub, and it came up twice, as I expected.
Is there any way I can access the value of Cancel set by BeforeUpdate without calling that sub with a parameter I can read on it's return?
I not 100% sure what you want to do, but im gonna guess that AfterUpdate will suit your needs.
That will run after the record has been updated, and only if the record was updated. So if you set cancel to true, the AfterUpdate wont run. You should of course use the Forms AfterUpdate and not one of the controls' afterupdate.
Yep! It's amazingly easier when you actually know what you are doing! Thanks. (By the way, from now on I will treat Access Help with even more scepticism ... I already thought it was pretty UNhelpful but hadn't actually expected it to be WRONG!)
Thank you, Smiley!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: nntp |
last post by:
I have a set of links which I want search engines to crawl them, but I want
to disable them from my visitors, so I will ask the link owners to pay me to
let me enable them.
<a disabled...
|
by: bonehead |
last post by:
Okay, here's a question I should have posted a very very long time ago.
I'd like to prevent the user from being able to bypass certain error
handling and updating functions that are programmed into...
|
by: cyshao |
last post by:
How to add a shortcut Ctrl+S ?
I want to add a new function for user to provide Ctrl+S to save things.
How can I do that?
Thanks
Charles Shao^_^
|
by: RootSpy2006 |
last post by:
Hi All,
Problem Definition:
---------------------
Microsoft Wirelss Keyboard works in BIOS but does not work when booting into
windows.
Discovered Work-around:
-----------------------------...
|
by: vanya |
last post by:
i have been tryin to program(javascript) to disable the following keystroke combinations
CTRL+O or CTRL+L Go to a new location (O = 79 L = 76)
CTRL+S Save the current page ( S = 83)
CTRL+E...
|
by: rn5a |
last post by:
In a shopping cart app, assume that a user has placed 4 orders (each
order has a corresponding OrderID which will be unique). When he comes
to MyCart.aspx, by default, the details of his last order...
|
by: Amar |
last post by:
Hi All,
I am a newbie to PHP and have the task to create a page using PHP where
in that page I need to disable all key operation as well as mouse
operation even also the menu operation that...
|
by: Michelle Anderson |
last post by:
Hi,
I have this code so it will ask user if they want to save if they click on the "close" button. But some how it also pops up the message when I click on the save button. Is there a way to...
|
by: =?Utf-8?B?TWljaGFlbCAwMw==?= |
last post by:
I need to disable the clipboard function in Windows XP. We are having a
problem with users using CTRL+C in one program, then using CTRL+V in another.
Specifically, they type their password into...
|
by: parshupooja |
last post by:
Hey all,
I have button on page. OnClick event I am saving records to database, which takes couple of seconds, meanwhile if user becomes impatient and clicks again, it triggers save again. I want...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
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 project—planning, coding, testing,...
| |