Solved

How can I parse a name into separate fields within an Access Query

Posted on 2013-11-14
5
1,473 Views
1 Endorsement
Last Modified: 2013-11-15
I have a field [Employee] that is formatted as follows [Lastname, Firstname, Middle], i.e.:

    Doe, John M

I need to separate the First and Last Names into their own individual fields within a query in MS Access (I don't need the middle initial at all, just the First and Last Names), i.e.:

   LastEmpName
   FirstEmpName

I have the following string for it to pull the Last Name out of the Employee field (and this works fine):

  LastEmpName: Left([tblEmployee_Audits].[Employee],InStr([tblEmployee_Audits].[Employee],",")-1)


I have the following string for it to pull the First Name out of the Employee field (this is not working correctly.  It is including the Middle Initial along with the First Name). See string below:

FirstEmpName: Trim(Mid([tblEmployee_Audits].[Employee],InStrRev([tblEmployee_Audits].[Employee],", ")+1))

Also, some names have a Middle Initial and some don't have one.

How do I write the string in the query for the FirstEmpName so that it doesn't include the Middle Initial?

Thanks,

gdunn59
1
Comment
Question by:gdunn59
5 Comments
 
LVL 2

Expert Comment

by:loki0609
ID: 39650381
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39650621
try this

FirstEmpName: Trim(Mid([employee],InStr([employee],",")+1,InStrRev([employee] & " "," ")-InStr([employee],",")))
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 300 total points
ID: 39650928
The easy way is:

split(myName, " ")(1)

Unfortunately, you cannot use that syntax in a query, so I use a function:

Public Function fnParse(ParseWhat As Variant, Delimiter As String, _
                        Position As Integer) As Variant

    Dim myArray() As String

    If IsNull(ParseWhat) Then
        fnParse = Null
    Else
        myArray() = Split(ParseWhat, Delimiter)
        If Position = 0 Or Position > UBound(myArray) + 1 Then
            fnParse = Null
        Else
            fnParse = myArray(Position - 1)
        End If
    End If
    
End Function

Open in new window

In your query, you would use:

FirstEmpName = fnParse([employee], " ", 2)
0
 
LVL 1

Author Closing Comment

by:gdunn59
ID: 39651896
Worked like a charm!

Thanks,
gdunn59
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39652689
glad to help
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

809 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