Microsoft Access - How do I trim a long contractor name so that the report shows everything before the "dba"?

Asked By Miss Jones on 27-Aug-12 11:30 AM
I have a report that pulls from a query.  Some of the contractor names in my database are very long because they include dba's (ie: Vanessa Williams dba Mama Lulu's Fresh Kitchen Cooking).  I want the main table to keep the contractor's full name, but when it shows up in a report, I want just the official name - which is everything before the word "dba."  Is it possible to trim this specifically?  Thanks in advance.
Robbe Morris replied to Miss Jones on 27-Aug-12 12:05 PM
You'll need to create a user defined function for this.

 select dbo.MyNameStripper(columnName) as MyName 
   from tableA


In the user defined function, you'd use CHARINDEX to find the string 'dba' and SUBSTRING to grab whatever portion of the string you need.  You'd also want the function to simply return the passed in string if 'dba' were not found.
wally eye replied to Miss Jones on 27-Aug-12 02:20 PM

Or, in MS Access vernacular:

Go to the VBA IDE (Alt-F11), insert a new module and paste in this code:

Public Function DropAfter(ByVal strText As String, ByVal strFind As String) As String

    Dim intPos      As Integer

    intPos = InStr(1, strText, strFind)
    If intPos > 0 Then
      DropAfter = Left(strText, intPos - 1)
    End If

End Function

Save the module.  Then, in your query, use something like:

TrueName: DropAfter([Contractor Name], "dba")

Where [Contractor Name] is the field name you are using to store contractor names.