Change case on exit

On a form, I want to reformat all characters to CAPS on exit from the field.

I have done "only allow caps" previously, but this allows you to cut and paste lower case, so on exit, I want to set the field to all caps.  That way if someone does a {CTRL}-{V}, the result is the same.
Who is Participating?
You could use this in the Exit Event of a textbox:

Me.txtbox = UCase("" & Me.txtBox)

Open in new window

Or for more immediate results, use the Change Event of the etxtbox, using the Text Property:

Me.txtbox.Text = UCase("" & Me.txtBox.Text)

Open in new window

See the following:
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Private Sub YourTextBox_Exit(Cancel as Integer)

' Convert the contents of the text box to all upper case
Me.YourTextBox = UCase(Me.YourTextBox)

Exit Sub
The Exit (or another) event is probably your best option.  The change event suggestion I posted has some disturbing cursor effects which can be improved, but not totally fixed by moving the cursor to the end of the text like this:

Me.txtBox.Text = UCase("" & Me.txtBox.Text)
Me.txtBox.SelStart = Len(Me.txtBox.Text)
Me.txtBox.SelLength = 0

Open in new window

Dale FyeCommented:
Another way to address this, if you need this for many fields is to create a global function (as below), and then in the Exit event of the control, (see image) enter: =fnUCASE()
Calling a function from the On Exit event
Public Function fnUCASE() As String

    Dim ctrl As Control
    Dim strVal As String
    Set ctrl = Screen.ActiveControl
    If ctrl.ControlType = acTextBox Then
        If Not IsNull(ctrl.Text) Then ctrl.Text = UCase(ctrl.Text)
    End If
End Function

Open in new window

TIgerVAuthor Commented:
Thank you!
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.