Found myself using numeric keypad when NumLock was off. Of course I wasn't aware of that and was going nuts over why cursor was jumping around and couldn't enter value. Doh!
So went hunting for code to programmatically control NumLock state. Found two, neither fully works.
http://www.tek-tips.com/viewthread.c...9487&page=1987
http://c85.cemi.rssi.ru/Access/Queri...l.asp?TipID=36
Debug shows that the state value is changed from off to on but the keyboard isn't actually changed. The NumLock light stays off and key entry behavior isn't altered. Here is how I implemented code from the second link.
Code:
'following used with code to set NumLock on keyboard
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Private Sub SetKeyState(intKey As Integer, fTurnOn As Boolean)
' Retrieve the keyboard state, set the particular
' key in which you're interested, and then set
' the entire keyboard state back the way it
' was, with the one key altered.
Dim abytBuffer(0 To 255) As Byte
GetKeyboardState abytBuffer(0)
abytBuffer(intKey) = CByte(Abs(fTurnOn))
SetKeyboardState abytBuffer(0)
End Sub
Private Sub Form_Load()
' Return or set the Numlock toggle.
Call SetKeyState(vbKeyNumlock, True)
End Sub
Found another, SendKeys works but it wants to add record to my UserComments table. That's weird but it does toggle the NumLock. Problem is if NumLock is on it will be turned off.
http://www.ozgrid.com/forum/showthre...t=13239&page=1
Well, this works, and with the True argument no longer writes record to table.
Code:
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Sub Form_Load()
If GetKeyState(vbKeyNumlock) = 1 Then
'set the Numlock on
SendKeys "NumLock", True
End If
End Sub
I spoke too soon. Additional testing shows that this still does not work as expected. My goal was to make sure the NumLock is turned on when attempting entry into a particular textbox. So I changed this code to the GotFocus event for that control and everything fell apart. Even tries to write that record again. I give up.
What I really wanted was code that would detect key press from the numeric keypad but everything I read says VBA can't distinguish whether the ASCII is from keyboard or keypad. In fact, when NumLock is off, the number 5 on keypad sends no ASCII at all.