Solved

Parse Text Field in Access

Posted on 2013-12-04
13
515 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

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…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

777 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