Search code examples
vbams-accessms-access-2016

Switch the keyboard language with VBA


I have a multi-lingual database and am trying to make it so that the correct language is typed in the correct fields by using the "on current" event. Presently, the users use Alt+Shift until the language appears in the task bar. The languages are English, Hebrew, Arabic & German. The users are fluent typists in the languages. Constantly Alt+Shifting to select the correct language for each field slows down data entry dramatically.

I have searched the internet for VBA code to do this. The closest I can find is "Sendkeys" which works in part by turning off & on Scroll Lock for autohotkeys which is only used for Hebrew vowel pointing, but I am unable to figure out how to select the keyboard language via VBA code.

Sample code for the Hebrew Name search button:

Private Sub btnHebCustomerSearch()
  SendKeys "{scrolllock}", True
    Me.FilterOn = False
    DoCmd.GoToRecord , "", acFirst
    DoCmd.RunCommand acCmdFind
  On Error Resume Next
End Sub

Solution

  • I found a ton of web resources on this topic with Bing search "Access VBA switch keyboard language".

    Use API function:

    Private Declare PtrSafe Function ActivateKeyboardLayout Lib _
    "user32.dll" (ByVal myLanguage As Long, Flag As Boolean) As Long
    
    'define your desired keyboardlanguage
    'find your desired language at http://www.trigeminal.com/frmrpt2dap.asp
    
    Private Const MKD = 1071 'macedonian keyboard language layout
    Private Const eng = 1033 'english(united states)keyboard language layout
    
    Private Sub A_Enter()
    Call ActivateKeyboardLayout(MKD, 0)
    End Sub
    
    Private Sub A_Exit(Cancel As Integer)
    Call ActivateKeyboardLayout(eng, 0)
    End Sub
    

    Untested, code from https://www.access-programmers.co.uk/forums/threads/any-idea-how-to-change-system-keyboard-lenguage-with-vba.193030/

    Probably should declare the API function as Public and place it in a general module instead of behind a form. The Const declarations are not needed if intrinsic language code constants are used. View a list in VBA object browser. Type MsoLanguageID in the Search box and click Search button. https://learn.microsoft.com/en-us/office/vba/api/access.application.languagesettings