John mc Niel
John mc Niel

Reputation: 1

Prevent spaces in textbox being enterd

Got a textbox in VBA and dont want the usser to be able to type spaces in it can i prevent the usser from doing this with programing?

Upvotes: 0

Views: 7626

Answers (3)

Daniel G. Wilson
Daniel G. Wilson

Reputation: 15055

' Disable Space in TextBox
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 32 Then
        KeyAscii = 0
    End If
End Sub 

http://www.vbforums.com/showthread.php?t=447978

From google :D

Upvotes: 1

Viper
Viper

Reputation: 2236

Maybe this will help you:

You can allow/disallow specific buttons in the KeyPress-Event

Private Sub TextBox1_KeyPress(KeyAscii As Integer)
  Select Case KeyAscii
    Case asc("0") To asc("9"), 8, 32, asc(",")
      'allow signs
    Case Else
       KeyAscii = 0 'forbid everything else
  End Select
End Sub

NOTE: This example allows only numbers to be entered. You need to adapt it for your case.

Here is another page with nearly the same example but in english: LINK

Upvotes: 0

Mr Shoubs
Mr Shoubs

Reputation: 15419

Handle the keypress/change event, check for " " then set to string.empty.

Alternativly use string.replace(" ", string.empty) after the data has been entered

Upvotes: 1

Related Questions