Data input in English

ScottXe

Registered User.
Local time
Today, 20:03
Joined
Jul 22, 2012
Messages
123
The database was developed on a computer with only English input setup. The user group is a combination of computers with only English input setup and both English and Chinese input setup. For those computers with both English and Chinese input setup, when they use the form to enter the records, the data input is in Chinese input. The users are very annoying to change to English input before data input. It is not the worse case and the most worse case is that when go to next new record, the input is automatically set back to Chinese and the user needs to manually changes to English before continue the data input. Is there any way to set the data input to English by default and if need, the user manually change to Chinese? Thanks!
 
There was a question recently about someone having a problem with Arabic (I think). Not sure if it was satisfactory resolved but worth searching the forum for, if you have a mind to.

This link allows you to search AWF "Only" ....

Search AWF

I have it Bookmarked!
 
Last edited:
Hi Scott,

I have an application where I need to switch between Chinese and English entry, so developed the code to do this. It is capable of switching between any languages. First I'll list the code, then explain how to use it.

'You'll need to create 2 standard modules. In the first place this:

Code:
[FONT=Arial][FONT=Arial]Option Private Module[/FONT]
 [FONT=Arial]Option Compare Database[/FONT]
 [FONT=Arial]Option Explicit[/FONT]
 
 [FONT=Arial]Private Const KL_NAMELENGTH As Long = 9 'HKL string buffer length.[/FONT]
 [FONT=Arial]Private Const KLF_SUBSTITUTE_OK As Long = &H2 'Allow locale substitutions. Default.[/FONT]
 
 [FONT=Arial]Private mstrLangID As String[/FONT]
 
 [FONT=Arial]#If Win64 = 1 And VBA7 = 1 Then[/FONT]
 [FONT=Arial]    Dim mlngChinese_HKL As LongPtr  'Handle to Chinese input method.[/FONT]
 [FONT=Arial]    Dim mlngDefault_HKL As LongPtr  'Handle to default input method.[/FONT]
 [FONT=Arial]    Dim mlngPrevious_HKL As LongPtr 'Use to store previous HKL.[/FONT]
 [FONT=Arial]    Private Declare PtrSafe Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameW" _[/FONT]
 [FONT=Arial]                        (ByVal pwszKLID As LongPtr) As Long[/FONT]
 [FONT=Arial]    Private Declare PtrSafe Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutW" _[/FONT]
 [FONT=Arial]                        (ByVal pwszKLID As LongPtr, ByVal Flags As Long) As LongPtr[/FONT]
 [FONT=Arial]    Private Declare PtrSafe Function GetKeyboardLayout Lib "user32" (ByVal idThread As Long) As LongPtr[/FONT]
 [FONT=Arial]    Private Declare PtrSafe Function ActivateKeyboardLayout Lib "user32" _[/FONT]
 [FONT=Arial]                    (ByVal HKL As LongPtr, ByVal Flags As Long) As LongPtr[/FONT]
 [FONT=Arial]#Else[/FONT]
 [FONT=Arial]    Dim mlngChinese_HKL As Long  'Handle to Chinese input method.[/FONT]
 [FONT=Arial]    Dim mlngDefault_HKL As Long  'Handle to default input method.[/FONT]
 [FONT=Arial]    Dim mlngPrevious_HKL As Long 'Use to store previous HKL.[/FONT]
 [FONT=Arial]    Private Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameW" _[/FONT]
 [FONT=Arial]                        (ByVal pwszKLID As Long) As Long[/FONT]
 [FONT=Arial]    Private Declare Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutW" _[/FONT]
 [FONT=Arial]                        (ByVal pwszKLID As Long, ByVal Flags As Long) As Long[/FONT]
 [FONT=Arial]    Private Declare Function GetKeyboardLayout Lib "user32" (ByVal idThread As Long) As Long[/FONT]
 [FONT=Arial]    Private Declare Function ActivateKeyboardLayout Lib "user32" _[/FONT]
 [FONT=Arial]                        (ByVal HKL As Long, ByVal Flags As Long) As Long[/FONT]
 [FONT=Arial]#End If[/FONT]
 
 [FONT=Arial]Public Function SetLanguage(strLCID As String)[/FONT]
 [FONT=Arial]'USE THIS FUNCTION TO CHOOSE LANGUAGE TO SET[/FONT]
 [FONT=Arial]' example:[/FONT]
 [FONT=Arial]'       SetLanguage ("00000409")[/FONT]
 [FONT=Arial]' This function only loads the desired language ID into memory. You must follot with a call to SetKbLayout.[/FONT]
 
 [FONT=Arial]    ' Chinese Locale ID values:[/FONT]
 [FONT=Arial]    ' 00000804 - Chinese Simplified, PRC[/FONT]
 [FONT=Arial]    ' 00001004 - Chinese Simplified, Singapore[/FONT]
 [FONT=Arial]    ' 00000404 - Chinese Traditional, Taiwan[/FONT]
 [FONT=Arial]    ' 00000C04 - Chinese Traditional, Hong Kong S.A.R.[/FONT]
 [FONT=Arial]    ' 00001404 - Chinese Traditional, Macau S.A.R.[/FONT]
 [FONT=Arial]    '[/FONT]
 [FONT=Arial]    ' English Locale ID values (not complete list):[/FONT]
 [FONT=Arial]    ' 00000409 - United States[/FONT]
 [FONT=Arial]    ' 00000809 - United Kingdom[/FONT]
 [FONT=Arial]    ' 00000C09 - Australia[/FONT]
 [FONT=Arial]    ' 00001009 - Canada[/FONT]
 [FONT=Arial]    ' 00004009 - India[/FONT]
 
 [FONT=Arial]    mstrLangID = strLCID & ChrW$(0) 'Null terminated.[/FONT]
 [FONT=Arial]    LoadKbLayout[/FONT]
 
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Public Function SetKbLayout()[/FONT]
 [FONT=Arial]'USE THIS FUNCTION TO ACTIVATE A NEW LANGUAGE.[/FONT]
 [FONT=Arial]' example:[/FONT]
 [FONT=Arial]'       SetKbLayout[/FONT]
 [FONT=Arial]' You must call SetLanguage prior to calling SetKbLayout.[/FONT]
 
 [FONT=Arial]If Not (GetCurrentKbLayoutHKL = mlngChinese_HKL) Then[/FONT]
 [FONT=Arial]    'Remember previous HKL.[/FONT]
 [FONT=Arial]    StorePreviousKbLayoutHKL[/FONT]
 [FONT=Arial]    'Change keyboard layout.[/FONT]
 [FONT=Arial]    ActivateKbLayout mlngChinese_HKL[/FONT]
 [FONT=Arial]End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Public Function RecallPreviousKbLayout()[/FONT]
 [FONT=Arial]ActivateKbLayout mlngPrevious_HKL[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Private Function LoadKbLayout() As Boolean[/FONT]
 
 [FONT=Arial]Dim strHKL As String[/FONT]
 
 [FONT=Arial]strHKL = String$(KL_NAMELENGTH, 0) 'Buffer.[/FONT]
 [FONT=Arial]strHKL = LoadKeyboardLayout(StrPtr(mstrLangID), KLF_SUBSTITUTE_OK)[/FONT]
 [FONT=Arial]If IsNull(strHKL) Then 'Returns NULL if fails.[/FONT]
 [FONT=Arial]    LoadKbLayout = False[/FONT]
 [FONT=Arial]    MsgBox "Error in LoadChineseKbLayout: " & GetAPIErrorMessage(Err.LastDLLError)[/FONT]
 [FONT=Arial]Else[/FONT]
 [FONT=Arial]    LoadKbLayout = True[/FONT]
 [FONT=Arial]    mlngChinese_HKL = CLng(Val(strHKL)) 'Store HKL.[/FONT]
 [FONT=Arial]End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Private Function StorePreviousKbLayoutHKL()[/FONT]
 [FONT=Arial]'Stores current HKL to be used to retreive previous keyboard layout.[/FONT]
 [FONT=Arial]    mlngPrevious_HKL = GetCurrentKbLayoutHKL[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]#If Win64 = 1 And VBA7 = 1 Then[/FONT]
 [FONT=Arial]    Private Function ActivateKbLayout(lngHKL As LongPtr)[/FONT]
 [FONT=Arial]    Dim lngReturnHKL As LongPtr[/FONT]
 [FONT=Arial]#Else[/FONT]
 [FONT=Arial]    Private Function ActivateKbLayout(lngHKL As Long)[/FONT]
 [FONT=Arial]    Dim lngReturnHKL As Long[/FONT]
 [FONT=Arial]#End If[/FONT]
 
 [FONT=Arial]'Load keyboard.[/FONT]
 [FONT=Arial]lngReturnHKL = ActivateKeyboardLayout(lngHKL, 0)[/FONT]
 [FONT=Arial]'Test if successful.[/FONT]
 [FONT=Arial]If Not (GetCurrentKbLayoutHKL = lngHKL) And (lngHKL = mlngChinese_HKL) Then[/FONT]
 [FONT=Arial]    'Try again![/FONT]
 [FONT=Arial]    LoadKbLayout[/FONT]
 [FONT=Arial]    lngReturnHKL = ActivateKbLayout(lngHKL)[/FONT]
 [FONT=Arial]    If Not (GetCurrentKbLayoutHKL = lngHKL) Then[/FONT]
 [FONT=Arial]        MsgBox "Error in ActivateChineseKdLayout: " & GetAPIErrorMessage(Err.LastDLLError)[/FONT]
 [FONT=Arial]    End If[/FONT]
 [FONT=Arial]End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]#If Win64 = 1 And VBA7 = 1 Then[/FONT]
 [FONT=Arial]    Private Function GetCurrentKbLayoutHKL() As LongPtr[/FONT]
 [FONT=Arial]#Else[/FONT]
 [FONT=Arial]    Private Function GetCurrentKbLayoutHKL() As Long[/FONT]
 [FONT=Arial]#End If[/FONT]
 [FONT=Arial]'Retrieves the handle to the keyboard layout (HKL) for the current thread.[/FONT]
 [FONT=Arial]'The bottom half is the LANGID, the top half is defined by the USED SUBSYSTEM to[/FONT]
 [FONT=Arial]'uniquely identify the keyboard layout. Note HKL is actually a handle to an input[/FONT]
 [FONT=Arial]'method.[/FONT]
 [FONT=Arial]GetCurrentKbLayoutHKL = GetKeyboardLayout(0) 'store[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Private Function GetCurrentKbLayoutID() As String[/FONT]
 [FONT=Arial]'Retrieves the current Keyboard Layout Identifier (KLID).[/FONT]
 [FONT=Arial]'Note the bottom half is the LANGID. Top half is device specific.[/FONT]
 [FONT=Arial]Dim strKLID As String[/FONT]
 [FONT=Arial]strKLID = String$(KL_NAMELENGTH, 0) 'Buffer[/FONT]
 [FONT=Arial]GetKeyboardLayoutName StrPtr(strKLID)[/FONT]
 [FONT=Arial]GetCurrentKbLayoutID = strKLID[/FONT]
 [FONT=Arial]End Function[/FONT][/FONT]
