Solved

Parse Text Field in Access

Posted on 2013-12-04
13
486 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now