473,378 Members | 1,119 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Instring Function Script

I am trying to write this instring script on a text field on Customer Name and having difficulties with the last name:
list of customer names:
Bob A George
John Thompson
Paul B Michael

I know the instr function is having difficulties because some of the names have middle letters. For the ones that have a Middle letter it is giving me the results with a space before it _George and the ones that don't have a middle letter it is cutting off the first couple letters. or giving me the last name with the middle letter. I have been trying it all different kind of ways and haven't figure it out
I have tried formulas such as:
Right([CN],InStr([CN]," ")-1)

I have also tried a mid function and haven't been successful. I almost think I need to do an if statement and do it one way if there is a letter and another if it is false, but haven't figured out to write it. Any help on this would be greatly appreciated.

The Left([CN],Instr([CN]," ")-1) works fine for the first name.

Thanks,

Matt
Jul 8 '08 #1
27 3163
missinglinq
3,532 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. LastName = Right([CN],Len([CN])-InstrRev([CN]," "))
Linq ;0)>
Jul 8 '08 #2
Expand|Select|Wrap|Line Numbers
  1. LastName = Right([CN],Len([CN])-InstrRev([CN]," "))
Linq ;0)>

Wow that was alot easier than I anticipated and worked perfectly..


Thank you so much.

Matt
Jul 8 '08 #3
ADezii
8,834 Expert 8TB
Here is also a little Routine that I use to extract the First, Last, and possibly Middle Initials from a Name.
Jul 8 '08 #4
missinglinq
3,532 Expert 2GB
Here? Where?

Linq ;0)>
Jul 8 '08 #5
NeoPa
32,556 Expert Mod 16PB
Leave him alone Linq!

He's having a little nap and will be back with some code shortly :D
Jul 9 '08 #6
missinglinq
3,532 Expert 2GB
I thought maybe it was some of that "stealth" code that can't be seen!

Linq ;0)>
Jul 9 '08 #7
NeoPa
32,556 Expert Mod 16PB
No. I checked ;)

There was nothing else there :)
Jul 9 '08 #8
ADezii
8,834 Expert 8TB
==> Linq & NeoPa, you gentlemen do not have the necessary clearance to view it. Actually, I forgot to Post it and it's at work, but as soon as I get back to work, I'll 'try' again to remember to Post the code.
Jul 9 '08 #9
missinglinq
3,532 Expert 2GB
I understand that too much smoke eating can negatively affect long term memory!

Linq ;0)>
Jul 9 '08 #10
ADezii
8,834 Expert 8TB
I understand that too much smoke eating can negatively affect long term memory!

Linq ;0)>
Long, Short, and Intermediate! (LOL).
Jul 9 '08 #11
ADezii
8,834 Expert 8TB
Here is a little, General Template, (the one that NeoPa and Linq missed), which will parse a Full Name consisting of a First, Last, and Optional Middle Name/MI, into its constituent parts. It ignores Names with double spaces, Prefixes and Suffixes, Titles, etc.
Expand|Select|Wrap|Line Numbers
  1. Public Function fParseName(strName As String)
  2. Dim varNameParts As Variant
  3. Dim intCounter  As Integer
  4. Dim strFirstName As String
  5. Dim strLastName As String
  6. Dim strMI As String
  7.  
  8. varNameParts = Split(strName)
  9.  
  10. Select Case UBound(varNameParts)
  11.   Case 1    'First and Last Name
  12.     strFirstName = varNameParts(0)
  13.       Debug.Print strFirstName
  14.     strLastName = varNameParts(1)
  15.       Debug.Print strLastName
  16.   Case 2    'First, MI, and Last Name
  17.     strFirstName = varNameParts(0)
  18.       Debug.Print strFirstName
  19.     strMI = varNameParts(1)
  20.       Debug.Print strMI
  21.     strLastName = varNameParts(2)
  22.       Debug.Print strLastName
  23.  Case Else  'anything goes Dr. Tom Clancy III
  24.    Debug.Print "Not a standard naming convention"
  25. End Select
  26. End Function