In the second module place this. It's just error handling for the API calls.

Code:
[FONT=Arial][FONT=Arial]Option Private Module[/FONT]
 [FONT=Arial]Option Compare Database[/FONT]
 [FONT=Arial]Option Explicit[/FONT]
 [FONT=Arial]'Modified from Access 2010 Programmer's Reference.[/FONT]
 
 [FONT=Arial]Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000[/FONT]
 
 [FONT=Arial]#If Win64 = 1 And VBA7 = 1 Then[/FONT]
 [FONT=Arial]    Declare PtrSafe Function FormatMessage Lib "kernel32" Alias "FormatMessageW" _[/FONT]
 [FONT=Arial]    (ByVal dwFlags As Long, lpSource As Long, ByVal dwMessageId As Long, _[/FONT]
 [FONT=Arial]     ByVal dwLanguageId As Long, ByVal lpBuffer As LongPtr, ByVal nSize As Long, Arguments As Any) As Long[/FONT]
 [FONT=Arial]#ElseIf VBA6 = 1 Then[/FONT]
 [FONT=Arial]    Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageW" _[/FONT]
 [FONT=Arial]    (ByVal dwFlags As Long, lpSource As Long, ByVal dwMessageId As Long, _[/FONT]
 [FONT=Arial]     ByVal dwLanguageId As Long, ByVal lpBuffer As Long, ByVal nSize As Long, Arguments As Any) As Long[/FONT]
 [FONT=Arial]#End If[/FONT]
 
 [FONT=Arial]Public Function GetAPIErrorMessage(lngError As Long) As String[/FONT]
 [FONT=Arial]    Dim strMessage As String[/FONT]
 [FONT=Arial]    Dim lngReturn  As Long[/FONT]
 [FONT=Arial]    Dim nSize      As Long[/FONT]
 
 [FONT=Arial]    strMessage = Space(256)[/FONT]
 [FONT=Arial]    nSize = Len(strMessage)[/FONT]
 [FONT=Arial]    lngReturn = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, _[/FONT]
 [FONT=Arial]        lngError, 0, StrPtr(strMessage), nSize, 0)[/FONT]
 [FONT=Arial]    If lngReturn > 0 Then[/FONT]
 [FONT=Arial]        GetAPIErrorMessage = Replace(Left(strMessage, lngReturn), vbCrLf, "")[/FONT]
 [FONT=Arial]    Else[/FONT]
 [FONT=Arial]        GetAPIErrorMessage = "Error not found."[/FONT]
 [FONT=Arial]    End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Public Function LoWord(dw As Long) As Integer[/FONT]
 [FONT=Arial]    'Retrieves the low-order word from the given 32-bit value. Provided by Allen Browne.[/FONT]
 [FONT=Arial]    If dw And &H8000& Then[/FONT]
 [FONT=Arial]        LoWord = dw Or &HFFFF0000[/FONT]
 [FONT=Arial]    Else[/FONT]
 [FONT=Arial]        LoWord = dw And &HFFFF&[/FONT]
 [FONT=Arial]    End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Public Function HiWord(dw As Long) As Integer[/FONT]
 [FONT=Arial]    'Retrieves the high-order word from the given 32-bit value. Provided by Allen Browne.[/FONT]
 [FONT=Arial]    HiWord = (dw And &HFFFF0000) \ &H10000[/FONT]
 [FONT=Arial]End Function[/FONT]
 [/FONT]
