'In the form code
Private Sub textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox1.KeyPress
Call MyKeyPress(sender, e, strValidChars)
End Sub
'In a module
Public Const strValidChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Public Sub MyKeyPress(ByRef Sender As Object, ByRef e As System.Windows.Forms.KeyPressEventArgs, ByVal ValidChars As String)
Dim TextBox As TextBox
Dim strLastChar As String
TextBox = CType(Sender, TextBox)
If Asc(e.KeyChar) <> Keys.Back Then
If InStr(ValidChars, e.KeyChar) = 0 Then
e.Handled = True
Else
If e.KeyChar.IsLetter(e.KeyChar) = True Then
TextBox.SelectedText = ""
If Not TextBox.TextLength = 0 Then
strLastChar = Mid(TextBox.Text, TextBox.TextLength, 1)
Else
strLastChar = "'"
End If
If strLastChar = " " Or strLastChar = "-" Or strLastChar = "'" Then
TextBox.SelectedText = e.KeyChar.ToUpper(e.KeyChar)
e.Handled = True
End If
End If
End If
End If
End Sub