andigirlsc
Registered User.
- Local time
- Today, 08:48
- Joined
- Jun 4, 2014
- Messages
- 59
Details
I have a Profile form that tracks the expiration date for each client’s various certifications. These dates are set up in the Short Date format in the table design of Access.
Problem
When a user accidentally presses an alphabetic key while updating an expiration date, an Access error message is triggered. This is confusing to my users as these messages are written in Access lingo. I would rather that nothing occurs at all. I wrote a case statement to disable each letter of the alphabet and applied it to the On Key Down Event for each expiration date control on my Profile form to solve this problem, but this must be applied to 28 separate controls. I would rather call a function that disables alphabetic keys for each date control in my form when called.
Questions
How do I transform my Disable Alphabetic Keys Case Statement into a function that I can call for each expiration date control? I know that when writing a function certain variables have to be declared and/or initialized, but I need help with this.
Also, will I need to create a function to re-enable alphabetic keys or is this unnecessary because the disable alpha keys function will only be called for specific controls, not the entire form?
What I Have Tried
I have tried copying and pasting my Disable Alphabetic Keys Case Statement into a module to attempt to create a function, but it needs work. Any help that can be provided would be greatly appreciated.
Below I have included 2 types of code:
(1) The original On Key Down code applied to each date control on my form
(2) The same code written as an attempt at a function
Original Profile Form Code to Disable Alphabetic Keys in the On Key Down event for each date control
Function to Disable Alphabetic Keys Code
I have a Profile form that tracks the expiration date for each client’s various certifications. These dates are set up in the Short Date format in the table design of Access.
Problem
When a user accidentally presses an alphabetic key while updating an expiration date, an Access error message is triggered. This is confusing to my users as these messages are written in Access lingo. I would rather that nothing occurs at all. I wrote a case statement to disable each letter of the alphabet and applied it to the On Key Down Event for each expiration date control on my Profile form to solve this problem, but this must be applied to 28 separate controls. I would rather call a function that disables alphabetic keys for each date control in my form when called.
Questions
How do I transform my Disable Alphabetic Keys Case Statement into a function that I can call for each expiration date control? I know that when writing a function certain variables have to be declared and/or initialized, but I need help with this.
Also, will I need to create a function to re-enable alphabetic keys or is this unnecessary because the disable alpha keys function will only be called for specific controls, not the entire form?
What I Have Tried
I have tried copying and pasting my Disable Alphabetic Keys Case Statement into a module to attempt to create a function, but it needs work. Any help that can be provided would be greatly appreciated.
Below I have included 2 types of code:
(1) The original On Key Down code applied to each date control on my form
(2) The same code written as an attempt at a function
Original Profile Form Code to Disable Alphabetic Keys in the On Key Down event for each date control
Code:
Private Sub txtCert1ExpDate_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
'All message box text is for me to test the code, not for the user to see
Case vbKeyA
MsgBox ("you pressed the A key")
KeyCode = 0
Case vbKeyB
MsgBox ("you pressed the B key")
KeyCode = 0
Case vbKeyC
MsgBox ("you pressed the C key")
KeyCode = 0
Case vbKeyD
MsgBox ("you pressed the D key")
KeyCode = 0
Case vbKeyE
MsgBox ("you pressed the E key")
KeyCode = 0
Case vbKeyF
MsgBox ("you pressed the F key")
KeyCode = 0
Case vbKeyG
MsgBox ("you pressed the G key")
KeyCode = 0
Case vbKeyH
MsgBox ("you pressed the H key")
KeyCode = 0
Case vbKeyI
MsgBox ("you pressed the I key")
KeyCode = 0
Case vbKeyJ
MsgBox ("you pressed the J key")
KeyCode = 0
Case vbKeyK
MsgBox ("you pressed the K key")
KeyCode = 0
Case vbKeyL
MsgBox ("you pressed the L key")
KeyCode = 0
Case vbKeyM
MsgBox ("you pressed the M key")
KeyCode = 0
Case vbKeyN
MsgBox ("you pressed the N key")
KeyCode = 0
Case vbKeyO
MsgBox ("you pressed the O key")
KeyCode = 0
Case vbKeyP
MsgBox ("you pressed the P key")
KeyCode = 0
Case vbKeyQ
MsgBox ("you pressed the Q key")
KeyCode = 0
Case vbKeyR
MsgBox ("you pressed the R key")
KeyCode = 0
Case vbKeyS
MsgBox ("you pressed the S key")
KeyCode = 0
Case vbKeyT
MsgBox ("you pressed the T key")
KeyCode = 0
Case vbKeyU
MsgBox ("you pressed the U key")
KeyCode = 0
Case vbKeyV
MsgBox ("you pressed the V key")
KeyCode = 0
Case vbKeyW
MsgBox ("you pressed the W key")
KeyCode = 0
Case vbKeyX
MsgBox ("you pressed the X key")
KeyCode = 0
Case vbKeyY
MsgBox ("you pressed the Y key")
KeyCode = 0
Case vbKeyZ
MsgBox ("you pressed the Z key")
KeyCode = 0
End Select
End Sub
Function to Disable Alphabetic Keys Code
Code:
Option Compare Database
Public Function DisableAlphaKeys()
On Error GoTo TheError
'Disable alphabetic keys listed below by setting KeyCode to 0.
'All message box text is for me to test the code, not for the user to see
Select Case KeyDown
Case vbKeyA
MsgBox ("you pressed the A key")
KeyCode = 0
Case vbKeyB
MsgBox ("you pressed the B key")
KeyCode = 0
Case vbKeyC
MsgBox ("you pressed the C key")
KeyCode = 0
Case vbKeyD
MsgBox ("you pressed the D key")
KeyCode = 0
Case vbKeyE
MsgBox ("you pressed the E key")
KeyCode = 0
Case vbKeyF
MsgBox ("you pressed the F key")
KeyCode = 0
Case vbKeyG
MsgBox ("you pressed the G key")
KeyCode = 0
Case vbKeyH
MsgBox ("you pressed the H key")
KeyCode = 0
Case vbKeyI
MsgBox ("you pressed the I key")
KeyCode = 0
Case vbKeyJ
MsgBox ("you pressed the J key")
KeyCode = 0
Case vbKeyK
MsgBox ("you pressed the K key")
KeyCode = 0
Case vbKeyL
MsgBox ("you pressed the L key")
KeyCode = 0
Case vbKeyM
MsgBox ("you pressed the M key")
KeyCode = 0
Case vbKeyN
MsgBox ("you pressed the N key")
KeyCode = 0
Case vbKeyO
MsgBox ("you pressed the O key")
KeyCode = 0
Case vbKeyP
MsgBox ("you pressed the P key")
KeyCode = 0
Case vbKeyQ
MsgBox ("you pressed the Q key")
KeyCode = 0
Case vbKeyR
MsgBox ("you pressed the R key")
KeyCode = 0
Case vbKeyS
MsgBox ("you pressed the S key")
KeyCode = 0
Case vbKeyT
MsgBox ("you pressed the T key")
KeyCode = 0
Case vbKeyU
MsgBox ("you pressed the U key")
KeyCode = 0
Case vbKeyV
MsgBox ("you pressed the V key")
KeyCode = 0
Case vbKeyW
MsgBox ("you pressed the W key")
KeyCode = 0
Case vbKeyX
MsgBox ("you pressed the X key")
KeyCode = 0
Case vbKeyY
MsgBox ("you pressed the Y key")
KeyCode = 0
Case vbKeyZ
MsgBox ("you pressed the Z key")
KeyCode = 0
End Select
TheError:
MsgBox Err.Description
End Function