Use the SetLanguage function to specify the language you want to switch to. Then call SetKbLayout to actually switch the language. When you switch to a Chinese language, the associated IME (input method editor) will also activate. This is an example of switching to US English when a form loads:

Code:
 Private Sub Form_Load()
    SetLanguage ("00000409")
    SetKbLayout
End Sub
I've attached a sample Access 2010 database.

Hope this helps.

Greg
 

Attachments

Hi GreqZ,

It is exciting to have received your response. Definitely I will give a trial and come back with the result. Thank you very much!
 
You're welcome Scott. Any problems let me know.

Greg
 
here some info from the help file:
You can use the KeyboardLanguage property to specify or determine the keyboard language on entry into a control

You can set this property by using the property sheet or Visual Basic.

Valid values for this property are 0 (zero), which corresponds to the default system language, or plid + 2 where plid is the primary language ID of a language installed on the current system. For example, the primary language ID of English is 9, so the corresponding KeyboardLanguage setting is 11. For a list of languages and their primary language IDs, search for "Primary Language IDs" in the MSDN Web site. (An exception to this list is Traditional Chinese which is represented by the value 200.)

Setting this property to a language that is not installed may either have no effect or cause an error.



Hope it will help
Tal
 
Hi Scott,

I have an application where I need to switch between Chinese and English entry, so developed the code to do this. It is capable of switching between any languages. First I'll list the code, then explain how to use it.

