Solved

Parse Text Field in Access

Posted on 2013-12-04
13
537 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
  • 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
Independent Software Vendors: 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 450 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 50 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

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!

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

726 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