[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

Parse Text Field in Access

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
Lawrence Salvucci
Asked:
Lawrence Salvucci
  • 4
  • 4
  • 3
  • +2
2 Solutions
 
jerryb30Commented:
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
 
mbizupCommented:
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
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
I mean it's not always in the same element. It could be in the first split using "-" or the fourth split using "-".
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
JimFiveCommented:
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
 
Emil_GrayCommented:
Both of your examples show "RD-" preceding the data you want to extract. Is that always the case?
0
 
mbizupCommented:
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
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
Yes it will always be right after the "RD-".
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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
 
mbizupCommented:
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
 
Emil_GrayCommented:
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
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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
 
Emil_GrayCommented:
Thanks for the points and acknowledgement. I'm glad you found your answer.
0
 
mbizupCommented:
Glad to help out
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now