'You'll need to create 2 standard modules. In the first place this:

Code:
[FONT=Arial][FONT=Arial]Option Private Module[/FONT]
 [FONT=Arial]Option Compare Database[/FONT]
 [FONT=Arial]Option Explicit[/FONT]
 
 [FONT=Arial]Private Const KL_NAMELENGTH As Long = 9 'HKL string buffer length.[/FONT]
 [FONT=Arial]Private Const KLF_SUBSTITUTE_OK As Long = &H2 'Allow locale substitutions. Default.[/FONT]
 
 [FONT=Arial]Private mstrLangID As String[/FONT]
 
 [FONT=Arial]#If Win64 = 1 And VBA7 = 1 Then[/FONT]
 [FONT=Arial]    Dim mlngChinese_HKL As LongPtr  'Handle to Chinese input method.[/FONT]
 [FONT=Arial]    Dim mlngDefault_HKL As LongPtr  'Handle to default input method.[/FONT]
 [FONT=Arial]    Dim mlngPrevious_HKL As LongPtr 'Use to store previous HKL.[/FONT]
 [FONT=Arial]    Private Declare PtrSafe Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameW" _[/FONT]
 [FONT=Arial]                        (ByVal pwszKLID As LongPtr) As Long[/FONT]
 [FONT=Arial]    Private Declare PtrSafe Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutW" _[/FONT]
 [FONT=Arial]                        (ByVal pwszKLID As LongPtr, ByVal Flags As Long) As LongPtr[/FONT]
 [FONT=Arial]    Private Declare PtrSafe Function GetKeyboardLayout Lib "user32" (ByVal idThread As Long) As LongPtr[/FONT]
 [FONT=Arial]    Private Declare PtrSafe Function ActivateKeyboardLayout Lib "user32" _[/FONT]
 [FONT=Arial]                    (ByVal HKL As LongPtr, ByVal Flags As Long) As LongPtr[/FONT]
 [FONT=Arial]#Else[/FONT]
 [FONT=Arial]    Dim mlngChinese_HKL As Long  'Handle to Chinese input method.[/FONT]
 [FONT=Arial]    Dim mlngDefault_HKL As Long  'Handle to default input method.[/FONT]
 [FONT=Arial]    Dim mlngPrevious_HKL As Long 'Use to store previous HKL.[/FONT]
 [FONT=Arial]    Private Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameW" _[/FONT]
 [FONT=Arial]                        (ByVal pwszKLID As Long) As Long[/FONT]
 [FONT=Arial]    Private Declare Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutW" _[/FONT]
 [FONT=Arial]                        (ByVal pwszKLID As Long, ByVal Flags As Long) As Long[/FONT]
 [FONT=Arial]    Private Declare Function GetKeyboardLayout Lib "user32" (ByVal idThread As Long) As Long[/FONT]
 [FONT=Arial]    Private Declare Function ActivateKeyboardLayout Lib "user32" _[/FONT]
 [FONT=Arial]                        (ByVal HKL As Long, ByVal Flags As Long) As Long[/FONT]
 [FONT=Arial]#End If[/FONT]
 
 [FONT=Arial]Public Function SetLanguage(strLCID As String)[/FONT]
 [FONT=Arial]'USE THIS FUNCTION TO CHOOSE LANGUAGE TO SET[/FONT]
 [FONT=Arial]' example:[/FONT]
 [FONT=Arial]'       SetLanguage ("00000409")[/FONT]
 [FONT=Arial]' This function only loads the desired language ID into memory. You must follot with a call to SetKbLayout.[/FONT]
 
 [FONT=Arial]    ' Chinese Locale ID values:[/FONT]
 [FONT=Arial]    ' 00000804 - Chinese Simplified, PRC[/FONT]
 [FONT=Arial]    ' 00001004 - Chinese Simplified, Singapore[/FONT]
 [FONT=Arial]    ' 00000404 - Chinese Traditional, Taiwan[/FONT]
 [FONT=Arial]    ' 00000C04 - Chinese Traditional, Hong Kong S.A.R.[/FONT]
 [FONT=Arial]    ' 00001404 - Chinese Traditional, Macau S.A.R.[/FONT]
 [FONT=Arial]    '[/FONT]
 [FONT=Arial]    ' English Locale ID values (not complete list):[/FONT]
 [FONT=Arial]    ' 00000409 - United States[/FONT]
 [FONT=Arial]    ' 00000809 - United Kingdom[/FONT]
 [FONT=Arial]    ' 00000C09 - Australia[/FONT]
 [FONT=Arial]    ' 00001009 - Canada[/FONT]
 [FONT=Arial]    ' 00004009 - India[/FONT]
 
 [FONT=Arial]    mstrLangID = strLCID & ChrW$(0) 'Null terminated.[/FONT]
 [FONT=Arial]    LoadKbLayout[/FONT]
 
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Public Function SetKbLayout()[/FONT]
 [FONT=Arial]'USE THIS FUNCTION TO ACTIVATE A NEW LANGUAGE.[/FONT]
 [FONT=Arial]' example:[/FONT]
 [FONT=Arial]'       SetKbLayout[/FONT]
 [FONT=Arial]' You must call SetLanguage prior to calling SetKbLayout.[/FONT]
 
 [FONT=Arial]If Not (GetCurrentKbLayoutHKL = mlngChinese_HKL) Then[/FONT]
 [FONT=Arial]    'Remember previous HKL.[/FONT]
 [FONT=Arial]    StorePreviousKbLayoutHKL[/FONT]
 [FONT=Arial]    'Change keyboard layout.[/FONT]
 [FONT=Arial]    ActivateKbLayout mlngChinese_HKL[/FONT]
 [FONT=Arial]End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Public Function RecallPreviousKbLayout()[/FONT]
 [FONT=Arial]ActivateKbLayout mlngPrevious_HKL[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Private Function LoadKbLayout() As Boolean[/FONT]
 
 [FONT=Arial]Dim strHKL As String[/FONT]
 
 [FONT=Arial]strHKL = String$(KL_NAMELENGTH, 0) 'Buffer.[/FONT]
 [FONT=Arial]strHKL = LoadKeyboardLayout(StrPtr(mstrLangID), KLF_SUBSTITUTE_OK)[/FONT]
 [FONT=Arial]If IsNull(strHKL) Then 'Returns NULL if fails.[/FONT]
 [FONT=Arial]    LoadKbLayout = False[/FONT]
 [FONT=Arial]    MsgBox "Error in LoadChineseKbLayout: " & GetAPIErrorMessage(Err.LastDLLError)[/FONT]
 [FONT=Arial]Else[/FONT]
 [FONT=Arial]    LoadKbLayout = True[/FONT]
 [FONT=Arial]    mlngChinese_HKL = CLng(Val(strHKL)) 'Store HKL.[/FONT]
 [FONT=Arial]End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Private Function StorePreviousKbLayoutHKL()[/FONT]
 [FONT=Arial]'Stores current HKL to be used to retreive previous keyboard layout.[/FONT]
 [FONT=Arial]    mlngPrevious_HKL = GetCurrentKbLayoutHKL[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]#If Win64 = 1 And VBA7 = 1 Then[/FONT]
 [FONT=Arial]    Private Function ActivateKbLayout(lngHKL As LongPtr)[/FONT]
 [FONT=Arial]    Dim lngReturnHKL As LongPtr[/FONT]
 [FONT=Arial]#Else[/FONT]
 [FONT=Arial]    Private Function ActivateKbLayout(lngHKL As Long)[/FONT]
 [FONT=Arial]    Dim lngReturnHKL As Long[/FONT]
 [FONT=Arial]#End If[/FONT]
 
 [FONT=Arial]'Load keyboard.[/FONT]
 [FONT=Arial]lngReturnHKL = ActivateKeyboardLayout(lngHKL, 0)[/FONT]
 [FONT=Arial]'Test if successful.[/FONT]
 [FONT=Arial]If Not (GetCurrentKbLayoutHKL = lngHKL) And (lngHKL = mlngChinese_HKL) Then[/FONT]
 [FONT=Arial]    'Try again![/FONT]
 [FONT=Arial]    LoadKbLayout[/FONT]
 [FONT=Arial]    lngReturnHKL = ActivateKbLayout(lngHKL)[/FONT]
 [FONT=Arial]    If Not (GetCurrentKbLayoutHKL = lngHKL) Then[/FONT]
 [FONT=Arial]        MsgBox "Error in ActivateChineseKdLayout: " & GetAPIErrorMessage(Err.LastDLLError)[/FONT]
 [FONT=Arial]    End If[/FONT]
 [FONT=Arial]End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]#If Win64 = 1 And VBA7 = 1 Then[/FONT]
 [FONT=Arial]    Private Function GetCurrentKbLayoutHKL() As LongPtr[/FONT]
 [FONT=Arial]#Else[/FONT]
 [FONT=Arial]    Private Function GetCurrentKbLayoutHKL() As Long[/FONT]
 [FONT=Arial]#End If[/FONT]
 [FONT=Arial]'Retrieves the handle to the keyboard layout (HKL) for the current thread.[/FONT]
 [FONT=Arial]'The bottom half is the LANGID, the top half is defined by the USED SUBSYSTEM to[/FONT]
 [FONT=Arial]'uniquely identify the keyboard layout. Note HKL is actually a handle to an input[/FONT]
 [FONT=Arial]'method.[/FONT]
 [FONT=Arial]GetCurrentKbLayoutHKL = GetKeyboardLayout(0) 'store[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Private Function GetCurrentKbLayoutID() As String[/FONT]
 [FONT=Arial]'Retrieves the current Keyboard Layout Identifier (KLID).[/FONT]
 [FONT=Arial]'Note the bottom half is the LANGID. Top half is device specific.[/FONT]
 [FONT=Arial]Dim strKLID As String[/FONT]
 [FONT=Arial]strKLID = String$(KL_NAMELENGTH, 0) 'Buffer[/FONT]
 [FONT=Arial]GetKeyboardLayoutName StrPtr(strKLID)[/FONT]
 [FONT=Arial]GetCurrentKbLayoutID = strKLID[/FONT]
 [FONT=Arial]End Function[/FONT][/FONT]
