Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389

    Please approve my code

    My respectful greetings to all of you. I want to disable all except a few shortcut keys in the form, for this I have seen the codes on Google and they are working properly but I do not have complete knowledge about such codes.
    I want to enable alt + F4 in the form, for this I have modified the code. Please tell me whether I have made the correct modification. If the modification is correct then why I have to write 'End Select' three times in it.


    Thank you


    Code:
    Public Sub sb_disablekeys(keycode As Integer, Shift As Integer)
        'All keyboard events with CTRL don’t function anymore with the exception of CTRL+C and CTRL+V
     '   ‘All keyboard events with ALT don’t function anymorge
     '   ‘All function keys are disabled
     
        Select Case Shift
            Case acCtrlMask   '  ‘CTRL pressed
                Select Case keycode
                    Case 0 To 16, 18 To 66, 68 To 85, 87 To 255
    
                    'All keycodes with the exception of 17 (CTRL), 67 (CTRL+C) and 86 (CTRL+V) are set to 0.
                        keycode = 0
                End Select
            Case acAltMask      'Alt pressed
                        keycode = 0
        End Select
    
    Select Case keycode  'FOR STOP PGUP & PAGE DOWN
        Case 33 To 34
        keycode = 0
        End Select
    
    
        Select Case keycode
            Case vbKeyF1 To vbKeyF16        '        ‘Function key pressed
                keycode = 0
        End Select
    End Sub
    
    
    Private Sub Form_KeyDown(keycode As Integer, Shift As Integer)
    sb_disablekeys keycode, Shift
    
    End Sub
    
    
    Private Sub Form_Open(CANCEL As Integer)
    Me.KeyPreview = True
    End Sub

    Revise Code

    Code:
    Public Sub sb_disablekeys(keycode As Integer, Shift As Integer)
        'All keyboard events with CTRL don’t function anymore with the exception of CTRL+C and CTRL+V
     '   ‘All keyboard events with ALT don’t function anymorge
     '   ‘All function keys are disabled
     
        Select Case Shift
            Case acCtrlMask   '  ‘CTRL pressed
                Select Case keycode
                    Case 0 To 16, 18 To 66, 68 To 85, 87 To 255
    
                    'All keycodes with the exception of 17 (CTRL), 67 (CTRL+C) and 86 (CTRL+V) are set to 0.
                        keycode = 0
                End Select
                
                         
           Select Case Shift
            Case acAltMask      'Alt pressed
            Select Case keycode
            Case 0 To 17, 19 To 255
            'Case 0 To 8, 10 To 17, 19 To 255
                      keycode = 0
                End Select               '1 time
                End Select               '2 time
                End Select               '3 time
    
    
    Select Case keycode  'FOR STOP PGUP & PAGE DOWN
        Case 33 To 34
        keycode = 0
        End Select
    
    
        Select Case keycode
            'Case vbKeyF1 To vbKeyF16        '        ‘Function key pressed
            Case vbKeyF1, vbKeyF3, vbKeyF5 To vbKeyF16
                keycode = 0
                
        End Select
    End Sub

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    To answer your question, each Select needs an End Select. You are nesting them, so the Ends have to be properly nested as well. If you indented you code properly such things should be obvious to you. I'm assuming what you have works but would rather view it properly indented to be sure but I'll let you do that. BTW, there should be no need to repeat the Shift select block.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Quote Originally Posted by Micron View Post
    To answer your question, each Select needs an End Select. You are nesting them, so the Ends have to be properly nested as well. If you indented you code properly such things should be obvious to you. I'm assuming what you have works but would rather view it properly indented to be sure but I'll let you do that. BTW, there should be no need to repeat the Shift select block.
    Hello Micron,
    Thank you very much for replying.
    As you have told that every select requires an end select, similarly I have checked in this entire code that every select has been given an end select, yet two end selects are coming extra in this code. Please. See in attached pic.
    As you mentioned there is no need to repeat shift selection but if I don't repeat it then Alt F4 key does not work please see in attached pic.
    Thank you.
    Attached Thumbnails Attached Thumbnails 1endselect.png   2select.png  

  4. #4
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    I did not know about vba code indent then I googled it then I came to know that to auto indent vba code third party software is required. Am I right? If yes then please tell which software you are use.
    Thank you.

  5. #5
    Join Date
    Apr 2017
    Posts
    1,680
    Your sub must be something like
    Code:
    Public Sub sb_disablekeys(keycode As Integer, Shift As Integer)
    
    
        Select Case Shift
            Case asCtrlMask ' as parameter Shift is integer value, why not simply use the integer matching with asCltrlMask here?
                Select Case keycode
                    Case 0 To 16, 18 To 66, 68 To 85, 87 To 255
                        keycode = 0
                End Select
            Case asAltMask ' as parameter Shift is integer value, why not simply use the integer matching with asAltMask here?
                Select Case keycode
                    Case 0 To 17, 19 To 255
                        keycode = 0
                End Select
            Case Else
                Select Case keycode
                    Case 33 To 34
                        keycode = 0
                    Case vbFKey1, vbFKey3, vbFKey5 To vbFKey16
                        keycode = 0
                End Select
        End Select
    End Sub

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by deepaksharma View Post
    I did not know about vba code indent then I googled it then I came to know that to auto indent vba code third party software is required. Am I right? If yes then please tell which software you are use.
    Thank you.
    You indent with the Tab key, YOURSELF as you type in your code.

    You can get software that will indent all the code where you were to lazy to do so yourself, or have to tidy up someone else's code who also could not be bothered.
    Smart Indenter is one I use for the latter situation.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    MZ tools has an inbuilt indenter, and works with 64 Bit Office, which Smart Indenter doesn't.
    Smart indenter is free however, whereas MZ Tools has a cost.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    RubberDuck is another, but that does much more. Takes a bit of getting used to.
    https://rubberduckvba.com/Features
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Thank you very much to all of you, now I understand it well and welshgasman and minty I am studying further on all the programs you suggested.
    I request you all, can any of you please help me in solving the note #3 posted by me also?
    I will always be grateful to you.
    Thank you.

  10. #10
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Avril Laanements Has already given you a neater more verbose method?

    You original code should look like this;
    Code:
    Public Sub sb_disablekeys(keycode As Integer, Shift As Integer)
        'All keyboard events with CTRL don’t function anymore with the exception of CTRL+C and CTRL+V
        '   ‘All keyboard events with ALT don’t function anymorge
        '   ‘All function keys are disabled
        Select Case Shift
            Case acCtrlMask   '  ‘CTRL pressed
                Select Case keycode
                    Case 0 To 16, 18 To 66, 68 To 85, 87 To 255
    
    
                        'All keycodes with the exception of 17 (CTRL), 67 (CTRL+C) and 86 (CTRL+V) are set to 0.
                        keycode = 0
                End Select
                                   
                Select Case Shift
                    Case acAltMask      'Alt pressed
                        Select Case keycode
                            Case 0 To 17, 19 To 255
                                'Case 0 To 8, 10 To 17, 19 To 255
                                keycode = 0
                        End Select               '1 time
                End Select               '2 time
        End Select               '3 time
    
    
        Select Case keycode  'FOR STOP PGUP & PAGE DOWN
            Case 33 To 34
                keycode = 0
        End Select
    
    
        Select Case keycode
            'Case vbKeyF1 To vbKeyF16        '        ‘Function key pressed
            Case vbKeyF1, vbKeyF3, vbKeyF5 To vbKeyF16
                keycode = 0
                
        End Select
    
    
    End Sub


    Which highlights why you need the correct number of end selects.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  11. #11
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Quote Originally Posted by Minty View Post
    Avril Laanements Has already given you a neater more verbose method?





    Thanks, can you tell what is making "select case swift" work twice here?
    Attached Thumbnails Attached Thumbnails 3r.png  

  12. #12
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I have no idea.
    You can now see why properly indenting your code makes things more obvious?

    Put a break point on the first Select case statement and then walk through the code to see what it is actually doing...
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Look at the blue highlights in the image you posted. The pairs are first and last, 2nd and 3rd, 4 and 7, 5 and 6. It is as I originally said.

    If your original code gets some indentation it would look something like this (see the notes within):
    Code:
    'ORIGNINAL, INDENTED
    Select Case Shift
    'if next line is true the next Shift Case will not evaluate so ALT key test is ignored
    'if the next line is false, the ALT key test will evaluate
        Case acCtrlMask
            Select Case KeyCode
                Case 0 To 16, 18 To 66, 68 To 85, 87 To 255
                    'All keycodes with the exception of 17 (CTRL), 67 (CTRL+C) and 86 (CTRL+V) are set to 0.
                    KeyCode = 0
            End Select
             
            Select Case Shift
                Case acAltMask      'Alt pressed
                    Select Case KeyCode
                        Case 0 To 17, 19 To 255
                            KeyCode = 0
                    End Select               '1 time
            End Select               '2 time
    End Select
    
    'if either ALT or CTRL test has evaluated, key code become 0 so this next part won't do anything
    'next test seems pointless because 33,34 was already evaluated above (18 to 66)
    Select Case KeyCode  'FOR STOP PGUP & PAGE DOWN
        Case 33 To 34
            KeyCode = 0
    End Select
    
    'if either ALT or CTRL test has evaluated, key code become 0 so this next part won't do anything
    'next test seems pointless because these codes should lie between 87 to 255 AFAIK
    Select Case KeyCode
        'Case vbKeyF1 To vbKeyF16        '        ‘Function key pressed
        Case vbKeyF1, vbKeyF3, vbKeyF5 To vbKeyF16
            KeyCode = 0
    End Select
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Quote Originally Posted by Micron View Post
    Look at the blue highlights in the image you posted. The pairs are first and last, 2nd and 3rd, 4 and 7, 5 and 6. It is as I originally said.

    thanks micron
    Actually I researched this code yesterday so I got some different experience in it please see the link pic.
    Thank you.
    Attached Thumbnails Attached Thumbnails 21Untitled.png  

  15. #15
    Join Date
    Apr 2017
    Posts
    1,680
    As I did show in my post #5, there is no need for 2nd 'Select Case Shift' cycle
    Code:
    SELECT CASE Shift
    CASE Value1OfShift ' i.e. acCtrlMask
         ' Any number of command lines (i.e. a first SELECT CASE keycode cycle is inserted)
    CASE Value2OfShift ' i.e. acAltMask
         ' Any number of command lines (i.e. a second SELECT CASE keycode cycle is inserted)
    End Select
    When 1st CASE evaluates True, all following command lines are run, until the next line starts with 'CASE', or is 'End Select', and then End Select is run ;
    When 1st CASE evaluates False, and there is more code rows before 'End Select' starting with 'CASE', the 2nd CASE is evaluated;
    Etc. until End Select is reached.
    In case in any section of command lines a new SELECT CASE is started, it must be ended with End Select before next CASE condition of main SELECT CASE, or before End Select of main SELECT CASE, when this is the last command lines section.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 20
    Last Post: 10-13-2015, 09:05 AM
  2. Replies: 3
    Last Post: 10-16-2014, 08:49 AM
  3. Report Code is not allowing return to main code
    By rcwiley in forum Programming
    Replies: 2
    Last Post: 06-16-2013, 10:31 AM
  4. Replies: 7
    Last Post: 05-28-2013, 09:11 AM
  5. Replies: 1
    Last Post: 05-04-2013, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums