Trim leading and trailing spaces in the form

billmark

Board Regular
Joined
Dec 11, 2011
Messages
59
Hi
I've have a form with 15 fields for user to update the data. Sometimes, the user enters with leading or trailing spaces and it creates problem when I use them to lookup. I try the following to trim one of the fields (using AfterUpdate), it fails:
QUOTE
Private Sub F_Name_AfterUpdate()
Me.F_Name = Trim([F_Name])
Do While InStr([F_Name], " ") > 0
Me.F_Name = Replace([F_Name], _
" ", " ", 1, -1, vbTextCompare)
Loop
End Sub

<tbody id="yui_3_16_0_ym19_1_1471391075601_2577" style="-webkit-padding-start: 0px; width: 392px;">
</tbody>
UNQUOTE

Please advise how to fix:
1. how to use one vba to apply to all fields or I need create vba for each in the control
2. some fields in the table do not show trailing spaces but in the form, it shows. I don't know why.

Regards
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Not terribly difficult would be to simply put your trim code in it's own function and call it from each after update event. You can also use a form before update event instead - just one form before update event that way, instead of 15 control after update events. Another approach is given here (for validation, but adaptable for your case): Validate Text Box Entries | Database Solutions for Microsoft Access | databasedev.co.uk

Note, MSAccess has a trim function, doesn't it? As well as an LTrim() function, which I think is what you are re-writing in this case.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,808
Messages
6,162,097
Members
451,741
Latest member
shove

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top