In the second module place this. It's just error handling for the API calls.

Code:
[FONT=Arial][FONT=Arial]Option Private Module[/FONT]
 [FONT=Arial]Option Compare Database[/FONT]
 [FONT=Arial]Option Explicit[/FONT]
 [FONT=Arial]'Modified from Access 2010 Programmer's Reference.[/FONT]
 
 [FONT=Arial]Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000[/FONT]
 
 [FONT=Arial]#If Win64 = 1 And VBA7 = 1 Then[/FONT]
 [FONT=Arial]    Declare PtrSafe Function FormatMessage Lib "kernel32" Alias "FormatMessageW" _[/FONT]
 [FONT=Arial]    (ByVal dwFlags As Long, lpSource As Long, ByVal dwMessageId As Long, _[/FONT]
 [FONT=Arial]     ByVal dwLanguageId As Long, ByVal lpBuffer As LongPtr, ByVal nSize As Long, Arguments As Any) As Long[/FONT]
 [FONT=Arial]#ElseIf VBA6 = 1 Then[/FONT]
 [FONT=Arial]    Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageW" _[/FONT]
 [FONT=Arial]    (ByVal dwFlags As Long, lpSource As Long, ByVal dwMessageId As Long, _[/FONT]
 [FONT=Arial]     ByVal dwLanguageId As Long, ByVal lpBuffer As Long, ByVal nSize As Long, Arguments As Any) As Long[/FONT]
 [FONT=Arial]#End If[/FONT]
 
 [FONT=Arial]Public Function GetAPIErrorMessage(lngError As Long) As String[/FONT]
 [FONT=Arial]    Dim strMessage As String[/FONT]
 [FONT=Arial]    Dim lngReturn  As Long[/FONT]
 [FONT=Arial]    Dim nSize      As Long[/FONT]
 
 [FONT=Arial]    strMessage = Space(256)[/FONT]
 [FONT=Arial]    nSize = Len(strMessage)[/FONT]
 [FONT=Arial]    lngReturn = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, _[/FONT]
 [FONT=Arial]        lngError, 0, StrPtr(strMessage), nSize, 0)[/FONT]
 [FONT=Arial]    If lngReturn > 0 Then[/FONT]
 [FONT=Arial]        GetAPIErrorMessage = Replace(Left(strMessage, lngReturn), vbCrLf, "")[/FONT]
 [FONT=Arial]    Else[/FONT]
 [FONT=Arial]        GetAPIErrorMessage = "Error not found."[/FONT]
 [FONT=Arial]    End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Public Function LoWord(dw As Long) As Integer[/FONT]
 [FONT=Arial]    'Retrieves the low-order word from the given 32-bit value. Provided by Allen Browne.[/FONT]
 [FONT=Arial]    If dw And &H8000& Then[/FONT]
 [FONT=Arial]        LoWord = dw Or &HFFFF0000[/FONT]
 [FONT=Arial]    Else[/FONT]
 [FONT=Arial]        LoWord = dw And &HFFFF&[/FONT]
 [FONT=Arial]    End If[/FONT]
 [FONT=Arial]End Function[/FONT]
 
 [FONT=Arial]Public Function HiWord(dw As Long) As Integer[/FONT]
 [FONT=Arial]    'Retrieves the high-order word from the given 32-bit value. Provided by Allen Browne.[/FONT]
 [FONT=Arial]    HiWord = (dw And &HFFFF0000) \ &H10000[/FONT]
 [FONT=Arial]End Function[/FONT]
 [/FONT]
