Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Parse Text Field in Access

Posted on 2013-12-04
13
Medium Priority
?
614 Views
Last Modified: 2013-12-04
I need to parse out some data in my text field. It's not always in the same place in the text field but it will always be surrounded by a "-" on both ends. I need to extract out what is in between them. Example:

303SS-RD-0.2366-.0002:               Result would be 0.2366
316/316L-RD-1.160-.0003:            Result would be 1.160

How can this be done?
0
Comment
Question by:Lawrence Salvucci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 39695860
When you say 'not always in the same place, do you mean not always the same element (third in a split using "-"), or not the place in the string?

If always the third element:

Function GetValue (a as string) as string
dim x as variant
dim y as string
x = split(a, "-")
y = x(2)
GetValue = y
End function

Called by:
Select getvalue(fieldName) from yourTable)
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39695863
Are you specifically trying to parse out numeric data?

Create a function in a module:

Function GetNumber(strData As String)
   Dim s() As String
   Dim I As Integer
   s() = Split(strData, "-")
   For I = 1 To (UBound(s) - 1)
         If IsNumeric(s(I)) Then
               GetNumber = s(I)
                Exit Function
         End If
   Next
End Function

Open in new window


Then in your query:

SELECT GetNumber([YourFieldName]) as NumericPart FROM YourTable
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39695864
I mean it's not always in the same element. It could be in the first split using "-" or the fourth split using "-".
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:JimFive
ID: 39695875
If it is always in the 3rd position you can use:
Split(field,"-")(2)

If it is not always in the 3rd position you will need to use:
Dim x as variant
x=Split(field, "-")
and then loop through x to find the one you want.  If it is always second to last you can use:
x(ubound(x)-1)
0
 
LVL 8

Expert Comment

by:Emil_Gray
ID: 39695879
Both of your examples show "RD-" preceding the data you want to extract. Is that always the case?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39695890
My comment at http:#a39695863 will return *numeric* data between two dashes.

Your particular example has two parts between two dashes (RD and 1.160), so your request is not clear
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39695891
Yes it will always be right after the "RD-".
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39695908
mbizup: Your example will work but I just realized that sometimes there won't be a trailing "-" after the numeric value I need. Sometimes the value will be at the end of the text field:

I want the numeric values that are preceded by a "-" and most of the time have a trailing dash or nothing after it as with the below example. But it will always start with a 0 or 1 and then a decimal right after the first "-". The "-" is used a few times in the text so I need to find the ones that have numeric values in it, not the "RD". Does that make better sense?

316/316L-RD-0.125
0
 
LVL 61

Accepted Solution

by:
mbizup earned 1800 total points
ID: 39695915
Give this a try:

Function GetNumber(strData As String)
   Dim s() As String
   Dim I As Integer
   s() = Split(strData, "-")
   For I = 1 To UBound(s) 
         If IsNumeric(s(I)) Then
               GetNumber = s(I)
                Exit Function
         End If
   Next
End Function

Open in new window


EDIT:  that simply changed Ubound(s) - 1 to Ubound(s).  The -1 ensured that there was a trailing -.  Dropping that will let you get the numeric data, even if it is the last element.
0
 
LVL 8

Assisted Solution

by:Emil_Gray
Emil_Gray earned 200 total points
ID: 39696158
Use a query. Substitute your field and table for "MyData".

SELECT MyData.MyData, Mid([MyData],InStr([MyData],"RD-")+3) AS Expr2, InStr([Expr2],"-") AS Expr1, IIf([Expr1]=0,[Expr2],Left([Expr2],[Expr1]-1)) AS Expr3
FROM MyData;
MyData.accdb
0
 
LVL 1

Author Closing Comment

by:Lawrence Salvucci
ID: 39697226
I tried both solutions and they worked fine but I went with mbizup's solution. Thank you everyone for your help. I greatly appreciate it.
0
 
LVL 8

Expert Comment

by:Emil_Gray
ID: 39697232
Thanks for the points and acknowledgement. I'm glad you found your answer.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39697290
Glad to help out
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

648 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question