Expand|Select|Wrap|Line Numbers
  1. ? fParseName("Alfred E. Neumann")
  2. Alfred
  3. E.
  4. Neumann
  5.  
  6. ? fParseName("Alfred Eugene Neumann")
  7. Alfred
  8. Eugene
  9. Neumann
  10.  
  11. ? fParseName("Dr. Thomas Cook III")
  12. Not a standard naming convention
Jul 10 '08 #12
NeoPa
32,556 Expert Mod 16PB
; - )
Jul 11 '08 #13
I am going back to the drawing board on this one. I was just informed that the last name of this is not working correctly. Using this function:
LastName = Right([CN],Len([CN])-InstrRev([CN]," "))
It is Giving me the III or JR or SR instead. So if a name looks as such:
Alfred J Hitchcock Jr
Its giving me the Jr.
There were 5 instances that this happened in out of 200. Is this something I will need to do manually or is there a better way of writing this.

Please help,

Thanks,

Matt
Dec 11 '08 #14
NeoPa
32,556 Expert Mod 16PB
You have to consider first what concept you will use to try to determine what should be recognised as the "Last Name". Usually, it's the last word in the field. If this is not right for you then you need to consider and specify, what logic you intend to use.

Turning the logic into code is only straightforward if the logic is known. Only you know your requirements.
Dec 11 '08 #15
I need the First and Last names of the customer. When someone helped me with the last name I didn't think about the Jr or Sr or III part being on the end. If the very limited instances that this happens is there a way to write to code to reflect only the last name even if there is something such as Jr or Sr or II or III after there name?
Dec 11 '08 #16
NeoPa
32,556 Expert Mod 16PB
That's what my last post was about.

In simpler terms that is something you will need to decide on yourself. We can help you code it when you know what you want, but we can't tell you what you want. That's always going to be down to you I'm afraid.
Dec 11 '08 #17
I thought I was just doing that. My Customers Name comes in one field such as: Alfred J Hitchcock Jr. or Alfred Hitchcock or Alfred J Hitchcock.
I am using this for the first name: Left([CN],Instr([CN]," ")-1) which is working fine.
I now having difficulties with the last name. I just need the last name only. I do not need the middle initial (If there is one) or the ending part (Jr., Sr., III, etc). Is there a way to write it to only give me the Last Name?

Thanks NeoPA,

Matt
Dec 11 '08 #18
NeoPa
32,556 Expert Mod 16PB
There still seems to ba a bit of confusion here Matt. Let me try to outline things a little.

Last Name is a concept that we can recognise quite easily as humans. We have learnt over many years how things fit together, and the rules for determining where to find this piece of info from within a larger "Name" format string. Because this is straightforward for us, doesn't mean that quite a lot of complex consideration is going on under the hood.

To translate this so that it can be processed by a computer is not remotely straightforward (due to the number of checks that DO go on under the hood if you think about it consciously). We are probably then looking at a set of imperfect rules to determine how best to approximate to a working solution. This might include numerous checks of the last "word" in the text to check for matching a known list of suffixes (Jr; Junior; II; III; IV; etc etc I'm sure you're getting the picture). It may alternatively, be a simpler algorithm which simply assumes that the last "word" is the last name.

How exactly do you want this to be done? Only you are in a position to make that call.
Dec 12 '08 #19
NeoPa,
First of all, I wanted to thankyou for your time helping me with this. I understand the complexity of the name situation because the name is constantly different in every row. Some have middle intials, some don't, and some have endings such as Jr, Sr, III, etc.
If there is a way to extract out the Last Name only and not include the Jr, Sr, III, etc. I would greatly appreciate some help on how to do that.
If there is a much simplier means of getting the Last name and the ending part of Sr, Jr, III, etc, than that would be acceptable, however, I can't use it the way it is written right now because it is giving me only the very last thing in the Name Field and that information is useless to the people getting the data without the actual last name to go with the ending part.

Thanks,

