Results 1 to 12 of 12
  1. #1
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618

    Returning Initials Only

    Trying to learn how to do the following. If there is a field "NAMESONID"; the person may have one name or multiple names.
    If the name is John. I would like to return a "J" in the field "INITIALS". That I can do.
    If the person has multiple names, let us say John Ross Peter. I would like to return JPR in the Initials field.


    The expression or VBA should therefore return the correct initials, no matter the amount of names.
    Thank you.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I would use the Split() function and then concatenate Left(ArrayName,1) within a loop of the split data.
    UCase the data before returning it to the calling code.

    Create a function to do all that.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Thanks. Sorry, I need more spoon feeding. I never used split before. Just used an hour to search how to do it, not there yet. My field holding the names, maybe 1, maybe multiple is "NamesOnID". Using Split("NamesOnId") didnt work yet.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    put this in a standard module

    Code:
    function getInitials(s as string) as string
    dim a() as string
    dim i as integer
    
        a=split(s," ")
        for i=0 to ubound(a)
            getInitials=getInitials & ucase(left(a(i),1)
        next i
    
    end function
    and call it in your query

    Initials:getinitials([fullname])

    note this code is aircode so check for typo's and you may need error handling in the event you try to pass a null or zls value for fullname

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556

  6. #6
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Thanks CJ. I created a standard Module, named Initials. In the query q01Employees I called Initials: getinitials([NamesOnId]). See the result on the image, opening the query again.Click image for larger version. 

Name:	220824a.png 
Views:	15 
Size:	20.6 KB 
ID:	48558

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Is missing a bracket??

    Code:
    Function getInitials(s As String) As String
    Dim a() As String
    Dim i As Integer
    
        a = Split(s, " ")
        For i = 0 To UBound(a)
            getInitials = getInitials & UCase(Left(a(i), 1))
        Next i
    
    End Function

  8. #8
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Baie Dankie. Dit werk goed.

  9. #9
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    I did say you would need to check for typo's

  11. #11
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Pleasant surprise!!!

  12. #12
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Thanks CJ for solving my issue!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Extracting initials from names
    By taryn in forum Queries
    Replies: 8
    Last Post: 02-11-2021, 08:37 AM
  2. Replies: 3
    Last Post: 05-09-2018, 10:23 PM
  3. Replies: 3
    Last Post: 01-31-2015, 07:24 PM
  4. Replies: 6
    Last Post: 02-27-2013, 04:43 PM
  5. need a formula function Name to Initials
    By techexpressinc in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 12:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums