Change case on exit

Posted on 2012-08-14
Last Modified: 2012-08-14
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.
Question by:TIgerV
    LVL 61

    Accepted 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:
    LVL 65

    Expert Comment

    by:Jim Horn
    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
    LVL 61

    Expert Comment

    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

    LVL 47

    Expert Comment

    by:Dale Fye (Access MVP)
    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


    Author Closing Comment

    Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now