473,666 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help parsing "name" field into fname, lname, middleinit

11 New Member
I'm a Novice User using Access 2003
Tables are via ODBC (i.e., cannot alter fields)

I have a report that uses the field OrderRepName (text string) and is formatted as lastname, firstname, middleinitial (No prefixes or suffixes, etc.). Sometimes the field is blank and sometimes there is no middle initial.

I found the following code on the Internet and it seems like it will work except I get an error message regarding Null values, and it doesn't seem to handle middle initials. (I modified it slightly replacing the word "underscore " with "space").

Can someone help me modify the following code, or suggest something else?
Expand|Select|Wrap|Line Numbers
  1. Function ParseFirstComp(pValue) As String
  2. Dim LPosition As Integer
  3. 'Find postion of space
  4. LPosition = InStr(pValue, " ")
  5. 'Return the portion of the string before the space
  6. If LPosition > 0 Then
  7. ParseFirstComp = Left(pValue, LPosition - 1)
  8. Else
  9. ParseFirstComp = ""
  10. End If
  11. End Function
  12.  
  13. Function ParseSecondComp(pValue) As String
  14. Dim LPosition As Integer
  15. 'Find postion of space
  16. LPosition = InStr(pValue, "_")
  17. 'Return the portion of the string after the space
  18. If LPosition > 0 Then
  19. ParseSecondComp = Mid(pValue, LPosition + 1)
  20. Else
  21. ParseSecondComp = ""
  22. End If
  23. End Function
This first function called ParseFirstComp will return the portion of the string before the space. The second function called ParseSecondComp will return the portion of the string after the space.
Next, you'll need to use this function in your query

