I have to validate numeric entry and the enter key press in 8 text boxes. After a number is entered and/or the enter or tab key is pressed, focus is set to the next text box. I wrote a KeyPress sub routine and validation works fine. I'd like to call the KeyPress sub routine as a function instead of copying and pasting it into 8 text box KeyPress sub routines. The challenge is that the next text box name is used to set the focus such that after validation, the Sub Text1_Keypress routine sets the focus to Text2 then the Sub Text2_Keypress routine sets the focus to Text3... etc. I'd like to name it Sub Validate_Entry_KeyPress and call it in each text box Sub Keypress routine instead of pasting the code 8 times but I've painted myself into the corner or text box 1. Any advice greatly appreciated.
Code:
Option Explicit
Sub Text1_KeyPress(KeyAscii as integer)
If KeyAscii = 13 Then
Text2.SetFocus
ElseIf KeyAscii < 48 or KeyAscii > 57 Then
Msgbox “Enter a number greater than zero”, , “”
KeyAscii = 0
End If
End Sub