How to Trim extra spaces of the Field

G

Guest

I want to remove the extra spaces of the field at the update property of the
same feild.

I tried by trim code but failed.

Private Sub Text18_AfterUpdate()
Me.Text18 = Trim(Me.Text18)
End Sub

Please advise and correct my code

Regards

Irshad
 
D

Douglas J. Steele

What does "but failed" mean in this context? Did you get an error? If so,
what's the error? If you didn't get an error, are you sure that the
characters are, in fact, spaces, and not special characters?

You're not hoping that Trim will remove spaces inside the text, are you?
(Trim only removes spaces from the beginning and end of the string)
 
G

Guest

I understood the trim function wrongly. Its working for the start space.

Actually my requirement is to remove all the extra spaces of the field,
whether it is at start or at end or inbetween. As many times user are posting
data and they press extra space bar inbetween th field which is creating a
problem.

Please advise me some codes which will help me to remove all the extra after
the field update property.

Regards

Irshad
 
R

Rick Brandt

Irshad Alam said:
I understood the trim function wrongly. Its working for the start space.

Actually my requirement is to remove all the extra spaces of the field,
whether it is at start or at end or inbetween. As many times user are posting
data and they press extra space bar inbetween th field which is creating a
problem.

Please advise me some codes which will help me to remove all the extra after
the field update property.

The Replace() function. Use it to replace " " with "".
 
G

Guest

I could not understand, could you please explain in code example, how to to
implement.

Regards

Irshad
 
R

Rick Brandt

Irshad Alam said:
I could not understand, could you please explain in code example, how to to
implement.

Have you looked at the help file? There is a Replace() function. What is does
is find all instances of a string pattern inside a larger string and replaces
them with a different string pattern.

You want to find all instances of a space " " and *replace* them with nothing
"".

EX:
Replace("Feed A Man A Fish", " ", "")

....would return...

"FeedAManAFish"
 
G

Guest

Thanks. I tried it worked. But my situation is as below:

User Some Type like this: John Grath is a Sales Engineer
It want to remove the extra space as: John Grath is a Sales Engineer

I mean one space should be there after every word, to bring a presentable
look in the report or in form.

The replace function removes all the space, which is not suitable for me.

Any further advise on this topic will help me to complete my requirement.

Very best regards.

Irshad
 
D

Douglas J. Steele

Do Until InStr(strMyText, " ") = 0
strMyText = Replace(strMyText, " ", " ")
Loop
 
G

Guest

As advised by you, I tried the below code and its loops Non-Stop. Its not
working.

Private Sub Text18_AfterUpdate()
Do Until InStr(Me.Text18, " ") = 0
Me.Text18 = Replace(Me.Text18, " ", " ")
Loop
End Sub

Pls. correct my code.

Regards.

Irshad
 
D

Douglas J. Steele

In the Do Until statement, there are supposed to be 2 spaces between the
quotes.

In the Replace statement, there are supposed to be 2 spaces between the
first sets of quotes, and 1 space between the second set of quotes.
 
G

Guest

Thank you so much. Done


Douglas J. Steele said:
In the Do Until statement, there are supposed to be 2 spaces between the
quotes.

In the Replace statement, there are supposed to be 2 spaces between the
first sets of quotes, and 1 space between the second set of quotes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top