(picture - but it won't let me paste it into this posting)

In the example above, we've used both functions to parse a field called Name. You will need to substitute your field name with the ParseFirstComp and ParseSecondComp functions. So we've typed ParseFirstComp([Name]) in the first field and ParseSecondComp ([Name]) in the second field. Access assigns the field name of "Expr1" and "Expr2" - you can always overwrite this.

Now, when we run the query, we'll get the following results:

(Picture - but it won't let me paste it into this posting)

Thanks,

Krazy
May 13 '08 #1
4 5519
MikeTheBike
639 Recognized Expert Contributor
I'm a Novice User using Access 2003
Tables are via ODBC (i.e., cannot alter fields)

I have a report that uses the field OrderRepName (text string) and is formatted as lastname, firstname, middleinitial (No prefixes or suffixes, etc.). Sometimes the field is blank and sometimes there is no middle initial.

I found the following code on the Internet and it seems like it will work except I get an error message regarding Null values, and it doesn't seem to handle middle initials. (I modified it slightly replacing the word "underscore " with "space").

Can someone help me modify the following code, or suggest something else?

Function ParseFirstComp( pValue) As String
Dim LPosition As Integer
'Find postion of space
LPosition = InStr(pValue, " ")
'Return the portion of the string before the space
If LPosition > 0 Then
ParseFirstComp = Left(pValue, LPosition - 1)
Else
ParseFirstComp = ""
End If
End Function

Function ParseSecondComp (pValue) As String
Dim LPosition As Integer
'Find postion of space
LPosition = InStr(pValue, "_")
'Return the portion of the string after the space
If LPosition > 0 Then
ParseSecondComp = Mid(pValue, LPosition + 1)
Else
ParseSecondComp = ""
End If
End Function


This first function called ParseFirstComp will return the portion of the string before the space. The second function called ParseSecondComp will return the portion of the string after the space.
Next, you'll need to use this function in your query

(picture - but it won't let me paste it into this posting)

In the example above, we've used both functions to parse a field called Name. You will need to substitute your field name with the ParseFirstComp and ParseSecondComp functions. So we've typed ParseFirstComp([Name]) in the first field and ParseSecondComp ([Name]) in the second field. Access assigns the field name of "Expr1" and "Expr2" - you can always overwrite this.

Now, when we run the query, we'll get the following results:

(Picture - but it won't let me paste it into this posting)

Thanks,

Krazy
Hi

Just a thought, have you tried using the string functions directly in the SQL, ie

SELECT Left([Name],InStr([Name]," ")-1) AS FName, Mid([Name],InStr([Name]," ")+1) AS SName
FROM tblTechnicians;

or won't the ODBC driver alow this.

Queries work faster with Native function than with bespoke function witten by the programmer (if you have a significant number of records)!!

Note: If the above works, and you have Initials (or not) between the first and last name then try this

SELECT Left([Name],InStr([Name]," ")-1) AS FName, Mid([Name],InStrRev([Name]," ")+1) AS SName
FROM tblTechnicians;

InStRev() searches from the end backwords.

HTH


MTB
May 14 '08 #2
NeoPa
32,568 Recognized Expert Moderator MVP
I've never seen commas (,) after a first name and before an initial.

Can you explain precisely and accurately the layout of the name you are working with and what possible alternatives can be found (Which parts are mandatory and which optional)?

I presume you want to take this single field and populate three fields; FName, LName and MiddleInit.
May 14 '08 #3
KrazyKasper
11 New Member
I've got it now.
Thanks to all for your help on this problem.

Krazy

p.s. If this thread needs to be closed, someone please let me know how to do that.
Jun 9 '08 #4
NeoPa
32,568 Recognized Expert Moderator MVP
No worries Krazy. We just like a solution posted where possible that's all. We tend not to close or lock threads as somebody else may want to add a helpful comment later.
Jun 10 '08 #5

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

Similar topics

2
17748
by: John Davis | last post by:
I want to know what's the differences between Request.Form("Field Name") and Request.QueryString("Field Name") OR they function exactly the same, which is to return the value of the field?? Thanks, John
6
7049
by: Mason A. Clark | last post by:
LAST WORD(s): 1. MSIE6 and Firefox will go to the top of the page on command <a href="#top">go upsy</a> even if there is NO name="top" or id="top" They know what a "top" is :-) Opera does not.
5
16597
by: Theresa Hancock via AccessMonster.com | last post by:
I have an Excel table I need to import into Access. The name is entered into one field "Name". I'd like to have two fields in Access, FirstName and LastName. How do I do this. -- Message posted via http://www.accessmonster.com
14
5496
by: Larry R Harrison Jr | last post by:
I have designed databases but have never come across any complications due to the ridiculous situation of a hyphenated last name. As a database designer (very junior level) I disdain anything unwieldly, and consider this unwieldly. I haven't dealt with it, as I said, but I figure if I do, I'd simply design the text field to not accept hyphens, so Smith-Barney would be stored as SmithBarney, or even Smithbarney as I commonly have the...
2
2765
by: billy001 | last post by:
How do I go about grabbing only the last name out of a 'Full Name' field ? I've so far tried executing a parse script in Oracle 9i using SQL Plus and am not successfully getting any data returned. Cannot find any references to the SQL scripts that will do this. Please help. All I need is just the basic SQL command and I can build on that. Thanks. Bill
2
1980
by: CollierC | last post by:
I need to write a query that manipulates a name field from "firstname lastname" to "lastname, firstname". I have read multiple solutions to split firstname and lastname to separate fields, unfortunately I haven't found anyting on manipulating substrings within the same field. Can this be done?
1
7078
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form action="http://cm1web1/WebSurveyComponents/script/ processform.asp" method="post">
4
3525
by: NeilIanBaker | last post by:
Hello I am trying to select the first name and surname from a name field where the name is in the form of; eg. Mrs Marilyn Payne Mrs Mary Swanton Ms EM Lomas Lt Col R Cartwright I need to break the name down into forename and surname separately so that I can link to another database where forename and surame are seperate.
2
1632
by: govmate | last post by:
Hello I'm new to queries! I'm having a problem writing an update query that will look at a name field ie (Dan Wilson Sr) and flip the name to a shortname ie (Wilson Sr Dan) with the criteria based to only flip the name if it contains Jr or Sr. Below is an example of a select query I was using to test before updating my table. SELECT * FROM WHERE (instr(shortname,' JR')>0 Or instr(shortname,'SR')>0 Or instr(shortname,' III')>0) And...
0
8878
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6200
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5671
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.