PDA

View Full Version : [SOLVED:] Stop user from pressing CTRL + BREAK



waimea
01-07-2019, 08:53 AM
Hi, I have a login script that doesn't allow the user to close the userform, however the user can still use CTRL + BREAK to stop the login script and there is NO "login".

I have googled and found that I should try to trap error 18 and/or disable the ESC key.

Is there any way to use Application.Username if CTRL + BREAK is pressed?

Psuedo code:

If CTRL + BREAK is pressed then
Username = Application.Username

Logit
01-07-2019, 09:41 AM
.
The following prevents using CTRL/BREAK :



Sub preventCtrlBreak()
On Error GoTo MyErrorHandler:
t = Timer
Application.EnableCancelKey = xlErrorHandler


'#######################################################
'delete these lines if you don't want to inform the user ... also line above t = Timer
MsgBox "Now you can't use ctrl + break"
Do While Timer - t < 5
Loop
'#######################################################




MyErrorHandler:
If Err.Number = 18 Then
MsgBox "Stop hitting ctrl + break !!!" '<-- change this line to your message UserName
Resume
Else
'Do something to make your impatient user happy
End If
End Sub

waimea
01-07-2019, 09:49 AM
Hi Logit,

thank you for your reply! I can still press CTRL + BREAK on my computer with this code?

Logit
01-07-2019, 09:56 AM
If you run the macro, then anytime you press CTRL/BREAK it won't do anything. You have disabled those two keys.

waimea
01-07-2019, 12:14 PM
Do I have to keep the loop?

Logit
01-07-2019, 01:27 PM
.
This is a cleaner and more direct approach to what you are wanting ...

Paste this in the ThisWorkbook Module :



Option Explicit


Private Sub Workbook_Open()
Application.OnKey "^{BREAK}", "NoNo"
End Sub


Paste this in a regular module :



Option Explicit


Sub NoNo()
Application.Quit
Application.Visible = False
End Sub


The above will force the workbook to close and prevent the user to proceed with any other attempts to circumvent your programming.

waimea
01-07-2019, 01:40 PM
Hi Logit,

thank you for this, it is exactly what I want!

Logit
01-07-2019, 02:06 PM
.
You are welcome.

waimea
01-07-2019, 10:54 PM
Would it be possible to say, if CTRL + BREAK is pressed, then the string Username becomes Application.Username??

Logit
01-07-2019, 11:07 PM
.
Describe in more detail what it is you are attempting to do.

waimea
01-07-2019, 11:13 PM
Hi Logit,

I am thinking that I should allow CTRL + BREAK and ESC but when a user pressed CTRL + BREAK or ESC I want my variable Username to become Application.Username?

Username is defined in a userform but perhaps I should declare a public variable in a module named Username?

Logit
01-07-2019, 11:22 PM
.
We are going in circles ... sorry.

Why do you want to create a variable when the user presses CTRL/BREAK or ESC ?

I still don't understand.

waimea
01-07-2019, 11:40 PM
Hi Logit,

I apologize for not being clear.

I have tried your code and I can still interrupt my login screen by pressing CTRL + BREAK and enter the VBA EDITOR.

Maybe I have missed something?

waimea
01-08-2019, 01:47 AM
I have thought about it and I would like to show the login userform when CTRL + BREAK is pressed.

Is this possible?

Logit
01-08-2019, 08:45 AM
show the login userform

Is this a form you created in your project ?

waimea
01-08-2019, 08:56 AM
Hi Logit,

thank you for your reply! I have a login form that can't be closed by pressing the X in the top right corner.

However, you can still press CTRL + BREAK so I am thinking that if you press ESC or CTRL + BREAK the login form is showed again.

Is this possible?

Logit
01-08-2019, 10:20 AM
I can still interrupt my login screen by pressing CTRL + BREAK and enter the VBA EDITOR.

I re-read the posts. I believe the above in Post #13 gets to the heart of the matter concerning your ultimate goal.


Excel has a few built in methods that "attempt" to provide security. HOWEVER, these security measures are only effective
against users who are absolutely EXCEL challenged. In other words ... they have no idea how EXCEL works behind the scenes
and more importantly, don't care. They have no interest in viewing / learning / editing the code. The Internet is loaded with
example code to break through EXCEL's built in security measures.

There are different measures / macros / add ons / etc. that have been created through the years, intending to make EXCEL more secure
or even 'inpenetrable' by anyone except the coder. Problem is ... all of these methods have been proven to be inadequate.

I myself have searched for several years for a means to protect my code ... no luck.

There are two methods that might possibly work ... but there is a cost.

#1 If you could compile your workbook into an executable file, just like any other commercial program, that would stop almost everyone except the hackers from
getting your code. Problem is ... no one has created a means of making an Excel workbook into an executable program. I've investigated some of the commercial
programs sold on the Internet that claim to make Excel into an executable. They don't work. I've been able to break their code that wraps around the workbook code.

#2 You could use a program that "scrambles" your code into unrecognizable gibberish. Here is an example : https://www.excel-pratique.com/en/vba_tricks/vba-obfuscator.php
My personal opinion is this is probably the most effective means of protecting your code. There is a downside to using an obfuscator .... every program I've tried either wouldn't
obfuscate every word / character / string in the macros ... and this would allow a good coder to piece together the code to the original format. OR ... the obfuscator would
do it's trick on everything but the macros wouldn't run. You will have to go back into the obfuscated code and located the key words in the macro commands and return
those back to the original form.

A few examples of this are the terms: SUB, INTEGER, CONST, DIM, FOR, IF, NEXT, etc. In a few of the cases I had to leave a large portion of the original coding in place for the macro/s to run. This
made the obfuscator totally useless in my mind because it wasn't doing the job I paid good money for it to do.

If you are able to locate a means to totally protect macro code, please let me and others know. Sorry but I can't assist you with the search ... I've been there / done that and came up empty handed.