Solved Activating a tab in a navigation, problem SendKeys (1 Viewer)

Superpat

Member
Local time
Today, 07:57
Joined
Aug 15, 2020
Messages
96
Hello, I read the subject: "https://www.access-programmers.co.uk/forums/threads/activating-a-tab-in-a-navigation-subform.316476/".
I have a problem with the opening of my form, the desired tab is activated. :
Private Sub Form_Open(Cancel As Integer)
Forms("_f_Navigation").ContrôleNavigation0.Tabs(3).SetFocus
'SendKeys "{ENTER}"
SendKeys " ", True ' Si le verrouillage est activé pas de pb, si désactivé pb
End Sub
The problem comes from Sendkeys, he activates the Num Lock key or disables it randomly. Depending on the previous state of this key. I would like that NUM LOCK be activated by default.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:57
Joined
May 7, 2009
Messages
19,169
Paste this in a Module.
to use:

Call fSendKeys "{ENTER}", True

Code:
Option Compare Database
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' author:
'
'   Dev Ashish and Arvin Meyer
'
' modified:
'
'   arnelgp
'   new VB7 and VBA7 support
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' VERY GOOD, WORKING!
'
'******** Code Start ***********
      ' Declare Type for API call:
      Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128   '  Maintenance string for PSS usage
      End Type

      ' API declarations:
#If VBA7 Then
    Private Declare PtrSafe Function GetVersionEx Lib "kernel32" _
        Alias "GetVersionExA" ( _
            lpVersionInformation As OSVERSIONINFO) As Long
            
    Private Declare PtrSafe Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare PtrSafe Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long

    Private Declare PtrSafe Sub keybd_event Lib "user32" ( _
        ByVal bVk As Byte, _
        ByVal bScan As Byte, _
        ByVal dwflags As Long, _
        ByVal dwExtraInfo As LongPtr)

#Else
      Private Declare Function GetVersionEx Lib "kernel32" _
         Alias "GetVersionExA" _
         (lpVersionInformation As OSVERSIONINFO) As Long

      Private Declare Sub keybd_event Lib "user32" _
         (ByVal bVk As Byte, _
          ByVal bScan As Byte, _
          ByVal dwflags As Long, ByVal dwExtraInfo As Long)

      Private Declare Function GetKeyboardState Lib "user32" _
         (pbKeyState As Byte) As Long

      Private Declare Function SetKeyboardState Lib "user32" _
         (lppbKeyState As Byte) As Long
#End If

' Constant declarations:
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VER_PLATFORM_WIN32_NT = 2
Const VER_PLATFORM_WIN32_WINDOWS = 1

Function IsCapsLockOn() As Boolean
        Dim o As OSVERSIONINFO

        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsCapsLockOn = keys(VK_CAPITAL)
End Function

Sub ToggleCapsLock()
        Dim o As OSVERSIONINFO

        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)

        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
        'Toggle capslock
            keys(VK_CAPITAL) = Abs(Not keys(VK_CAPITAL))
            SetKeyboardState keys(0)
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
          'Simulate Key Press>
            keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
          'Simulate Key Release
            keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
               Or KEYEVENTF_KEYUP, 0
        End If
End Sub

Function IsNumLockOn() As Boolean
        Dim o As OSVERSIONINFO
        
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsNumLockOn = keys(VK_NUMLOCK)
End Function

Sub ToggleNumLock()
        Dim o As OSVERSIONINFO
                
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)

          If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
                keys(VK_NUMLOCK) = Abs(Not keys(VK_NUMLOCK))
                SetKeyboardState keys(0)
          ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
          'Simulate Key Press
            keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
          'Simulate Key Release
            keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
               Or KEYEVENTF_KEYUP, 0
          End If
        
End Sub

Function IsScrollLockOn()
        Dim o As OSVERSIONINFO
        
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsScrollLockOn = keys(VK_SCROLL)
End Function

Sub ToggleScrollLock()
        Dim o As OSVERSIONINFO
        
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
            keys(VK_SCROLL) = Abs(Not keys(VK_SCROLL))
            SetKeyboardState keys(0)
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
            'Simulate Key Press
            keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
            'Simulate Key Release
            keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
        End If
End Sub

