Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1334
  • Last Modified:

Name Initials in query

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
urjudo
Asked:
urjudo
  • 5
  • 2
  • 2
  • +2
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
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
 
Dale FyeCommented:
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
 
urjudoAuthor Commented:
you mean, I have to use public function to declear the Names?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Dale FyeCommented:
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
 
urjudoAuthor Commented:
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
 
Rey Obrero (Capricorn1)Commented:
test this sample db..

run the query qryGetInitials
db-Q-28163004.mdb
0
 
aikimarkCommented:
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
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<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
 
urjudoAuthor Commented:
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
 
urjudoAuthor Commented:
never mind Caricorn1, it works on the III too.
0
 
urjudoAuthor Commented:
Thanks so very much!!!!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now