Hi Scott,
I recommend splitting this data into two fields, so that you do not need to
run time-consuming functions each time you run your report. One of my
favorite utilities for splitting names in Access is called "Splitter for
Microsoft Access". If you don't mind popping $39 for a copy, you will have a
very good utility that can untangle the messiest name data:
http://www.infoplan.com.au/splitter/
A very easy method to use involves exporting the table to Excel. From within
Excel, use Data > Text to Columns... Then re-import your table back into
Access.
You can also create the new fields in the table and then use an update query
to populate the new fields. The update query would look like this:
Field: FirstName
Table: Your tablename
Update To: SplitName([FullName],0)
Field: LastName
Table: Your tablename
Update To: SplitName([FullName],1)
If you do not wish to permanently split this name data, then create a query
that includes fields similar to this, plus all the other fields that you need
in your report's recordsource:
Field: FirstName: SplitName([FullName],0)
Table: Your tablename
Field: LastName: SplitName([FullName],1)
Table: Your tablename
Add the following function to a new standard module. Name the module
something like basSplitNames (the name of the module *must* be different than
the name of any procedures in your application). Copy and paste the following
function into it. This will create a zero-based array named strResult. The On
Error Resume Next statement will prevent the function from choking to a halt
if there is no middle name for a given record.
Public Function SplitName(FullName As String, _
intElement As Integer) As String
On Error Resume Next
Dim strResult() As String
strResult = Split([FullName], Chr(32))
SplitName = strResult(intElement)
End Function
Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
Scott said:
I have a name field that contains first names or first and last names
together. I want to show only the first name. In a report on the field I
tried setting the Control Source =Left([Name],InStrRev([Name]," ")-1) but it
returned a #Error if there is not a last name (first name only). If there
was a first and last name in the field it returned the first name only which
is what I want. How do I get first names only for the field when in contains
both first and first and last names?
Thanks,
Scott