Using Keyboard keys as condition in 2010 macro (not vba) (1 Viewer)

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
Hello Everyone.
I have an idea and i want to do something like this in form's BeforeUpdate Event:

If {Ctrl+S Pressed} then
RunMenuCommand
Command SaveRecord

Else
If MsgBox("Do you want to save the form?",4)=6 then
RunMenuCommand
Command SaveRecord

Else
RunMenuCommand
Command UndoRecord

End if
End if

The actual thing i wanna do is to save the form only by pressing the Ctrl+S shortcut key and in any other case, a prompt be shown to prevent auto save.
but i dont know what is the right condition for "If {Ctrl+S Pressed} then"
is there any condition for using keyboard like this at all in new macro mode of Access ?!

Thank you so much.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,169
on design view of your form, on Property Sheet->Event->Key Preview=Yes.
then add code to On Key Down and Before Update events:
Code:
Option Compare Database
Option Explicit

Dim bolSave As Boolean

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Not bolSave Then
        If MsgBox("do you want to save?", vbYesNo) = vbNo Then
            Cancel = True
        End If
    End If
    bolSave = False
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 83 And Shift = 2 Then
        bolSave = True
        KeyCode = 0
        DoCmd.RunCommand acCmdSaveRecord
    End If
End Sub
 

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
Thank You very much arnelgp
First of all i'm a newcomer to Access,i start it with 2019 office version and i found out that Access has replaced vba and code writing with new macro action options.
in my version of access, in front of events, there is space and after that a down arrow button which if click on it, a switch shows with [Event Procedure] written on it, and next to arrow, a ... button which if click on it, a window will show up with "Choose Builder" title and three choices of "Macro Builder" , "Expression Builder" and "Code Builder", and if I click on Code Builder I get this error: "Microsoft Access failed to create the Visual Basic Module", so till now I am working with Macro Builder all the time.
so I have learned many thing of how to work with Macro Builder, whether just hanging around the app or searching the forums, but this time I came to a dead end.

So, I Pasted did changed the key preview to yes, and I did paste the code to the space in front of On Key Down & Before Update Event, and on pressing any key of keyboard I got another long error "MS Access cannot find the object "Option Compare Database Option Explicit..."
So I don't know where should I put this code exactly
 

Attachments

  • 001.jpg
    001.jpg
    44.9 KB · Views: 148
  • 002.jpg
    002.jpg
    84.1 KB · Views: 142

isladogs

MVP / VIP
Local time
Today, 09:17
Joined
Jan 14, 2017
Messages
18,186
The last post was moderated due to the attached files. Until you have 10 posts, please zip any attachments to avoid this issue.

Posting this to trigger email notifications

FYI macros are more powerful than in the past but definitely haven't replaced VBA.
To use code as your default, click Always use event procedures in Options...Object Designers...Form/Report design view
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,169
no, remove what you typed on Keydown Event.
after that go to VBA (Alt-F11).
delete all the code and paste the one I gave you.
 

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
Many thanks arnelgp
I did so, but nothing happens I'm afraid :( no message shown on closing the form after changing records.
 

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
no, remove what you typed on Keydown Event.
after that go to VBA (Alt-F11).
delete all the code and paste the one I gave you.

ok I remember You told me to add the code to On Key Down & Before Update, how should I do that ? the codes are now just saved as a Module in navigation pane
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,169
check this demo.
the codes are now just saved as a Module in navigation pane
why is that?
 

Attachments

  • testCTR-S.zip
    38.6 KB · Views: 121

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
this is it, I guess there's something wrong with My app !!
when i pressed Alt+F11, the VBA window popped up, but there were no code window within, so I clicked on insert module and thus it saved as a module at the end.
in Your demo, when I click on a blank event's "..." button and choose the code Builder from the "Choose Builder" window, the VBA window Pops up again, but in My app, when I do so, I get the error i mentioned: "MS Access failed to create the Visual Basic module. if your database is on a network drive, check your network connection, and then try again."
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,169
so you need to rectify that first.
possible un-install/re-install office.
i start it with 2019 office version and i found out that Access has replaced vba and code writing with new macro action options.
I am using A2019, so nothing has been replaced.
 

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
check this demo.

why is that?

All Right. I found it out finally!
my mistake was that i named my tables and forms in another language :/
I fixed it, and now I get this one: "The expression On Key Down you entered as the event property setting produced the following error: A problem occurred while microsoft accesss was communicating with the OLE server or ActiveX control.
*The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
*There may have been an error evaluating the function, event, or macro."
 

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
Sorry to bother You dear, but this solved totally!
this error too was because of field's non-english name!
I really really appreciate your help and taking time and you caused me learn a lot !
I'm so thankful for You.
 

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
Oops you know what ? after I the code worked, I found out this is not what i wanted :D:D
I wanted if I press Ctrl+S, no YesNo message box be shown and the form be saved, but in other situations such as closing the form, the message box come along.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,169
checked my demo again, it doesn't ask question when Ctrl-S is pressed.
 

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
checked my demo again, it doesn't ask question when Ctrl-S is pressed.

You are quiet right, and quiet perfect!
that must be some differences that i will find and correct.
Many Thanks Again !
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,169
happy code hunting!
 

IMyself

Registered User.
Local time
Today, 13:47
Joined
Oct 15, 2019
Messages
30
happy code hunting!

You know what I did again?
I made a save button which does the same job as Ctrl-S, and I added this part to your code and it worked too:
Code:
Private Sub SaveComm_Click()
bolSave = True
so now I'm a newcomer to vba too thanks to You! and I'm ambitious to learn more!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,169
so now I'm a newcomer to vba too thanks to You! and I'm ambitious to learn more!
so whatever you learn, don't keep it in the brain.. learn it by heart.
 

Users who are viewing this thread

Top Bottom