Consulting

Results 1 to 17 of 17

Thread: Stop user from pressing CTRL + BREAK

  1. #1
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location

    Stop user from pressing CTRL + BREAK

    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
    Last edited by waimea; 01-07-2019 at 09:06 AM.

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    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

  3. #3
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    Hi Logit,

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

  4. #4
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    If you run the macro, then anytime you press CTRL/BREAK it won't do anything. You have disabled those two keys.

  5. #5
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    Do I have to keep the loop?

  6. #6
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    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.

  7. #7
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    Hi Logit,

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

  8. #8
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    You are welcome.

  9. #9
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    Would it be possible to say, if CTRL + BREAK is pressed, then the string Username becomes Application.Username??

  10. #10
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    Describe in more detail what it is you are attempting to do.

  11. #11
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    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?

  12. #12
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    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.

  13. #13
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    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?

  14. #14
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    I have thought about it and I would like to show the login userform when CTRL + BREAK is pressed.

    Is this possible?

  15. #15
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    show the login userform
    Is this a form you created in your project ?

  16. #16
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    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?

  17. #17
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    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/vb...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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •