Solved Run a module from a command button (1 Viewer)

mistermarty

Member
Local time
Today, 18:25
Joined
Oct 31, 2020
Messages
41
Evening all. I have a module in my DB to disable and enable the shift startup override function. At the moment, I have to type apDisableShift or apEnableShift into the immediate window to turn the function on and off. I have placed an "on" and "off" command button on a form that only I have access to, but I don't know how to get each button to link to the relevant parts of the module. Can anyone point me in the right direction?

As always, please explain it as though you were talking to a young child, as my knowledge is basic to say the least! 🤣

Here's the code behind my module called modDisableShift

Code:
Function ap_DisableShift()
'This function disable the shift at startup. This action causes
'the Autoexec macro and Startup properties to always be executed.

On Error GoTo errDisableShift

Dim db As DAO.Database
Dim prop As DAO.Property
Const conPropNotFound = 3270

Set db = CurrentDb()

'This next line disables the shift key on startup.
db.Properties("AllowByPassKey") = False

'The function is successful.
Exit Function

errDisableShift:
'The first part of this error routine creates the "AllowByPassKey
'property if it does not exist.
If Err = conPropNotFound Then
Set prop = db.CreateProperty("AllowByPassKey", _
dbBoolean, False)
db.Properties.Append prop
Resume Next
Else
MsgBox "Function 'ap_DisableShift' did not complete successfully."
Exit Function
End If

End Function

Function ap_EnableShift()
'This function enables the SHIFT key at startup. This action causes
'the Autoexec macro and the Startup properties to be bypassed
'if the user holds down the SHIFT key when the user opens the database.

On Error GoTo errEnableShift

Dim db As DAO.Database
Dim prop As DAO.Property
Const conPropNotFound = 3270

Set db = CurrentDb()

'This next line of code disables the SHIFT key on startup.
db.Properties("AllowByPassKey") = True

'function successful
Exit Function

errEnableShift:
'The first part of this error routine creates the "AllowByPassKey
'property if it does not exist.
If Err = conPropNotFound Then
Set prop = db.CreateProperty("AllowByPassKey", _
dbBoolean, True)
db.Properties.Append prop
Resume Next
Else
MsgBox "Function 'ap_DisableShift' did not complete successfully."
Exit Function
End If

End Function

Thanks
 

Cronk

Registered User.
Local time
Tomorrow, 03:25
Joined
Jul 4, 2013
Messages
2,772
If your On/off buttons were called cmdOn and cmdOff, then go to the properties of each button and in the onClick event, select the elipses, select Code builder and then add the code so it reads
Code:
private sub cmdOn_click()
   ap_EnableShift
end sub

private sub cmdOff_click()
   ap_ap_DisableShift
end sub
 

mistermarty

Member
Local time
Today, 18:25
Joined
Oct 31, 2020
Messages
41
Should just be able to call the function from the button's click event?

If your On/off buttons were called cmdOn and cmdOff, then go to the properties of each button and in the onClick event, select the elipses, select Code builder and then add the code so it reads
Code:
private sub cmdOn_click()
   ap_EnableShift
end sub

private sub cmdOff_click()
   ap_ap_DisableShift
end sub
Thanks!! Worked perfectly. I can't believe it was that simple, I was obviously over thinking it in my own head.
Cheers
M (y) :)
 

isladogs

MVP / VIP
Local time
Today, 18:25
Joined
Jan 14, 2017
Messages
18,211
You should be aware that certain properties only take effect the second time you restart your application.
You will need to close and reopen your database TWICE to ensure the shift bypass has been disabled

NOTE:
1. You can't run a module - you should have said run a procedure
2. For info, it is possible to re-enable the shift bypass externally
 

mistermarty

Member
Local time
Today, 18:25
Joined
Oct 31, 2020
Messages
41
You should be aware that certain properties only take effect the second time you restart your application.
You will need to close and reopen your database TWICE to ensure the shift bypass has been disabled

NOTE:
1. You can't run a module - you should have said run a procedure
2. For info, it is possible to re-enable the shift bypass externally
Thanks for your reply. It looks like this one works straight away, as the shift key is disabled if I hit the "Disable" command button, log out of the DB and then try to go back in with the shift key pressed.

I'm guessing (and kind of hoping) that to re-enable the shift bypass externally as you mentioned above isn't very straight forward? I was banking on my disable function to make my DB a bit more secure, in case any of the other users had more than a basic knowledge of MS Access. 🙁
 

isladogs

MVP / VIP
Local time
Today, 18:25
Joined
Jan 14, 2017
Messages
18,211
Nothing to do with your code
It may work first time ... providing the property already exists. Otherwise as stated you need to restart it twice.

Sorry to tell you this.... but anyone who knows how to disable it can very easily re-enable it from another application
You should use disabling the shift bypass in conjunction with a number of other security features.
See my web article for more info: Improve Security 2 - Mendip Data Systems

BTW - it is possible to use code to check whether the shift bypass has been disabled externally and, if so, close the database automatically
 

mistermarty

Member
Local time
Today, 18:25
Joined
Oct 31, 2020
Messages
41
Nothing to do with your code
It may work first time ... providing the property already exists. Otherwise as stated you need to restart it twice.

Sorry to tell you this.... but anyone who knows how to disable it can very easily re-enable it from another application
You should use disabling the shift bypass in conjunction with a number of other security features.
See my web article for more info: Improve Security 2 - Mendip Data Systems

BTW - it is possible to use code to check whether the shift bypass has been disabled externally and, if so, close the database automatically
Cheers, I will have a look at your article 👍
 

Users who are viewing this thread

Top Bottom