Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2430
  • Last Modified:

How to split last, first and middle name from Name field?

Hi,
In my table I have a field called "Name" and the field look like this:

Name
Lamphere, Justin C
Vazquez Cortez, Fernando
Thompson, Chennea S
Her, Mai

Is it possible to separate them and create 3 columns, one for last, one for first and the other for middle name.

The output table will look like this:
Name                                   last                        first         middle
Lamphere, Justin C                Lamphere             Justin         C
Vazquez Cortez, Fernando      Vazquez Cortez    Fernando
Thompson, Chennea S            Thompson           Chennea      S
Her, Mai                                  Her                     Mai

I there a way to separate this field to 3 columns above. Thanks.

     

0
sgrahman
Asked:
sgrahman
1 Solution
 
tdkellerCommented:
Check out this MS site Sample Expressions to Extract a Portion of a String or Text:

http://support.microsoft.com/default.aspx?scid=kb;en-us;286238&Product=acc2002

it gives you exactly what you need.

Tina
0
 
thorkylCommented:
yes you will need some code

public function tkb_SPLIT_NAME()
    dim db as database
    dim rs as dao.recordset
    dim sLAST as string
    dim sFIRST as string
    dim sMI as string
    dim sNAME as string
    set db = currentdb
    set rs = db.openrecordset("SELECT [NAME] FROM TBL_NAMES")
    while not rs.eof
        sNAME = rs!NAME
        sLAST = left(sNAME,instr(1,sNAME,",")-1) 'get the last name
        sNAME = trim(right(sNAME,len(sNAME)-1) 'drop the comma and the space
        if instr(1,trim(sNAME)," ")>0 then 'we have a MI
            sFIRST = left(sNAME,instr(1,sNAME," ") 'get the first name
            sNAME = trim(right(sNAME,len(sNAME)) 'drop the space
            sMI = sNAME
        else
            sFIRST = Trim(sNAME) 'No MI
            sMI=""
        end if
        rs.edit
            rs!LNAME = trim(sLAST)
            rs!FNAME = trim(sFIRST)
            rs!MNAME = trim(sMI)
        rs.update
        rs.movenext
    wend
end function


Should do it for you
0
 
sgrahmanAuthor Commented:
hi thorkyl,
Where exactly am i supposed to be adding this code to? I inserted this code to module but it is not working. thx.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
tdkellerCommented:
use the expressions in my previous response in an update query.  Add FName, LName, MI to the table so they can be updated from Name.
0
 
fantasy1001Commented:
Ok, I will stick to the query type solution:

step:
1.   Create a table with field: Names, First, Middle, Last (in table 1)
2.   Paste your data into Names
3.   Create a new query in design mode
4.   Add table1 to the query
5.   From menu query, select update query
Now, we will create the query for first name
6.   in the input grid, select field->First
7.   in the Update To clause->Right(Trim([Names]),Len(Trim([Names]))-InStr(1,[Names]," "))
8.   Save and run the query, accept all the warning and continue to finish,
9.   Your result is shown in the column [first] of table1

Continue to other fields with Update To:
Refer here for update to clause
http://support.microsoft.com/default.aspx?scid=kb;EN-US;286238

~ fantasy ~



0
 
thorkylCommented:
If you place the code in a module
then just run the code ( F5 key )

You will need to change the line

 set rs = db.openrecordset("SELECT [NAME] FROM TBL_NAMES")

TO

 set rs = db.openrecordset("SELECT * FROM TBL_NAMES")

My sample taks the presumption that you added the new fields to the existing table
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now