Microsoft KB Archive/210054

= ACC2000: Sample Function to Format Names Several Different Ways =

Article ID: 210054

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210054



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



SUMMARY
This article shows you how to create a sample user-defined Visual Basic for Applications function to concatenate portions of text fields. The function is useful for displaying names in various formats on forms and reports. You can use this function as a control source in a text box on either a form or report, or as an expression in a query.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Open any database in Microsoft Access and follow these steps to create the NFORMAT function:   Create a module and type the following line in the Declarations section if it is not already there: Option Explicit   Type the following procedure: Function NFormat(First As Variant, Middle As Variant, Last As _ Variant, Style As Variant)

On Error GoTo Err_NFormat Dim iFirst As Variant, iMiddle As Variant, iLast As Variant Dim NewName As String

iFirst = IIf(Len(First),Left(First, 1) + IIf(Len(First) = 1, " ", _ ". "), "")  iMiddle = IIf(Len(Middle),Left(Middle, 1) + IIf(Len(Middle) = 1, _ " ", ". "),"")  iLast = IIf(Len(Last),Left(Last, 1) + IIf(Len(Last) = 1, " ", ". ") _    , "")

Select Case Style Case "0", "FML" NewName = First & " " & (Middle + " ") & Last Case "1", "FIL" NewName = First & " " & iMiddle & Last Case "2", "IIL" NewName = iFirst & iMiddle & Last Case "3", "LFM" NewName = Last & ", " & First & (" " + Middle) Case "4", "LFI" NewName = Last & ", " & First & (" " + iMiddle) Case "5", "LII" NewName = Last & ", " & iFirst & iMiddle Case "6", "FL" NewName = First & " " & Last Case "7", "FI" NewName = First & " " & iLast Case "8", "LF" NewName = Last & ", " & First Case "9", "LI" NewName = Last & ", " & iFirst Case "10", "III" NewName = iFirst & iMiddle & iLast Case "11", "II" NewName = iFirst & iLast Case Else NewName = "" End Select

NFormat = Trim(NewName) Exit Function

Err_NFormat: NFormat = "#Error" End Function

  To test this function, type the following line in the Immediate window, and then press ENTER: ? NFormat("Nancy","Anne","Davolio",4) Note that Davolio, Nancy A. is displayed. 

Sample output with the following fields [First]="Nancy", [Middle]="Anne", [Last]="Davolio":   NFormat([First],[Middle],[Last],[Style])   Where                      Displays [Style]=0                 Nancy Anne Davolio [Style]=1                 Nancy A. Davolio [Style]=2                 N. A. Davolio [Style]=3                 Davolio, Nancy Anne [Style]=4                 Davolio, Nancy A.   [Style]=5                  Davolio, N. A.   [Style]=6                  Nancy Davolio [Style]=7                 Nancy D.   [Style]=8                  Davolio, Nancy [Style]=9                 Davolio, N.   [Style]=10                 N. A. D.   [Style]=11                 N. D. If you use an invalid style, the procedure returns an empty string ("").

Additional query words: inf

Keywords: kbhowto kbprogramming kbusage KB210054

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.