Solved

I need help parsing a string and placing it into 3 separate variables

Posted on 2014-04-16
8
215 Views
Last Modified: 2014-05-11
Hi Experts,
I need help parsing a string in my VB6 application.  I have a list of names formatted in the following manner:  

LAST NAME, FIRST NAME Middle Initial
Jones, Martha
Smith, Olivia R


I want to parse  the name so that the LAST NAME, FIRST NAME, and Middle Initial are placed in the following 3 variables:  strLastName, strFirstName, strMiddleInit (if it is there).  How can I do this?


Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
8 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 167 total points
ID: 40005858
This should come close
Dim oRE as object
Dim oMatches as object
Dim oM as object      'only needed if passing a lot of names at once
Dim strWholeName as string
Dim strLastName as string, strFirstName as string, strMiddleInit as string

set oRE = createobject("vbscript.regexp")
oRE.Global = True
oRE.pattern = "(.*?), ([^ ]*) ?(.*)"
strWholename = "Jones, Martha"
if oRE.test(strWholename) THen
   set oMatches = oRE.Execute(strWholename)
   strLastName = oMatches(0).submatches(0)
   strFirstName = oMatches(0).submatches(1)
   strMiddleInit = oMatches(0).submatches(2)
End If

strWholename = "Smith, Olivia R"
if oRE.test(strWholename) THen
   set oMatches = oRE.Execute(strWholename)
   strLastName = oMatches(0).submatches(0)
   strFirstName = oMatches(0).submatches(1)
   strMiddleInit = oMatches(0).submatches(2)
End If

Open in new window

0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 167 total points
ID: 40006099
Here is a way with a bit less code:
   
Dim strParts() As String
    Dim strSurname  As String
    Dim strFirstName  As String
    Dim strMiddle  As String
    
    strParts = Split(Replace(strFullName, ",", ""), " ")
    strSurname = strParts(0)
    strFirstName = strParts(1)
    If UBound(strParts) > 1 Then
        strMiddle = strParts(2)
    End If

Open in new window

0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 166 total points
ID: 40006639
Here's some strait forward code doing simple InStr searches and basic Left, Mid, and Len functions:
Dim I as Integer
I = InStr( strFullName "," )
If I > 0 Then
    strLastName = Left$( strFullName, I - 1 )  'Save Last Name    

    strFullName = Trim$(Mid$( strFullName, I + 1 ))  'Update Name to just 1st and MI

    I = InStrRev( strFullName, " " )
    If I > 0 Then
        strMiddleInit = Mid$( strFullName, I + 1 )

        'Check for double 1st names (i.e. name with a space, yet no middle initial
        If Len( strMiddleInit ) > 1 Then
            strMiddleInit = ""
            strFirstName = strFullName 
        Else
            strFirstName = Left$( strFirstName, Len( strFullName ) - 2 )
        End If
    Else
        'No Middle Initial
        strFirstName = strFullName
        strMiddleInit = ""
    End If
End If

Open in new window


But the
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:aikimark
ID: 40006939
comment on http:#a40006099 - Graham's code
* if you have a double last name (not hyphenated), the parse will not be correct.  Think of Spanish family names.
* a correct version of Graham's code would not use the Replace() function, instead splitting on the ", " delimiter first and any remaining space delimiter second.
    strParts = Split(strFullName, ", ")
    strSurname = strParts(0)
    strParts = Split(Trim(strParts(1)), " ")
    strFirstName = strParts(0)
    If UBound(strParts) > 0 Then
        strMiddle = strParts(1)
    End If

Open in new window


comment on http:#a40006639 - HooKooDooKu's code
* if someone has a double first name, would we expect them to also have a middle initial?
* I do like your check for a single character middle initial.  It has caused me to rethink my regex pattern.  This pattern should handle multiple first names with a middle initial
^(.*?), \b(.*?)\b(.{0,1}?)$

Open in new window

It parses "briggs, joe bob d" into
Lastname=briggs
firstname=joe bob
MI=d

It parses "briggs, joe bob" into
Lastname=briggs
firstname=joe bob
MI=
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 40008060
If you need something more robust, you might give this a spin:

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1819-Parsing-Names-in-MS-Office-Visual-Basic-6-and-Visual-Basic-for-Applications.html

It's not perfect, of course, and some edge cases will cause it to return unexpected results.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40057280
I've requested that this question be closed as follows:

Accepted answer: 168 points for aikimark's comment #a40005858
Assisted answer: 166 points for GrahamSkan's comment #a40006099
Assisted answer: 166 points for matthewspatrick's comment #a40008060

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 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

21 Experts available now in Live!

Get 1:1 Help Now