473,396 Members | 2,011 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,396 software developers and data experts.

How can I disable Ctrl+S so user has to use MY save button?

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?
Jan 27 '10 #1

✓ answered by missinglinq

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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  If IsNull(Me.Control1) Then
  3.    MsgBox "Control1 Must Not Be Left Blank!"
  4.    Cancel = True
  5.    Control1.SetFocus
  6.    Exit Sub
  7.  End If
  8.  If IsNull(Me.Control2) Then
  9.    MsgBox "Control2 Must Not Be Left Blank!"
  10.    Cancel = True
  11.    Control2.SetFocus
  12.    Exit Sub
  13.  End If
  14. End Sub
  15.  

Linq ;0)>

8 8030
missinglinq
3,532 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  If IsNull(Me.Control1) Then
  3.    MsgBox "Control1 Must Not Be Left Blank!"
  4.    Cancel = True
  5.    Control1.SetFocus
  6.    Exit Sub
  7.  End If
  8.  If IsNull(Me.Control2) Then
  9.    MsgBox "Control2 Must Not Be Left Blank!"
  10.    Cancel = True
  11.    Control2.SetFocus
  12.    Exit Sub
  13.  End If
  14. End Sub
  15.  

Linq ;0)>
Jan 27 '10 #2
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!)
Jan 27 '10 #3
missinglinq
3,532 Expert 2GB
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)>
Jan 27 '10 #4
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)"
Jan 28 '10 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Read the access help like you read the bible. Its a set of guidelines, its not a set of truths. :)
Jan 28 '10 #6
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

Expand|Select|Wrap|Line Numbers
  1. If not Cancel then
  2.      'go ahead and do the reset stuff
  3. end if
  4.  
But, Cancel came back false (default, I suppose) regardless of whether the BeforeUpdate sub had set it to true. So, I did the following ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub SaveNewButton_Click()
  2.  
  3.     Dim cancelled As Integer
  4.     Call Form_BeforeUpdate(cancelled)
  5.     If Not cancelled Then
  6.         DoCmd.RunCommand acCmdSaveRecord
  7.           'now need to resort the records, and bookmark
  8.           'the one just added ...  
  9.         Dim ContId As Long
  10.         ContId = Me.ContactID
  11.         Me.Requery
  12.         Dim rst As Recordset
  13.         Set rst = Me.RecordsetClone
  14.         rst.FindFirst "ContactID = " & ContId
  15.         Me.Bookmark = rst.Bookmark        
  16.         ResetForm     'a sub call
  17.           'requery the organisation combo box
  18.         Me.Organisation.Requery
  19.    End If
  20.  
  21. End Sub
  22.  
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?
Jan 28 '10 #7
TheSmileyCoder
2,322 Expert Mod 2GB
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.
Jan 28 '10 #8
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!
Jan 28 '10 #9

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

Similar topics

6
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...
3
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...
3
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^_^
2
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: -----------------------------...
3
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...
2
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...
14
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...
3
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...
6
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...
3
parshupooja
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...
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: 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: 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:
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...
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,...
0
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...
0
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 project—planning, coding, testing,...

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.