Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check for a space in a text input box

Status
Not open for further replies.

jimboby

Technical User
Aug 21, 2003
1
US
Can any one help?

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?

Any help greatly appreciated.

 
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 &quot;Don't enter any spaces&quot;
newStr = Left(myStr, spFind - 1)
textboxname.Text = newStr
Else
End If
Else
End If
HTH
~Geoff~
[noevil]
 
Jimboby,

Here is a another version of Geoff's idea:

Code:
Private Sub TextBox1_Change()
  With TextBox1
    If InStr(1, .Text, Chr(32), vbTextCompare) <> 0 Then
      MsgBox &quot;No spaces allowed.&quot;, vbOKOnly + vbExclamation, &quot;Input Error&quot;
      .Text = Left(.Text, Len(.Text) - 1)
    End If
  End With
End Sub

Of course you would replace TextBox1 with the name of your textbox.

Regards,
M. Smith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top