Results 1 to 9 of 9
  1. #1
    taryn is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    17

    Extracting initials from names

    Hello. I am trying to extract initials from first names, but I am having trouble with the query.



    Please see below examples of rows in a column called First_Names and what I would require the results of the query to be:

    Example Result
    Taryn T
    Taryn Louise TL
    Taryn Louise Anne TLA

    It's unlikely that there will be more than 3 first names in each row, but it is possible.

    I would be grateful if someone could provide a query for the above.

    Kind regards.

    Taryn

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I'd think you would need a custom vba function to process each name. You can call it from a query but if your recordset is large it'll be slow. Something like this
    Code:
    Public Function initials(first_names As String) As String
        Dim name_array() As String
        Dim item As Variant
        Dim rslt As String
        
        name_array = Split(first_names, " ")
        
        For Each item In name_array
            rslt = rslt & Left(item, 1)
        Next item
        
        initials = rslt
    End Function
    Then in a query you would call it like so
    Code:
    SELECT First_Names, initials(First_Names) As Initial FROM [your table]

  3. #3
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    A thread with almost the same requirements is here : https://www.access-programmers.co.uk...itials.315635/
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    taryn is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    17
    Thank you for your prompt reply kd2017.

    I would really rather have it in a query. For my current purposes, I will only be extracting the initials from one row in the First_Names column.

    Kind regards.

    Taryn

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    You put the function call in the query in query design grid. NYWFYC: Initials([First_Names])
    NYWFYC = Name You Want For Your Column

    Click image for larger version. 

Name:	qryFunctionCall.jpg 
Views:	27 
Size:	9.5 KB 
ID:	44174
    What you have been given is the only practical method for this.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    You would call the function from the query. A bit inefficient if you have thousands of rows though.

    You could possibly use a combination of Instr() & Len() but I'm not sure you could make it work with a variable number of words in one statement in a query...
    It's an interesting challenge though. I suspect a horrible nested IIf() could do it.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by Minty View Post
    I suspect a horrible nested IIf() could do it.
    Hmm... Not so horrible.
    This one below can manage up to three names:
    Code:
    Initials: Left([FN];1) 
    & IIf(InStr(1;[FN];" ")>0;Mid([FN];InStr(1;[FN];" ")+1;1)) 
    & IIf(InStr(InStr(1;[FN];" ")+1;[FN];" ")>0;Mid([FN];InStr(InStr(1;[FN];" ")+1;[FN];" ")+1;1))
    Replace the semicolons with commas if need it.

  8. #8
    taryn is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    17
    Thanks Minty.

    Kind regards.

    Taryn

  9. #9
    taryn is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    17
    Thanks Accesstos

    Kind regards.

    Taryn

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

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2018, 10:23 PM
  2. Replies: 3
    Last Post: 01-31-2015, 07:24 PM
  3. Replies: 9
    Last Post: 04-17-2013, 03:55 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