InstrRev - Strip out end of text, where multiple parenthesis

I need to compare 2 fields if field 2 is null then look at field 1 (text string) and strip out the end of text where data is within ( ).  However the text string can contain multiple sets of parenthesis, I am only interest in the last set at the end of the text string.  How do I strip out the data between the Last Parenthesis?

Sample
Courses for the Employee (members) Must take (TR1092)
Results should be:  TR1092

Note that the length of the data between the last () can vary  looking for a function to use with a query..

Karen
Karen SchaeferBI ANALYSTAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iandianCommented:
You should use the mid function.
http://msdn.microsoft.com/en-us/library/05e63829%28VS.80%29.aspx

something like this:

if isnull(field2) then
  dim result = mid(field1, instrrev(field1, "("), instrrev(field1, ")"))
end if

Open in new window

0
mbizupCommented:
This will do it.  I'm assuming you can have characters after the last  ")"

mid(S, instrrev(s,"(") + 1, instrrev(s,")")- instrrev(s,"(")-1)
0
Karen SchaeferBI ANALYSTAuthor Commented:
No characters after last )

Still getting the data within the parenthesis and I need those stripped off also.

1st try's Results currently getting:

(9HBGMDFODC)
(TR007262)
(TR007262)
(FINEST255)
(FINEST255)

2nd attempt gives error:
Invalid procedure call or argument (Error 5)


Function CleanUPTxt(s As String)
If IsNull(s) = False Then
  s = Mid(s, InStrRev(s, "("), InStrRev(s, ")"))
  Debug.Print s
End If

Or 

Function CleanUPTxt(s As String)
If IsNull(s) = False Then
  s = Mid(s, InStrRev(s, "(") - 1, InStrRev(s, ")") - InStrRev(s, "(") - 1)
  Debug.Print s
End If

End Function

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Karen SchaeferBI ANALYSTAuthor Commented:
Also need to handle if there are no parenthesis then skip it an move to the next
K
0
Karen SchaeferBI ANALYSTAuthor Commented:
This what I have so far, however the code runs but does not return an actual value - what am I missing.

I need it to display the CourseNO if not null or if isnull then the stripped out Course number from the Title field.

SELECT TL_CourseList.[Ilp Learning Title], TL_CourseList.[Ilp Learning Cd], CleanUPTxt([Ilp Learning Title]) AS CourseNo
FROM TL_CourseList LEFT JOIN TL_Training ON TL_CourseList.[Ilp Learning Cd] = TL_Training.TrainingCourseNo
GROUP BY TL_CourseList.[Ilp Learning Title], TL_CourseList.[Ilp Learning Cd], CleanUPTxt([Ilp Learning Title]);


Function CleanUPTxt(s As String)
If IsNull(s) = False Then
    On Error GoTo cont:
  s = Mid(s, InStrRev(s, "(") + 1, InStrRev(s, ")") - InStrRev(s, "(") - 1)
  Debug.Print s
cont:
End If

End Function

Open in new window

0
mbizupCommented:
You need to define your function using a datatype, and then explicitly set your function return by using the function's name:

Function CleanUPTxt(s As String) As String  '<--- define it as string
If IsNull(s) = False Then
    On Error GoTo cont:
  s = Mid(s, InStrRev(s, "(") + 1, InStrRev(s, ")") - InStrRev(s, "(") - 1)
  Debug.Print s
  CleanUPTxt = s  '<---- Set the function's return value.
cont:
End If

End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karen SchaeferBI ANALYSTAuthor Commented:
Thanks that did the trick once I included

IIf(IsNull([Ilp Learning Cd]),CleanUPTxt([Ilp Learning Title]),[Ilp Learning Cd]) into the query.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.