Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Remove middle of string too
Thomas Gonder 
      
4 years ago
If there are a bunch of spaces in a string, and we want to reduce all extras between some character other than space, is there a built in function to do that?

Example s = "    a    b    c           d   "
we want after the function(s)
?s
a b c d
This is what Trim() does in most BASIC I've worked with, but not with M$.
Adam Schwanz  @Reply  
            
4 years ago
You're just trying to get rid of spaces?

Replace([Field]," ","")
Adam Schwanz  @Reply  
            
4 years ago
If you want to only get rid of EXTRA spaces, that's a little more advanced.

Make a global module
Public Function RemoveSpaces(FixString As Variant) As Variant
  If IsNull(FixString) Then 'handle Null values
    RemoveSpaces = Null
    Exit Function
  End If

  Dim S As String
  S = Replace(FixString, vbCr, " ")
  S = Replace(S, vbLf, " ")
  S = Replace(S, vbTab, " ")
  S = Replace(S, vbVerticalTab, " ")
  S = Replace(S, vbBack, " ")
  S = Replace(S, vbNullChar, " ")
  While InStr(S, "  ") > 0
    S = Replace(S, "  ", " ")
  Wend
  S = Trim(S)
  RemoveSpaces = S
End Function


On your form call the procedure, do something like this
Dim X
X = RemoveSpaces(TextboxName)
TextboxName = X
Thomas Gonder OP  @Reply  
      
4 years ago
Thanks Adam. I wrote the while part last night, but have you been finding those other characters in typical data entry? Shouldn't Access be filtering all those control codes out like they do extra spaces at the end of text in a text box? Do you find you have to do this after every input? Or do you have a way to do all the text boxes just before the record is saved, and from what event do you call it?
Thomas Gonder OP  @Reply  
      
4 years ago
Was that somewhere in the "code vault"? If so, how could I have found it?
Adam Schwanz  @Reply  
            
4 years ago
Well, it's not always at the end of a text box. The code searches through everything because you had extra spaces everywhere, not just at the end of the field, it could be in the middle of the text or the beginning, in which case access would leave it alone.

I would just call the procedure on the after update event of the field it's checking, or go through everything on the on close event of the form. I was under the impression this was just for a certain field that imports weird or something. If this is a problem on all of your fields, is it user error? Or you just want to apply it to everything just to keep it looking one way? I have a global mod I made a while ago around here that can set global settings in the on current/on close event, if you want to do this on just everything just do it, that may be easier.
Adam Schwanz  @Reply  
            
4 years ago
Not in the code vault, in the brain vault (and a little google assistance LOL)
Thomas Gonder OP  @Reply  
      
4 years ago
I was wondering, in your experience, if users are entering these control codes in through text boxes on forms. Yea, I expect weird stuff in imports.
If users can enter these control codes somehow, then it would be best to check the record for all entered data just before save and get rid of them. I'm not sure what you meant by "set global settings in the on current/on close event".

I haven't done it yet, but I'm guessing there's a way to loop through all the controls in a form and If...Then those that are text box or contain entered user data, to do RemoveSpaces() on each.  I'm sure Richard must have touched on this somewhere by now, but with only one-word searches being effective...
Kevin Robertson  @Reply  
          
4 years ago
Thomas Gonder OP  @Reply  
      
4 years ago
It took me a minute:
You: Well, it's not always at the end of a text box...
Me: I was talking about entering into a text box on a form, not the code you graciously wrote  (or found).

Note: In some BASIC I've worked in, control characters (ANSII dec 0 - 31) are allowed in an input statement while others don't. Has Richard addressed what and what isn't stored in text boxes?
Adam Schwanz  @Reply  
            
4 years ago
Right, imports could have all kinds of issues come up, but even on a text box under normal daily use, the user could press space 5 times or copy and paste something with issues in it anywhere in the text box, beginning middle or ending. Just keeping it in there because there's no harm in it.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/7/2026 12:58:36 AM. PLT: 1s