Private Sub mySendKeys(sKeys As String, Optional bWait As Boolean = False)
    Dim bNumLockState As Boolean
    Dim bCapsLockState As Boolean
    Dim bScrollLockState As Boolean
    bNumLockState = IsNumLockOn()
    bCapsLockState = IsCapsLockOn()
    bScrollLockState = IsScrollLockOn()
    SendKeys sKeys, bWait
    If IsNumLockOn() <> bNumLockState Then
        ToggleNumLock
    End If
    If IsCapsLockOn() <> bCapsLockState Then
        ToggleCapsLock
    End If
    If IsScrollLockOn() <> bScrollLockState Then
        ToggleScrollLock
    End If
End Sub

Function fSendKeys(sKeys As String, Optional bWait As Boolean = False)
' Function to make it callable from macros
    mySendKeys sKeys, bWait
End Function
'******** Code End ***********
 

missinglinq

AWF VIP
Local time
Today, 02:57
Joined
Jun 20, 2003
Messages
6,423
Not sure what the

SendKeys " ", True

is supposed to be doing (?setting SendKeys to nothing?)

but try replacing it with

Sendkeys "{NUMLOCK}", True

and see what happens.

Linq ;0)>
 

Superpat

Member
Local time
Today, 07:57
Joined
Aug 15, 2020
Messages
96
Paste this in a Module.
to use:

Call fSendKeys "{ENTER}", True

Code:
Option Compare Database
....
End Sub

Function fSendKeys(sKeys As String, Optional bWait As Boolean = False)
' Function to make it callable from macros
    mySendKeys sKeys, bWait
End Function
'******** Code End ***********
Thanks arnelgp for your answer,
There is a problem :

sendkeys1.jpg

Hello missinglinq, If I do not put this command, the desired tab does not open.
Sendkeys "{NUMLOCK}", True is not working
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:57
Joined
May 7, 2009
Messages
19,169
sorry, my answer on post#2 is incorrect.
you need to enclosed the parameters in parenthesis
if you are going to Call the function.

Call fSendKeys("{ENTER}", True)
 

Superpat

Member
Local time
Today, 07:57
Joined
Aug 15, 2020
Messages
96
Thanks arnelgp,
I try it,
Private Sub Form_Open(Cancel As Integer)
Forms("_f_Navigation").ContrôleNavigation0.Tabs(3).SetFocus
'SendKeys "{ENTER}"
'SendKeys " ", True ' Si le verrouillage est activé pas de pb, si désactivé pb
Call fSendKeys("{ENTER}", True)
End Sub
I am sorry, but it freeze !
sendkeys2.jpg
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:57
Joined
May 7, 2009
Messages
19,169
sorry, i don't experience that.
if it hangs, don't use. try BrowseTo.
you need to supply the correct name of the Bold words.


Private Sub Command12_Click()
DoCmd.BrowseTo ObjectType:=acBrowseToForm, _
ObjectName:="theSubFormName", _
PathToSubformControl:="_f_Navigation.NavigationSubformName", _
WhereCondition:="", _
Page:="", _
DataMode:=acFormEdit

EDIT:
wait a minute, what Tab are you trying to Activate? f_Operation (tab index=3)
why don't you directly use the NavigationButton name.

Me![The Navigation Button Name Of f_Operation].SetFocus
Call fSendKeys("{ENTER}", True)
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:57
Joined
May 7, 2009
Messages
19,169
sample demo
 

Attachments

  • NavigationForm.accdb
    560 KB · Views: 202

Superpat

Member
Local time
Today, 07:57
Joined
Aug 15, 2020
Messages
96
Thank you arnelgp
, I just noticed that when copying the module, there was a deterioration of 2 lines, I repaired this problem and everything works properly there is no more modification of the Num lock key.
Excuse me for my mistake and thank you for your wise advice
Code:
Private Sub Form_Open(Cancel As Integer)
    Me.ContrôleNavigation0.Tabs(3).SetFocus
    Call fSendKeys("{ENTER}", True)
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:57
Joined
May 7, 2009
Messages
19,169
you must try to understand BrowseTo.
it is much simpler to implement.
 

Superpat

Member
Local time
Today, 07:57
Joined
Aug 15, 2020
Messages
96
Thanks very much arnelgp,
Code:
Private Sub Form_Open(Cancel As Integer)
'    Me.ContrôleNavigation0.Tabs(3).SetFocus
'    Call fSendKeys("{ENTER}", True)
    DoCmd.BrowseTo ObjectType:=acBrowseToForm, _
    ObjectName:="f_Operation", _
    PathToSubformControl:="_f_Navigation.SousFormulaireNavigation", _
    WhereCondition:="", _
    Page:="", _
    DataMode:=acFormEdit
End Sub
I tried and I no longer need the validation command: ENTER.
 

Users who are viewing this thread

Top Bottom