I let the API do this for me. I add this function to the .bas module and call it for any edit control that I only need to set to a number.
Option Explicit Private Const ES_NUMBER = &H2000& Private Const GWL_STYLE = (-16) Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 'set an editbox to numeric only - return the previous 'style on success or zero on error Public Function ForceNumeric(ByVal EditControlhWnd As Long) As Long Dim lngCurStyle As Long Dim lngReturn As Long lngCurStyle = GetWindowLong(EditControlhWnd, GWL_STYLE) If lngCurStyle <> 0 Then lngReturn = SetWindowLong(EditControlhWnd, GWL_STYLE, lngCurStyle Or ES_NUMBER) End If ForceNumeric = lngReturn End Function
To use it, call the function using the TextBox descriptor.
Private Sub Form_Load() Dim lngResult As Long lngResult = ForceNumeric(Text1.hwnd) End Sub
jac
source share