SendKeys is messing with my NumLock key through VBA code in access form

I have the following code for an access form. It seems that SendKeys messing with my NumLock key, toggling it when I open and close the form.

For legitimate reasons that I don’t want to go into, I really don’t want to hide the feed completely (I want the dropdown menus to still be available), so the DoCmd.ShowToolbar command DoCmd.ShowToolbar not my preferred way of doing this.

Does anyone have any suggestions as to how I can modify the code below to accomplish what I want using the SendKeys command?

Using Access 2007, the command

 CommandBars.ExecuteMso "MinimizeRibbon" 

unavailable to me.

By the way, the database will be distributed, so the solution should be contained in the database.

 Private Sub Form_Close() ' Unhide navigation pane DoCmd.NavigateTo "acNavigationCategoryObjectType" DoCmd.Maximize ' Maximize the ribbon RibbonState = (CommandBars("Ribbon").Controls(1).Height < 75) Select Case RibbonState Case True SendKeys "^{F1}", True Case False 'Do nothing, already maximized End Select End Sub Private Sub Form_Load() ' Hide navigation pane DoCmd.NavigateTo "acNavigationCategoryObjectType" DoCmd.Minimize Debug.Print Application.CommandBars.Item("Ribbon").Height ' Minimize ribbon RibbonState = (CommandBars("Ribbon").Controls(1).Height < 100) Select Case RibbonState Case True 'Do nothing, already minimized Case False SendKeys "^{F1}", False End Select End Sub 
+12
vba access-vba ms-access-2007 sendkeys
source share
9 answers

This is a bug in Microsoft VBA. But there is a workaround.

Use F8 to run the macro and find where it disconnects. This is usually after SendKeys .

Then add the Sendkeys "{NUMLOCK}", True after the line to cancel the effect.

If you cannot find it, just add it at the end and when it is over, it will return. Hopefully if you add it during the show / hide process, it will work.

+13
source share

I had a similar problem and found a solution in some vba forum. Instead of intercepted Sendkeys, you can simulate kyes like this.

  Option Explicit '//WIN32API Declare Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) '//WIN32API Constant Public Const KEYEVENTF_EXTENDEDKEY = &H1 Public Const KEYEVENTF_KEYUP = &H2 Public Const VK_CONTROL = &H11 Public Const VK_SHIFT = &H10 Public Const VK_F6 = &H75 Public Function PreviousTab() keybd_event VK_CONTROL, 0, 0, 0 keybd_event VK_SHIFT, 0, 0, 0 keybd_event VK_F6, 0, 0, 0 keybd_event VK_F6, 0, KEYEVENTF_KEYUP, 0 keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0 keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0 End Function 

Other keys can be found here vba forum. This "previousTab" function simply sends the Control + Shift + F6 key.

+4
source share

This line caused my problem:

 Application.SendKeys "%s" 

SOLVED, changing to this:

 Application.SendKeys "{NUMLOCK}%s" 

There is no difference between adding {NUMLOCK} at the beginning or end of a line.

+3
source share

This is caused by:

 Sendkeys "any key", False 

Instead of False , use True as the second parameter.

+2
source share

Immediately after your SendKeys statement, add these two lines:

 DoEvents SendKeys "{NUMLOCK}{NUMLOCK}" 
+1
source share

When you execute the final sendKeys command in your code, adding in {NUMLOCK} to the statement can do the trick, as RodB and iceBird76 note. But this is not a good coding practice, and here's why: if something is different from one point in time when you run a macro, it may or may not work. I know this because I myself am experiencing a similar problem. When I do the sendKeys command at the end of my program, sometimes Num Lock will continue to work, but in other cases it will remain unchanged, only depending on certain variables in my table (regardless of whether I include / NUMLOCK in my last SendKeys operator).
I will not go into the details of my own variables, but the fact is that in order to create a program / macro that will constantly support your Num Lock, you need the FIRST TEST TO SEE IF THE NUMBER BLOCK IS ON OR OFF, then execute the code based on current state.

 Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer Private Const kNumlock = 144 Public Function NumLock() As Boolean NumLock = KeyState(kNumlock) If (NumLock = True) Then MsgBox ("Num lock was off. Will turn back on now...") SendKeys "{NUMLOCK}", True Else: MsgBox ("Num Lock stayed on") End If End Function Private Function KeyState(lKey As Long) As Boolean KeyState = CBool(GetKeyState(lKey)) End Function Sub myMainMethod() 'do a function here that includes .SendKeys Call NumLock End Sub 

This sample program will give you a confirmation message to enable or disable Num Lock and enable it if it is turned off.

+1
source share

The SendKeys () function built into VBA does have a side effect, which leads to the deactivation of NumLock. But you can use a workaround and call another implementation of the same function, which is part of the WSCRIPT component (part of the Windows operating system). The following code example shows how you can create a link to this component and then call its method:

 Set WshShell = CreateObject("WScript.Shell") WshShell.SendKeys "^g", True 

Thus, you get the same functionality (in this case, call the Ctrl-G key combination), but in this case there are no problems with NumLock.

+1
source share

SendKeys "^{HOME}", True disabled the num lock, so I just repeated the command and turned it on again:

 SendKeys "^{HOME}", True SendKeys "^{HOME}", True 
0
source share

Having tried many solutions. The most solid seems to be the link below. Insert it into the module.

http://access.mvps.org/access/api/api0046.htm

0
source share

All Articles