I have a vba form with a text box for a field
the text data entered into this box must contain NO spaces
Is there some code that could check this and retun an error message box if a space is inputed by a user?
Use the textbox change event
Dim myStr As String, spFind As Integer, newStr As String
myStr = textboxname.Text
spFind = InStr(myStr, Chr(32))
If Len(myStr) <> 0 Then
If spFind <> 0 Then
MsgBox "Don't enter any spaces"
newStr = Left(myStr, spFind - 1)
textboxname.Text = newStr
Else
End If
Else
End If
HTH
~Geoff~
Private Sub TextBox1_Change()
With TextBox1
If InStr(1, .Text, Chr(32), vbTextCompare) <> 0 Then
MsgBox "No spaces allowed.", vbOKOnly + vbExclamation, "Input Error"
.Text = Left(.Text, Len(.Text) - 1)
End If
End With
End Sub
Of course you would replace TextBox1 with the name of your textbox.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.