Solved

Name Initials in query

Posted on 2013-06-20
11
1,199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 58
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 48

Expert Comment

by:Dale Fye
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 48

Expert Comment

by:Dale Fye
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 46

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 58
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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