Microsoft KB Archive/302499

= ACC97: How to Use the StrConv Function to Capitalize Words and Phrases =

Article ID: 302499

Article Last Modified on 1/31/2007

-

APPLIES TO


 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q302499



Moderate: Requires basic macro, coding, and interoperability skills.

For a Microsoft Access 2000 version of this article, see 253911.

For a Microsoft Access 2002 version of this article, see 298607.



SUMMARY
This article provides examples for using a built-in function called StrConv that you can use to capitalize the first character of a word or a set of words. This function is not affected by leading spaces, but it does have the following limitations for some names:
 * Changes &quot;MacDonald&quot; to &quot;Macdonald.&quot;
 * Changes &quot;van Buren&quot; to &quot;Van Buren.&quot;
 * Changes &quot;James Pratt VI&quot; to &quot;James Pratt Vi.&quot;



MORE INFORMATION
The StrConv function can be used to change the case of a string to uppercase, to lowercase, or so that the first letter is uppercase. The syntax is StrConv(string, conversion), where &quot;string&quot; is the text string and &quot;conversion&quot; is 1, 2, or 3. For &quot;conversion&quot;, uppercase is 1, lowercase is 2, and 3 makes the first letter of each word uppercase.

When writing the StrConv function in Visual Basic for Applications (VBA), instead of the integers 1, 2, or 3, you can also use one of the following constants:

vbUpperCase Converts the entire string to uppercase.

vbLowerCase Converts the entire string to lowercase.

vbProperCase Converts the first letter of each word to uppercase and the remaining letters to lowercase.

NOTE: The StrConv function has more constants than just the three that are mentioned here; however, this article focuses only on the three constants that are used for case conversion.

Demonstrating the StrConv Function
 Start Microsoft Access, and then open a new blank database.  Create a new table with the following fields:   Field Name: FirstName Data Type: Text

Field Name: LastName Data Type: Text Save the table as MyNamesList.   Add the following sample names to the table:   john chen joanna fuller becki culbert jeff smith 

Using StrConv in Code in the AfterUpdate Property of a Control
 Create a new form that is based on the MyNamesList table.</li> Add text box controls for the FirstName and LastName fields by dragging the field names from the field list.</li> If the property sheet is not visible, on the View menu, click Properties.</li>  Set the AfterUpdate property of the LastName text box to the following event procedure: Private Sub LastName_AfterUpdate LastName = StrConv(LastName, vbProperCase) End Sub </li> On the File menu, click Close.</li> Open the form that you created in step 1 in Form view, and enter some new names in lowercase. Note that when you return to these records, the LastName field is now correctly capitalized.</li></ol>

Using StrConv in a Query
 Create a new query that is based on the MyNamesList table, and then type the following line in the first Field cell of the query design grid:

FullName: StrConv([LastName] & &quot;, &quot; & [FirstName], 3)

</li> Run the query.

Note that the LastName and FirstName fields are concatenated and any names beginning with lowercase are converted so that the first letter is uppercase.</li></ol>

Using StrConv in a Macro
<ul> Create a new macro called Proper with the following specifications:

Action: SetValue

Item: Screen.ActiveControl

Expression: StrConv(Screen.ActiveControl,3)

NOTE: You can call this macro from the AfterUpdate property of a control on a form. This has the same effect as the first method.</li></ul>

<div class="references_section">