Microsoft Knowledge Base Email Alertz

(815282) - This article describes how to convert data values of Text data type to Proper Case format (Title case format).

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 815282 - Last Review: April 11, 2007 - Revision: 5.1

How to convert data values of the Text data type to the proper case format in Microsoft Access

Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) file or to a Microsoft Access database (.accdb) file.

On This Page

SUMMARY

This article describes how to convert data values of the Text data type to the proper case format.

MORE INFORMATION

The data values of Text data type can be converted to the proper case format by using one of the following methods:
  • Method 1: Use the Built-In String Conversion Function
  • Method 2: Use a User-Defined Function
Note Before you use either of these methods, create a sample table that is named MyTestTextList. To do this, follow these steps:
  1. Create a new blank database in Access.
  2. Create a new table with the following fields:
    Field Name: testText
    Data Type: Text
    
  3. Save the table as MyTestTextList.
  4. Add the following sample testText to the table:
    the cOw jumped Over the MOON
    
    THE QUICK BROWN FOX jUmped over THE lazy DOG 
    
  5. Save the database as MyTestDatabase.

Method 1: Use the built-in string conversion function

  1. Open the MyTestDatabase database in Access.
  2. On the Insert menu, click Query.

    Note In Microsoft Office Access 2007, click Query Design in the Other group on the Create tab.
  3. In the New Query dialog box, click Design view.

    Note In Access 2007, skip this step.
  4. In the Show Table dialog box, click Close.
  5. On the View menu, click SQL View.

    Note In Access 2007, click SQL in the Results group on the Design tab.
  6. Type the following code in the SQL view:
    SELECT testText, STRCONV(testText,3) as  TestText_in_Proper_Case FROM MyTestTextList
  7. On the Query menu, click Run.

    Note In Access 2007, click Run in the Results group on the Design tab.

    The output of the query follows:
    TestText					TestText_in_Proper_Case
    the cOw jumped Over the MOON The Cow Jumped Over The Moon
    THE QUICK BROWN FOX jUmped over THE lazy DOG The Quick Brown Fox Jumped Over The Lazy Dog
Note The StrConv(<Text>,3) method converts the first letter of every word in the text to uppercase. This behavior occurs only when the words are separated by a space or a tab. StrConv does not treat the special characters, such as - or $, as a word separator.

Method 2: Use a user-defined function

  1. Open the MyTestDatabase database in Access.
  2. On the Insert menu, click Module.

    Note In Access 2007, , click the drop-down arrow under Macro in the Other group on the Create tab.
  3. Type the following code in the current module and save your changes.
    Function Proper(X)
    Capitalize first letter of every word in a field.
    
    
    Dim Temp$, C$, OldC$, i As Integer
    
         If IsNull(X) Then
    
               Exit Function
    
         Else
    
               Temp$ = CStr(LCase(X))
    
               ' Initialize OldC$ to a single space because first
               ' letter must be capitalized but has no preceding letter.
    
               OldC$ = " "
    
                For i = 1 To Len(Temp$)
                       C$ = Mid$(Temp$, i, 1)
                       If C$ >= "a" And C$ <= "z" And (OldC$ < "a" Or OldC$ > "z") Then
                             Mid$(Temp$, i, 1) = UCase$(C$)
                       End If
                       OldC$ = C$
                Next i
    
                Proper = Temp$
    
          End If
    
    End Function
    
    Note You must specify Option Compare Database in the "Declarations" section of this module for the function to work correctly.
  4. On the File menu, click Close and Return to Microsoft Access.

    Note On the File menu, click Close for Access 97.
  5. On the Insert menu, click Query.

    Note In Access 2007, click Query Design in the Other group on the Create tab.
  6. In the New Query dialog box, click Design view.

    Note In Access 2007, skip this step.
  7. In the Show Table dialog box, click Close.
  8. On the View menu, click SQL View.

    Note In Access 2007, click SQL in the Results group on the Design tab.
  9. Type the following code in the SQL view:

    SELECT testText, proper(testText) as  testText_in_Proper_Case FROM MyTestTextList
    Notice that this query is similar to the query in Method 1. This is except for the function call.
  10. On the Query menu, click Run.

    Note In Access 2007, click Run in the Results group on the Design tab.

    The output of the query follows:
    TestText					TestText_in_Proper_Case
    the cOw jumped Over the MOON The Cow Jumped Over The Moon
    THE QUICK BROWN FOX jUmped over THE lazy DOG The Quick Brown Fox Jumped Over The Lazy Dog
While the output of both methods is similar, Method 2 gives you the flexibility to select any case format. This includes a chosen word separator such as - or _. You can define the required case format, or you can define a word separator. You can do this if you modify the Proper function that is mentioned in step 3.

REFERENCES

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
298607  (http://kbalertz.com/Feedback.aspx?kbNumber=298607/EN-US/ ) ACC2002: How to Use the StrConv Function to Capitalize Words and Phrases
253911  (http://kbalertz.com/Feedback.aspx?kbNumber=253911/EN-US/ ) ACC2000: How to Use the StrConv Function to Capitalize Words and Phrases
302499  (http://kbalertz.com/Feedback.aspx?kbNumber=302499/EN-US/ ) ACC97: How to Use the StrConv Function to Capitalize Words and Phrases
209903  (http://kbalertz.com/Feedback.aspx?kbNumber=209903/EN-US/ ) ACC2000: Cannot Use Intrinsic Constants in Queries, Forms, Reports

APPLIES TO
  • Microsoft Office Access 2007
  • Microsoft Office Access 2003
  • Microsoft Access 2002 Standard Edition
  • Microsoft Access 2000 Standard Edition
  • Microsoft Access 97 Standard Edition
Keywords: 
kbinfo kbexpertiseinter kbprogramming kbconversion kbhowto KB815282
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Anonymous User Report As Irrelevant  
Written: 5/6/2004 1:06 PM
Thank you so much!!! This was very helpful

Steven Report As Irrelevant  
Written: 10/21/2004 11:09 AM
Thanks very much. It works very well.