Solved

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

Posted on 2013-11-14
5
1,433 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 119

Expert Comment

by:Rey Obrero
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
 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now