Solved

Name Initials in query

Posted on 2013-06-20
11
1,074 Views
Last Modified: 2013-06-20
Hi Experts,
I have a question about pull only the initial from the name in query, here is what I try to do:
I have a name table, RINFNAME, RINMINIT, RINLNAME

Jane Doe   --   would like show j,d

Jane A. Doe  -- would like to show j,a,d

Jane B. Doe-Smith   -- would like to show j,b,D-S

John C. Smith-Jones, JR  -- would like to show j,c,s-m,jr

John Smith, JR  -- would like to show j,s,jr

whatever that full name is then I would like to show all the initials

I try this in the query:
Name: Left([RINFNAME],1) & ", " & Left([RINLNAME],1)
but this is only show the first & last initial

any help will be great appreciate
0
Comment
Question by:urjudo
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 57
ID: 39263346
You'll have to code the exceptions (for things like "Jr.", "Sr." etc.).

I would write this as a procedure:

Public Function GetInitials(varFName variant, varMiddleInit as variant, varLastName variant)  variant

 where you can do the lefts(), but also code for the exceptions,

and calling it like this from the query:

UserInitials: GetInitials( [RINFNAME], [RINMINIT], [RINLNAME])


Jim.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39263364
No points,

I agree with Jim that there are too many exceptions to do this with a simple string of left( ) function calls.  Write a function, it may take just slightly longer to run, but it allows you to insert remarks in your code so you can recall a year from now exactly what you were trying to do.
0
 

Author Comment

by:urjudo
ID: 39263429
you mean, I have to use public function to declear the Names?
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39263481
No, you create a public function and then pass it the first, mi, last field values, as shown in Jim's post.  Then you wrote the va code to strip off the first character of each name part, then you handle the exceptions.  

I'd write up a quick function for you but I'm on my iPad.
0
 

Author Comment

by:urjudo
ID: 39263708
can you give me a little more hint about the how to writ the public function because I'm not familiar to do the function, I normally use query.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 39263904
test this sample db..

run the query qryGetInitials
db-Q-28163004.mdb
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39263936
This might work for your form.
Name: Left([RINFNAME],1) & IIF(IsNull([RINMINIT]),"", "," & Left([RINLNAME],1)) & Left([RINLNAME],1) & IIF(Instr([RINLNAME], ",")=0,"", Mid([RINLNAME], Instr([RINLNAME], ","))

Open in new window

0
 
LVL 57
ID: 39263937
<<familiar to do the function, I normally use query. >>

  The same statement you use in a query are what you use in a function (left(), right(), Mid(), InStr(), etc)


<<can you give me a little more hint about the how to writ the public function >>

In Access goto modules and then do new standard module.  Paste in this as a start:

Public Function GetInitials(varFName As Variant, varMiddleInit As Variant, varLastName As Variant) As Variant

  Dim varInitials As Variant

  ' Process the first name
  If Not IsNull(varFName) Then varInitials = varInitials & Left$(varFName, 1)

  ' Process middle initial
   If Not IsNull(varMiddleInit) Then varInitials = varInitials & Left$(varMiddleInit, 1)

  ' Process last name
   If Not IsNull(varLastName) Then varInitials = varInitials & Left$(varLastName, 1)

   GetInitials = varInitials

End Function


 This gives you the basics.  Now you need to handle those exceptions, like "Jr.", etc.   See if you can do those with what I've given you.

 You can test this from the debug window (Ctrl/G when your in the code window) by typing:

?  GetInitials("Jane","A.","Doe")

 Once you have it working the way you want, call it from the query as I showed above.

Jim.
0
 

Author Comment

by:urjudo
ID: 39264039
Hi Capricorn1,
I tried your test, that is exactly what I need, but one more question, I forget some times the last name has II or III, in other word, John Smith,III, how do I show j,s,III.   In your test, I saw that you j as integer,  (I copy the function that you did here), so where I put the III?   thanks

Function fGetInitials(strName As String) As String
Dim varName() As String, j As Integer, strInit As String, xName() As String
If strName & "" <> "" Then
     varName = Split(strName, " ")
     
     For j = 0 To UBound(varName)
          If j > 2 Then
               strInit = strInit & "," & varName(3)
               Exit For
          End If
          xName = Split(varName(j), "-")
          If UBound(xName) > 0 Then
               strInit = strInit & "," & Left(xName(0), 1) & "-" & Left(xName(1), 1)
               Else
               strInit = strInit + "," + Left(varName(j), 1)
          End If
   
     Next
     fGetInitials = Replace(Mid(strInit, 2), ",,", ",")
End If

End Function
0
 

Author Comment

by:urjudo
ID: 39264046
never mind Caricorn1, it works on the III too.
0
 

Author Closing Comment

by:urjudo
ID: 39264049
Thanks so very much!!!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question