Trim a validation rule?

I have a form that has an email field.  I have a validation rule of:

Is Null Or ((Like "*?@?*.?*") And (Not Like "*[ ,;]*"))

to help ensure the email is entered fairly accurately.  For the most part it works great.  However if a user pastes text into the field that has a space at the start of the text the validation rule fails.

Is there a way to incorporate trim into the validation rule or perhaps run after update VBA code to validate the form and trim it instead?
thandelAsked:
Who is Participating?
 
Gustav BrockConnect With a Mentor CIOCommented:
You can add this code in the OnChange event to clean up the pasted address before it will be evaluated:

Private Sub Email_Change()

    If Asc(Me!Email.Text) = 32 Then
        Me!Email.Text = Trim(Me!Email.Text)
    End If
    
End Sub

Open in new window

0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
hi,

a Validation Rule just validates data, it cannot change it.

Normally, one would use a form to enter data, so there could be AfterUpdate event for the control, or BeforeUpdate event for the form, that trims the entry

You could also validate with an event too

have an awesome day,
crystal
0
 
Dale FyeCommented:
I tend to not use validation rules on fields and use the Form_BeforeUpdate event to test for valid entries in all fields, similar to Crystal's comment above.  I've found that validation rules can be difficult to format properly and get the effect you want, but putting VBA in the Form_BeforeUpdate event allows me to test for all of the required fields, and validation in a single place.  It also allows you to manipulate text (like trimming that leading space).
0
 
Gustav BrockCIOCommented:
Solution provided.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.