?
Solved

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

Posted on 2013-11-14
5
Medium Priority
?
1,528 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 earned 1200 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
ID: 39652689
glad to help
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

770 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