Hi,
I have a textBox for the user to key in their name. Please help me how do remove or avoid the double spaces between words.
Thanks !
Vivian
Printable View
Hi,
I have a textBox for the user to key in their name. Please help me how do remove or avoid the double spaces between words.
Thanks !
Vivian
First, add this code to your project...it will remove all instances of 2 or more spaces, so there will only be 1 space.
VB Code:
Function SingleSpaces(ByVal TextString As String) As String Dim lonLen As Long, lonLoop As Long Dim strCur As String, strNext As String Dim strTemp As String, strRet As String strTemp = TextString lonLen = Len(strTemp) If lonLen = 0 Then Exit Function For lonLoop = 1 To lonLen strCur = Mid$(strTemp, lonLoop, 1) If strCur = " " Then strNext = Mid$(strTemp, lonLoop + 1, 1) If Not strNext = " " Then strRet = strRet & strCur End If Else strRet = strRet & strCur End If Next lonLoop SingleSpaces = strRet End Function
Then, you can remove all spaces by using the code:
VB Code:
TextBox.Text = SingleSpaces(TextBox.Text)
Where TextBox is the name of the textbox the user enters their name into.
VB Code:
Option Explicit Private Sub Command_Click() If InStr(Text.Text, " ") Then MsgBox "instr" Text.Text = Replace(Text.Text, " ", " ") End If End Sub
Hi,
Thanks for the help, but the problem is when the user press the space bar twice, the cursor will move back to the beginning of the words. How to solve the problem ? It has to avoid the user to press the spaces bar more than once..
vivian
VB Code:
Private Sub txtName_KeyPress(KeyAscii As Integer) If KeyAscii = 32 Then If Len(Text1.Text) > 0 Then If Right$(Text1.Text, 1) = " " Then KeyAscii = 0 End If End If End If End Sub
Thank you. Thats exactly what I want. :wave:
:thumb:
vivian