Daniel Dickover said:
I have an unbound field on a form that I use to make a search SQL statement.
How do I limit that field to only text? For example, in the name "O'brien," the " ' " causes an
error in my SQL statement so I want to limit the field to only letters.
Is it on the keypress event for each letter entered?
Thanks
Dan
Hi Dan...!
You have probably more then one way, i give you two ...!
1)On TAG property write the Limit number es.: 5
On KeyDown TextBox Event Procedure write:
Private Sub TextBoxName_KeyDown(KeyCode As Integer, Shift As Integer)
If Len(Me.TextBoxName.Text) > Me.TextBoxName.Tag - 1 Then KeyCode = 0
End Sub
This second way is not easy, but work better.
2)Always use TAG property to indicate Limit Number
On your TextBox Change Event Procedure
Private Sub txtTest_Change()
Call LimitChars(Me.TextBoxName, Val(TextBoxName.Tag))
End Sub
'******** PUT THIS CODE IN MODULE *********
Private Declare Function SendMessageLong _
Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lngValue As Long) As Long
Private Declare Function GetFocus _
Lib "user32" () As Long
Private Const EM_SETLIMITTEXT As Long = &HC5
Public Sub LimitChars(txt As TextBox, lngLimit As Long)
Dim hwnd As Long
Dim lngResult As Long
Dim lngNewMax As Long
' Get the window handle for the current window.
hwnd = GetFocus()
lngNewMax = Len(txt.Text)
If lngNewMax < lngLimit Then
lngNewMax = lngLimit
End If
' Send the message to the current text box
' to limit itself to lngNewMax characters.
SendMessageLong hwnd, EM_SETLIMITTEXT, lngNewMax, 0
End Sub
Alessandro(IT)