Use the SetLanguage function to specify the language you want to switch to. Then call SetKbLayout to actually switch the language. When you switch to a Chinese language, the associated IME (input method editor) will also activate. This is an example of switching to US English when a form loads:

Code:
 Private Sub Form_Load()
    SetLanguage ("00000409")
    SetKbLayout
End Sub
I've attached a sample Access 2010 database.

Hope this helps.

Greg

Hi Greg,

I tried the demo on two NB computers: one with US English & English keyboard and another one US English & English keyboard or Chinese Traditional Taiwan. Open the form, select the language and highlight the field and enter the text. Unfortunately it won't work on both NBs at all. Do I need to set up anything on each NB?
 
here some info from the help file:
You can use the KeyboardLanguage property to specify or determine the keyboard language on entry into a control

You can set this property by using the property sheet or Visual Basic.

Valid values for this property are 0 (zero), which corresponds to the default system language, or plid + 2 where plid is the primary language ID of a language installed on the current system. For example, the primary language ID of English is 9, so the corresponding KeyboardLanguage setting is 11. For a list of languages and their primary language IDs, search for "Primary Language IDs" in the MSDN Web site. (An exception to this list is Traditional Chinese which is represented by the value 200.)

Setting this property to a language that is not installed may either have no effect or cause an error.



Hope it will help
Tal

Hi Tal,

Thanks for you info. The users would like to type in English in most case. By default, the NBs use English, not Chinese. Occasionally, the user switches to Chinese input by pressing Shift + Alt. When launch the database, the would like to use English by default but now the database switches it to Chinese by default automatically if the NB is set up for both English or Chinese input. I am at loss why the database does it.
 
set the KeyboardLanguage property of every TextBox to english.
The default setting is System. This cause it to jump back to Chinese on Chinese systems.
 
The demo was set up for "Chinese Simplified, PRC", which has a Locale ID of 00000804. For "Chinese Traditional, Taiwan" the Locale ID is 00000404.

So, in the code behind the form "tblSomeData", under the cmdChinese_Click() sub,
change this line from:

SetLanguage ("00000804")
to
SetLanguage ("00000404").

The code was failing because it couldn't find the Chinese Simplified, PRC keyboard on the computers that you ran the demo on.

With that said, the approach smig is suggesting is much simpler.
 

Users who are viewing this thread

Back
Top Bottom