Run Sub using keyboard shortcut (1 Viewer)

jeffreyccs

New member
Local time
Today, 06:48
Joined
Nov 10, 2012
Messages
29
Hi

In Excel it is very straightforward to execute a sub from a shortcut but in Access as far as I can see it is only possible to run a macro from a shortcut and from a macro only possible to execute a function rather than a sub.
As using a button is not practical I would like to find out if there is any other way to execute VBA code.

Any assistance would be much appreciated

Regards

Jeff
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:48
Joined
May 7, 2009
Messages
19,233
create a macro called AutoKeys macro.
you can assign a selected keyboards combination and call a function.
you can google it, like "ms access autokeys macro"

EDIT:

you can only Call Public Subs/Functions.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:48
Joined
Feb 19, 2013
Messages
16,607
There should be no problem restating your sub as a function, the function does not need to return anything

function myFunction()
'do something
end function

as opposed to

function mySub()
'do something
end sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:48
Joined
Oct 29, 2018
Messages
21,455
Hi Jeff. Just curious, what does your Sub do and when do you need to run it? You don't need a macro or a button, if you use a Form to run it.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:48
Joined
Jul 9, 2003
Messages
16,271
The original version of the Northwind Sample Database provided by Microsoft contains a nice contact detail form which has a set of A to Z buttons, and these buttons are accessible by the keyboard shortcut key combination Alt/Shift plus the underscored letter in the the command button caption. You can download either/both versions of the northwind database, the old version and the new version here:- https://www.niftyaccess.com/northwind-downloads

In the following video I demonstrate how the keyboard shortcut key combination works. With this solution, you would have the Best of Both Worlds, a key combination and a Command Button!


Assigning Keyboard Shortcuts to a Button - Nifty Access
 

Isaac

Lifelong Learner
Local time
Today, 06:48
Joined
Mar 14, 2017
Messages
8,774
You can also use the form's properties key preview = Yes, and then code in the form's on key press event.
 

isladogs

MVP / VIP
Local time
Today, 14:48
Joined
Jan 14, 2017
Messages
18,209
You can also use the form's properties key preview = Yes, and then code in the form's on key press event.

Doing that would be a nightmare. Every time the user types any key anywhere on the form, the code would run...repeatedly,
Much better to use the ampersand before a selected letter on the command button caption e.g. &Run as a keyboard shortcut
 

Isaac

Lifelong Learner
Local time
Today, 06:48
Joined
Mar 14, 2017
Messages
8,774
Doing that would be a nightmare. Every time the user types any key anywhere on the form, the code would run...repeatedly,
Much better to use the ampersand before a selected letter on the command button caption e.g. &Run as a keyboard shortcut
The way you described it, I agree, but I thought that that type of code could pick up more complex/intelligently designed combinations of keys, no? I honestly am asking...as I've rarely used that method. I always thought you could pick up one specific combination by checking the keyascii that got passed and if not exit sub. If that's correct, then that doesn't sound like a nightmare at all. If I'm wrong, I won't be surprised, as I've not gotten into that much at all.
I agree the button with the underlined Alt method, is awesome.
 

isladogs

MVP / VIP
Local time
Today, 14:48
Joined
Jan 14, 2017
Messages
18,209
Yes you could use the key press method to respond to a specific keystroke only e.g. Ctrl+Shift+Q.
Indeed I use that approach in kiosk style applications
However its overkill for this purpose in my opinion.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:48
Joined
Oct 29, 2018
Messages
21,455
My question is, why would the OP consider using a button "not practical?"
 

Micron

AWF VIP
Local time
Today, 09:48
Joined
Oct 20, 2018
Messages
3,478
My question is, why would the OP consider using a button "not practical?"
Had to wonder the same thing as I read all the suggestions on how to do it with something that wasn't wanted. Yet a button provides a "no code" solution and the option to click on it or not. Then I suspected perhaps there is no form, just a need to run a routine at some point. The post ratio here is OP 1 to us 10 before me. Maybe it's jeffreyccs' turn?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:48
Joined
Jul 9, 2003
Messages
16,271
I don't know what the op means by shortcut in Excel.

It occurred to me that the op might be thinking in terms of opening MS Access from the command line with a shortcut to run a particular function, which is an entirely different matter!
 

jeffreyccs

New member
Local time
Today, 06:48
Joined
Nov 10, 2012
Messages
29
Hi

Thanks for all the replies

Unfortunately I was unwell and could not reply earlier.

I will work through everything and let you know how I get on.

Many thanks

Regards

Jeff
 

Users who are viewing this thread

Top Bottom