Option Compare Database
Option Explicit
#If VBA7 Then
#If Win64 Then
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare PtrSafe Function GetFocus Lib "user32" () As Long
#Else
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare PtrSafe Function GetFocus Lib "user32" () As Long
#End If
#Else
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function GetFocus Lib "user32" () As Long
#End If
Const EM_SETLIMITTEXT As Long = &HC5
' maybe called from the Change_Event of the textbox
' to limit
Public Function fLimitCharacters(ctl As control, lngLimit As Long)
On Error GoTo Error_Handler
#If Win64 Then
Dim hWnd As LongPtr
#Else
Dim hWnd As Long
#End If
Dim lngResult As Long
Dim lngNewMax As Long
'Get the handle of the current window
hWnd = GetFocus()
lngNewMax = Len(ctl & "")
If lngNewMax < lngLimit Then
lngNewMax = lngLimit
End If
SendMessage hWnd, EM_SETLIMITTEXT, lngNewMax, 0
Exit_Here:
Exit Function
Error_Handler:
MsgBox err.Number & ": " & err.Description
Resume Exit_Here
End Function