Solved

Name Initials in query

Posted on 2013-06-20
11
934 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 119

Accepted Solution

by:
Rey Obrero 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
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…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now