• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Not only accepting space inside the textbox

marreco

Member
Hi, i need verify if user enter (one or more...only)space key in textbox.

User can enter space with text but not only text


Thank you
 

Attachments

  • NotOnlySpaceInTextbox.jpg
    NotOnlySpaceInTextbox.jpg
    84.1 KB · Views: 2
Would be a logic rule similar to
Code:
Dim startVal As String
Dim noSpace As String

startVal = TextBox1.Value
noSpace = Replace(startVal, " ", "")

If Len(startVal) = Len(noSpace) Then
    MsgBox "Need to have at least one space"
ElseIf Len(noSpace) = 0 Then
    MsgBox "Need to have some text"
Else
    MsgBox "Good to go"
    'Proceed with code
End If
 
Hi luke, i need prevent use enter only space in textbox, but user enter with text (a-z 1-9 and space bar key).

Ex1: ( ) <- five space = Only space Not OK
Ex2: (xxxxx) <- five string = OK
 
Below should check if range has only space or no text.
If it has any combination of text and spaces it should pass test.

Replace range with textbox as needed.
Code:
Sub onlySpace()
Dim i, y As Integer
Dim x
y = 0
x = Split(ActiveSheet.Range("A2"), " ")

If UBound(x) = Len(ActiveSheet.Range("A2")) Or Len(ActiveSheet.Range("A2")) = 0 Then
    MsgBox "Need Text"
Else
    MsgBox "Good To Go" 'Your code here
For i = LBound(x) To UBound(x) - 1
    y = y + Len(x(i)) + 1
    x(i) = y
Next i
End If

End Sub
 
On second thought, I was making it unnecessarily complicated...

Code:
Sub Test()
Dim tLen As String

tLen = Len(Trim(ActiveSheet.TextBox1.Text))

If tLen = 0 Then
    MsgBox "Need Text"
Else
    MsgBox "Good to Go"
End If

End Sub
 
Sorry, i use Userform with textbox's.

look picture, please.

Imagine user enter space key in textbox ( ) <- spacebar key (not accept)
but user enter in texbox (text text text) <- accept

in this part has space (text..it's space..text..it's space..text) = OK


Thank you
 
Hi marreco,

I'm not sure you've even tested some of the solutions we gave you. Both chihiro and I presented valid solutions to meet your criteria.

All spaces = Bad
alphanumeric w/ no space = Bad
alphanumeric w/ at least 1 space = Good
 
I do tests but you code cannot allow tip in spacebar key in textbox.

Then strings in side textbox, the sentences are stuck together.

Att
 
User: admin
Password:admin

in Userform "frmCadAva" and textbox "txtObs"

open "frmCadAva" fill....
Data (<- portuguese) =date (dd/MM/yyyy)
Setor (<- portuguese) = Sector etc....you need fill all fields.

When you try fill "txtObs" with space (or multiple spaces), "cmdSalvar " save datas in sheet.
this field "txtObs" (the user must fill with letters, numbers, special characters [spacebar key]), but I must not allow the user to fill space only
 

Attachments

  • ContrAva_ES.xlsb
    926.6 KB · Views: 4
Thanks for the file!

At top of cmdSalvar_Click macro, we can add the check like so
Code:
Private Sub cmdSalvar_Click()
Dim ws As Worksheet
Set ws = Worksheets("BD_Ava")

'NEW CHECK
If Len(Replace(txtObs.Value, " ", "")) = 0 Then
    MsgBox "Must fill in some text"
    Exit Sub
End If
'continue with rest of code
'...
 
Also I see bit of issue where you inserted my first code.
Code:
Private Sub txtObs_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim i, y As Integer
Dim x
y = 0
x = Split(Activesheet.Range("A2"), " ")

If UBound(x) = Len(txtObs.Text) Or Len(txtObs.Text) = 0 Then
    MsgBox "Need Text"
Else
    MsgBox "Good To Go" 'Your code here
For i = LBound(x) To UBound(x) - 1
    y = y + Len(x(i)) + 1
    x(i) = y
Next i
End If

ContadorTextBox
frmCadAva.Repaint
End Sub

Activesheet.Range("A2") should be replaced with txtObs
You'd probably want to replace MsgBox "Need Text" with your code to force the user to enter text.

MsgBox "Good To Go" part can be removed.

Also you have following code which replaces space key stroke with no key stroke. This can be removed.

Code:
Private Sub txtObs_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = vbKeySpace Then
KeyAscii = 0
End If
End Sub
 
Back
Top