Solved

Name Initials in query

Posted on 2013-06-20
11
1,022 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

777 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