Matt
Dec 12 '08 #20
ADezii
8,834 Expert 8TB
@Supermansteel
Here is some code, along with results, that will successfully generate the Last Name from any Name ending in Jr, Jr., Sr, Sr., II, III. It can be expanded as much as you like, and can easily be incorporated into the logic that I have previously demonstrated (Post #12). It is, as stated by NeoPa, almost impossible to allow for every contingency, however. Let me know if you need further explanation on the code.
Expand|Select|Wrap|Line Numbers
  1. Dim strFullName As String
  2. Dim varNameParts As Variant
  3.  
  4. strFullName = "Alfred J Hitchcock Jr"
  5. 'strFullName = "Alfred J Hitchcock Jr."
  6. 'strFullName = "F. Wilder II"
  7. 'strFullName = "James Earl Jones III"
  8. 'strFullName = "Tom Jones Sr."
  9. 'strFullName = "Tom Jones Sr"
  10.  
  11. varNameParts = Split(strFullName, " ")
  12.  
  13. If InStr(strFullName, "Jr") > 0 Or InStr(strFullName, "Sr") Or _
  14.    InStr(strFullName, "II") > 0 Or InStr(strFullName, "III") Then
  15.   Debug.Print varNameParts(UBound(varNameParts) - 1)
  16. End If
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Hitchcock
  2. Hitchcock
  3. Wilder
  4. Jones
  5. Jones
  6. Jones
Dec 14 '08 #21
NeoPa
32,556 Expert Mod 16PB
OK. Let's get down to brass tacks.

There are two fundamental algorithms or approaches that can be used here. I was hoping that Matt could get that far by himself. At least to sit down and think about it and determine exactly what he was even looking for.

One is fairly simplistic and has already been attempted. Get the last word (set of characters that doesn't include a space) and assume it is the Surname required. This does have the drawback that the standard suffixes (Jr; Jr.; Junior; Sr; Sr.; Senior; I; II; III; IV; etc; etc; etc) are treated as the surname when present. This approach involves less up-front development, but many more false results.

The second is to follow the same logic except that this word is then checked against a known list of suffixes and, if a match is found, the previous word is then used. This approach involves much more development but far fewer failed results.

Assuming that what Matt really wants is the latter (probably a reasonable guess even without a direct statement to that effect) then ADezii has provided a solution that may fit the bill.

In a situation, such as this though, where the list of possible suffixes is so large, I would recommend using the software to find a match from within a table (If being done in Access this is straightforward. If in Excell then the data could be held in a (possibly hidden) worksheet and the VLookup() function used). This has the benefit of simplifying the code greatly. At least in as far as reading and understanding it goes. It also means that it should be a lot easier to add new items for exclusion when found.
Dec 16 '08 #22
Stewart Ross
2,545 Expert Mod 2GB
With the excellent help provided by ADezii and NeoPa here there is little need for further comment I know, but taking a slightly different angle to my colleagues I'd just point out that trying to dis-assemble a name into its components is always a compromise. In mathematical terms there is no inverse function available for this task.

A name stored in a single field is in your case essentially made up of four components, two of which are optional:

Name = FirstName + [Initial] + LastName + [Honorific]

As the honorific component - the bit that identifies someone as Joe Bloggs Jr or Joe Bloggs III etc - is just a character sequence like the name itself there is no inverse function which can correctly return the last name in all circumstances without knowing all potential honorific values (now and in the future) in advance.

So, your single field is essentially acting as a function f(x) where x=F+I+L+H. You need to be able to return an inverse of this to separate L (or any other component) from the combined value. But this is not directly possible, as information about which of the components was entered has been lost by combining them as one field.

The look-up table as suggested by NeoPa is the best solution for this - but in my view your question is a very good illustration of why names should not be entered into single fields then parsed later. It is never 100% possible to recover the separate components in all circumstances and identify them 100% correctly as FirstName, LastName etc.

-Stewart
Dec 16 '08 #23
OldBirdman
675 512MB
None of the above examples even addresses the possibility of compound last names (surnames). Names containing de, van, von, Mc, Mac, will further compound any algorithm. When I have this problem, I count the words and when words <> 2, I open a dialog to ask the user to enter the surname.

OldBirdman
Dec 16 '08 #24
ADezii
8,834 Expert 8TB
NeoPa makes an excellent point in suggesting that if you really want to expand the logic as far as Prefixes and Suffixes go, you should test for Matches with a Table(s). If anyone is actually interested, I wrote a simple Algorithm that illustrates how this may be accomplished:
Expand|Select|Wrap|Line Numbers
  1. Public Function fParseFullName(strFullName As String)
  2. Dim varNameParts As Variant
  3. Dim blnHasPrefix As Boolean
  4. Dim blnHasSuffix As Boolean
  5. Dim MyDB As Database
  6. Dim rstPrefixes As DAO.Recordset
  7. Dim rstSuffixes As DAO.Recordset
  8.  
  9. Set MyDB = CurrentDb
  10. Set rstPrefixes = MyDB.OpenRecordset("tblPrefixes", dbOpenForwardOnly)
  11. Set rstSuffixes = MyDB.OpenRecordset("tblSuffixes", dbOpenForwardOnly)
  12.  
  13. 'Initialize (not really required)
  14. blnHasPrefix = False
  15. blnHasSuffix = False
  16.  
  17. Do While Not rstPrefixes.EOF
  18.   If InStr(strFullName, rstPrefixes![Prefix]) > 0 Then
  19.     blnHasPrefix = True
  20.       Exit Do
  21.   Else
  22.     rstPrefixes.MoveNext
  23.   End If
  24. Loop
  25.  
  26. Do While Not rstSuffixes.EOF
  27.   If InStr(strFullName, rstSuffixes![Suffix]) > 0 Then
  28.     blnHasSuffix = True
  29.       Exit Do
  30.   Else
  31.     rstSuffixes.MoveNext
  32.   End If
  33. Loop
  34.  
  35. varNameParts = Split(strFullName, " ")
  36.  
  37. 'UBound(varNameParts) can indicate the number of Spaces in a Name, so
  38. 'if a Name has a Prefix (blnHasPrefix = True) and a Suffix (blnHasSuffix = True),
  39. 'and also contains 4 Spaces it is more than likely:
  40. 'Prefix & First & MI & Last & Suffix and the First and Last Names will be
  41. 'contained in varNameParts(1) and varNameParts(3)
  42. If blnHasPrefix And blnHasSuffix And UBound(varNameParts) = 4 Then
  43.   Debug.Print "First Name: " & varNameParts(1)
  44.   Debug.Print "Last Name: " & varNameParts(3)
  45. End If
  46. yada, yada, yada code...
  47.  
  48.  
  49. rstPrefixes.Close
  50. rstSuffixes.Close
  51. Set rstPrefixes = Nothing
  52. Set rstSuffixes = Nothing
  53. End Function
Function Call and Results:
Expand|Select|Wrap|Line Numbers
  1. Call fParseFullName("Dr. Armund P. Dezii Sr.")
Expand|Select|Wrap|Line Numbers
  1. First Name: Armund
  2. Last Name: Dezii
Dec 17 '08 #25
OldBirdman
675 512MB
I think you are giving false hope to Supermansteel. However this full name was generated, it was done by humans, and may have problems beyond honorifics, suffixes, middle names, initials, nicknames, or compound names. The simple addition of an extra blank destroys the accuracy of the function Split. There is a difference between "Howard James" and "Howard, James".

Either Supermansteel has some control over the input, and should therefore redesign the form and/or table, or he has no control, and should write some kind of parse routine and then manually either accept the result or re-enter the name field data.

You all can write very nice code to process any short sample data with 100% accuracy, providing you can see the sample data in advance. This gives the impression that the routine always works, and it won't. If the function call is:
Expand|Select|Wrap|Line Numbers
  1. Call fParseFullName("Dr. Armund P.  Dezii Sr.")
there will be no last name in varNameParts(3) and UBound(varNameParts) = 5, not 4.
Expand|Select|Wrap|Line Numbers
  1. Call fParseFullName("Armund Drew Dezii,  MD")
will meet your conditions, but First Name: "Drew" and Last Name: " ". Maybe yada, yada, yada code... will take care of this, but I doubt it.

OldBirdman
Dec 18 '08 #26
ADezii
8,834 Expert 8TB
@OldBirdman
You are, of course, correct in everything that you say. Actually, I was making a very silly point and stating that almost every conceivable contingency can be allowed for if you are willing to write enough code to allow for it. The revised code below will correctly process your 2 exceptions above, namely:
"Dr. Armund P. Dezii Sr." and "Armund Drew Dezii, MD".
Expand|Select|Wrap|Line Numbers
  1. Public Function fParseFullName(strFullName As String)
  2. Dim varNameParts As Variant
  3. Dim blnHasPrefix As Boolean
  4. Dim blnHasSuffix As Boolean
  5. Dim MyDB As Database
  6. Dim rstPrefixes As DAO.Recordset
  7. Dim rstSuffixes As DAO.Recordset
  8.  
  9. Set MyDB = CurrentDb
  10. Set rstPrefixes = MyDB.OpenRecordset("tblPrefixes", dbOpenForwardOnly)
  11. Set rstSuffixes = MyDB.OpenRecordset("tblSuffixes", dbOpenForwardOnly)
  12.  
  13. 'Initialize (not really required)
  14. blnHasPrefix = False
  15. blnHasSuffix = False
  16.  
  17. strFullName = Replace(strFullName, "  ", " ")
  18. strFullName = Replace(strFullName, ",", "")
  19.  
  20. Do While Not rstPrefixes.EOF
  21.   If InStr(strFullName, rstPrefixes![Prefix] & " ") > 0 Then
  22.     blnHasPrefix = True
  23.       Exit Do
  24.   Else
  25.     rstPrefixes.MoveNext
  26.   End If
  27. Loop
  28.  
  29. Do While Not rstSuffixes.EOF
  30.   If InStr(strFullName, " " & rstSuffixes![Suffix]) > 0 Then
  31.     blnHasSuffix = True
  32.       Exit Do
  33.   Else
  34.     rstSuffixes.MoveNext
  35.   End If
  36. Loop
  37. MsgBox blnHasPrefix & " ==> " & strFullName & " ==> " & blnHasSuffix
  38. varNameParts = Split(strFullName, " ")
  39.  
  40. 'UBound(varNameParts) can indicate the number of Spaces in a Name, so
  41. 'if a Name has a Prefix (blnHasPrefix = True) and a Suffix (blnHasSuffix = True),
  42. 'and also contains 4 Spaces it is more than likely:
  43. 'Prefix & First & MI & Last & Suffix and the First and Last Names will be
  44. 'contained in varNameParts(1) and varNameParts(3)
  45. If blnHasPrefix And blnHasSuffix And UBound(varNameParts) = 4 Then
  46.   Debug.Print "First Name: " & varNameParts(1)
  47.   Debug.Print "Last Name: " & varNameParts(3)
  48. ElseIf Not blnHasPrefix And blnHasSuffix And UBound(varNameParts) = 3 Then
  49.   'First & MI & Last & Suffix
  50.   Debug.Print "First Name: " & varNameParts(0)
  51.   Debug.Print "Last Name: " & varNameParts(2)
  52. End If
  53.  
  54. rstPrefixes.Close
  55. rstSuffixes.Close
  56. Set rstPrefixes = Nothing
  57. Set rstSuffixes = Nothing
  58. End Function
P.S. - Keep in mind that what I have posted would only be a portion of the complete Algorithm.
Dec 18 '08 #27
NeoPa
32,556 Expert Mod 16PB
These are all very valid points.

However, there are sometimes situations in the real world where a reasonable approximation is adequate, and indeed most appropriate. If you are receeiving millions of records of data and 99.99% of them can be handled by a certain algorithm, then it is reasonable to use that algorithm to process most of the data, but handle the rest by hand.

We cannot always exert control over incoming data. Hopefully, when we can, we do it intelligently. We cannot afford to refuse contemplation of issues simply because they are not formulated as we would have them. That way leads to sand in the ears (ask any ostrich) :D
Dec 18 '08 #28

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: Thomas Mlynarczyk | last post by:
Hi, I stumbled over a strange behaviour of Mozilla. When I want to access the caller property of a function that was not called from within another function, Mozilla seems to abort the script....
3
by: Mark Kurten | last post by:
in the visual studio .NET samples that come with the product, why do javascript functions all have the parameters below. Even though, the event (like Onclick) which is calling it doesn't have any...
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
11
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
2
by: WGW | last post by:
Hello all, I need another set of eyes cause it just isn't working, no matter how identical I make it. The initRotator function works when called by itself, but when adding another function, only the...
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.