In removing spaces in a string, all you need to do is to use “Trim” and “Replace” function. This is very important if you are dealing with databases.
Application:
If you have an input let say Company Name and you want the Company Name to be unique which mean no duplicate allowed. If you are not going to trim your input the tendency is it will create an error or the “no duplicate rule” will not be applied. Here is the reason why:
Assuming I already have Google Company in my database and I want no duplicate, if I input “_Google_Company_ _” it will also be accepted in the database because by putting spaces in the first and the last part of the string tells that they are not equal. To make it equal you have to use Trim.
Trim$(Input.text)
This will remove spaces both first and last part of the string. Here is the code.
Private Sub Command1_Click()You can download the whole program here.
Text2.Text = LTrim$(Text1.Text) 'This will remove Left space or the first space
Text3.Text = RTrim$(Text1.Text) 'This will remove right space or the last space
Text4.Text = Trim$(Text1.Text) 'This will remove space both first and last
Text5.Text = Replace(Text1.Text, " ", "") 'This will remove all the spaces
End Sub
Screenshot:
Remember, this tutorial is very important in storing and retrieving data from database. This will help you compare two strings properly.
can you make an automatic space remover?