ACC2000: Sample Function to Format Names Several Different Ways
ID: Q210054
|
The information in this article applies to:
SUMMARYModerate: Requires basic macro, coding, and interoperability skills.
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 INFORMATIONMicrosoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals 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 needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the
following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
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 : kbprg kbusage kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
|