Solved

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

Posted on 2013-11-14
5
1,512 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 48

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 48

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

688 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