Changing the Case of Strings

LCase and UCase Functions

The LCase function converts a string to lower case. The UCase function converts a string to upper case.

Syntax

LCase(string)
UCase(string)
string is a string variable or text inside quotes.

Look at the following example.

Example 1
Dim strText As String
strText = "Miami"
lblCity.Caption = UCase(strText)

What is the result of Example 1?

StrConv Function

The StrConv function converts a string to lower, upper, or title case (first letter of each word is capitalized).

Syntax

StrConv(string, conversion)
string is a string variable or text inside quotes. conversion can be vbUpperCase, vbLowerCase, or vbProperCase (for title case).

Look at the following example.

Example 2
Dim strText As String
strText = "chicago bears"
lblTeam.Caption = StrConv(strText, vbProperCase)

What is the result of Example 2?

Class Exercise: Text Case

We’re going to create program to change the case of a string.

 

  1. Begin by designing the form.
  2. Use the names txtInput, optUpper, optLower, optTitle, lblOutput, cmdDone for the controls on the form.
  3. Add code to cmdDone.
  4. Add the code for optUpper, optLower, and optTitle.

Text Case Code
Private Sub cmdDone_Click()
    ' end program
    Unload frmMain
End Sub

Private Sub optUpper_Click()
‘ convert to upper case
lblOutput.Caption = UCase(txtInput.Text)
End Sub

Private Sub optLower_Click()
‘ convert to lower case
lblOutput.Caption = LCase(txtInput.Text)
End Sub

Private Sub optTitle_Click()
‘ convert to title case
lblOutput.Caption = StrConv(txtInput.Text, vbProperCase)
End Sub

Class Assignment: Text Case 2.0

Modify your program so that as the input changes, the output is updated.

  1. Create a subroutine called ConvertCase().
  2. Make the click event for the option buttons call ConvertCase().
  3. Try your program out to make sure it works.
  4. Raise your hand and ask your teacher to grade your assignment on screen.