[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

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.
1 Solution
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!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now