Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to trim a field 1

Status
Not open for further replies.

jhb11

MIS
Sep 17, 2002
5
US
I have a field that contains (lastname, firstname). I would like to be able to delete (, firstname) from all fields. What do I need to do. Thanks.
 
Here's a proof-of-concept function I wrote that Extracts the first and last name out of a string with the "Last, First" structure.

Code:
Public Function GetLastName()
Code:
' This function extracts the first part of a string that is "LastName, FirstName"
Code:
Dim strLast, strFirst, strString As String
Dim intStrPos As Integer

strString = "Jones, John"
Code:
'1 -- Search for the position of the "comma" delimiter.
Code:
intStrPos = InStr(1, strString, ",", vbTextCompare)
Code:
'2 -- Get the last name using a Left Trim
Code:
strLast = Left(strString, (InStr(1, strString, ",", vbTextCompare) - 1))
Code:
'3 -- Get the First name using a combination of Trim and Right Trim
Code:
strFirst = Trim(Right(strString, Len(strString) - (InStr(1, strString, ","))))
Code:
'4 -- see the results!
Code:
MsgBox "Original String: " & strString
MsgBox "Last Name: '" & strLast & "' First Name: '" & strFirst & "'"

End Function

I haven't tried this but if you want to put it in an Access query, try to set one of the fields like this:
Code:
First: Left([tableName].[FieldNameToTrim], (InStr(1, [tableName].[FieldNameToTrim], ",", vbTextCompare) - 1))
Earnie Eng
If you are born once, you will die twice.
If you are born twice, you will die once
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top