Link to home
Start Free TrialLog in
Avatar of sgrahman
sgrahman

asked on

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.

     

ASKER CERTIFIED SOLUTION
Avatar of tdkeller
tdkeller

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thorkyl
thorkyl

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
Avatar of sgrahman

ASKER

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.
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.
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 ~



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