MS Access Forum / New Users / October 2007
Extracting data from strings
|
|
Thread rating:  |
Sgurdon - 10 Mar 2007 02:01 GMT I have some text strings that I need to extract specific data from.
SAMPLES: Stored ITEM: 4610201465 QTY: 5 LOC: A 105-130-C
Rewarehouse Pick: 4610060729 QTY: 44 LOC: A 101-111-T SERIAL:
Rewarehouse Stored: 4610060729 QTY: 44 LOC: A 101-111-A SERIAL: I need to extract the task: Stored Item; Rewarehouse pick; Rewarehouse Stored. The task always ends at the ":". I need to extract the item number. It is usually 10 characters but some are 14 characters, The item number is always followed by QTY. I need to extract the QTY. It is usually a number from 0 to 10,000. It always follows the Qty's ":" and ends 1 space before the LOC. Finally, I need to extract the LOC. It is always 11 characters. In the past I have used Excel and used the MID, SEARCH, and other functions to extract from the strings but I would like to import the CSV file directly into Access and do it all from there. Thanking everyone in advance. Sgurdon
 Signature Always in search of knowledge
Seth Schwarm - 10 Mar 2007 05:50 GMT If you were successful in Excel, then this will be a snap for you in Access.
Instead of the SEARCH function use the InStr function along with Left, Mid and Right and you'll be done in no time.
You would do this in a query, not a table.
Seth Schwarm
> I have some text strings that I need to extract specific data from. > [quoted text clipped - 18 lines] > Thanking everyone in advance. > Sgurdon Sgurdon - 10 Mar 2007 07:59 GMT Not snapping in Access; maybe i'm missing something. This the formula I used in Excel to extract the first variable, how does it translate to Access?
Cell A2=date Cell B2=Stored ITEM: 4610201465 QTY: 5 LOC: A 105-130-C
This is the function I use: "=MID(B2,1,SEARCH(":",B2)-1)" it brings back Stored ITEM
If I can get that translated to use the InStr function I should be able to figure how to use it to pull all of the tasks.
Expr1: Mid([action],1,InStr(":",[action])-1)
I tried replacing "Search" with "InStr" and "B2" with "[action]", the name of the field containing the string, but it returns an error message.
 Signature Always in search of knowledge
> If you were successful in Excel, then this will be a snap for you in Access. > [quoted text clipped - 27 lines] > > Thanking everyone in advance. > > Sgurdon Tom Lake - 10 Mar 2007 12:14 GMT > Not snapping in Access; maybe i'm missing something. > This the formula I used in Excel to extract the first variable, how does [quoted text clipped - 6 lines] > This is the function I use: "=MID(B2,1,SEARCH(":",B2)-1)" it brings back > Stored ITEM If Stored ITEM is all numeric, here's one way: =Trim(Str(Val(Mid(a$, 14))))
The 14 gets you past the "Stored ITEM:" , Val takes the value of stored item Str converts it to a string and Trim trims off the sign space.
Make sure the field name in the record is B2 and that the control on your form or report is NOT called B2.
Tom Lake
Tom Lake - 10 Mar 2007 12:19 GMT > If Stored ITEM is all numeric, here's one way: > =Trim(Str(Val(Mid(a$, 14)))) > > Make sure the field name in the record is B2 and that the control on your > form or report is NOT called B2. Oops! I changed B2 to a$ in testing and didn't change it back, The above should be:
=Trim(Str(Val(Mid(B2, 14))))
Tom Lake
Sgurdon - 11 Mar 2007 01:18 GMT Thanks Tom, I will try this once I achieve the results I need using the InStr function. I don't want to get thrown off of my current track, but I can see that this formula will come in handy.
 Signature Always in search of knowledge
> > If Stored ITEM is all numeric, here's one way: > > =Trim(Str(Val(Mid(a$, 14)))) [quoted text clipped - 8 lines] > > Tom Lake Seth Schwarm - 10 Mar 2007 18:14 GMT Sgurdon:
Ok, well sorry - I assumed you knew more about Access. Let's back up and discuss a couple things.
You will import your data into a table. A table stores data and not formulas. The only similarity between Access' table and Excel is the fact they look similar and each box is called a cell. The similarities absolutely end there. Now you are in Access world - things are different and you accomplish tasks using different objects versus cell or sheet manipulation.
I am not going to explain the details of a table and its design view versus data view, but you need to conceptualize each row is a record and each column is a field in that record and you don't/can't store formulas/calculated values in a table. Instead we use queries.
In your case you need to create a new query in design view, then add the table which contains your imported data onto the Layout Area (top part of query design screen). The grided area is called the Design Grid. You drag fields from tables in the Layout Area down to a column in the Design Grid. You also create formulas (referred to as calculated fields in Access) in the Design Grid.
In your case you do not need to drag any fields down to the design to build a calculated field. Your formulas will be built along the same logic used in Excel, using many of the same function names you used in Excel (except SEARCH is now what we call InStr). In Access we don't use the equals sign (=) in formulas in queries; it is improper syntax. Just begin typing your formula in the row labeled Field.
In Access we don't refer to data by using cell intersection names such as A2, G74; we use field names. When you write a formula it is assumed/implied you are working with a record in a table. You don't reference fields/values in other records, the context is the one record in the table you are working with. Most folks really struggle with this coming from Excel, but it just is.
So anytime you are thinking of referring to cell A1 you would instead type the field name from the table you are working with in the Layout Area. Remember, cell references are a thing of Excel, not Access. Now you are working with fields (columns) and records (rows). Once you enter a formula in a query it will calculate a value for each record in the table. There is no AutoFilling or copying and pasting of formulas. What you do for one record is done for all records in a query.
A note about calculated fields: after you enter your formula, then tab out of the field or click elsewhere you will notice a name is given to your to your formula - Expr1:. This is because in a sense you have created a new 'field' and every field must have a name. You will notice this name as the column header where you are used to seeing A, B, C, D, E, F. You can rename the field to something meaningful by selecting Expr and overwriting it. You will learn in time to do this when entering your formula similar to - Item: Mid( . . .
See how this helps you, and then come back with additional questions.
Seth Schwarm
> Not snapping in Access; maybe i'm missing something. > This the formula I used in Excel to extract the first variable, how does it [quoted text clipped - 45 lines] > > > Thanking everyone in advance. > > > Sgurdon Sgurdon - 11 Mar 2007 01:15 GMT Thank you Seth, I haven't gotten the results I need yet but you have put me on the right track. I will have what I need shortly with a little experimentation and practice.
 Signature Always in search of knowledge
> Sgurdon: > [quoted text clipped - 102 lines] > > > > Thanking everyone in advance. > > > > Sgurdon Carmen - 30 Oct 2007 18:41 GMT Seth, I have a similar request. I need to extract the decimal and digits past the second "*" in the example below. I entered this funtion: Expr1: InStr("*.",[Rate]), but just got ones and zeros, so don't really know how it works. [Rate] is my field name.
0*999999999999*.008
Problem is that sometimes there are multiple digits to the right of the decimal. This is data stored as text. I don't have access to the original data, so I can't change the report, I can only attempt to extract the data I need. I will receive these reports weekly.
Thank you for your help!
> Sgurdon: > [quoted text clipped - 102 lines] > > > > Thanking everyone in advance. > > > > Sgurdon John Spencer - 30 Oct 2007 20:09 GMT What are the rules for deciding? Are there always two asterisks (*)? Do you always have an asterisk followed by a period as the breakpoint?
Is the first asterisk always in the 2nd position?
If the first asterisk always occurs in the first2 characters, then this expression should work
Mid([TheField],Instr(3,[TheField],"*")+1)
If the first asterisk appears somewhere in the string then you need a slightly more complex expression
Mid([TheField],Instr(Instr(1,[TheField],"*")+1 ,[TheField],"*")+1)
I suggest you look up the Mid and Instr functions in the VBA help for an understanding of what they do.
 Signature John Spencer Access MVP 2002-2005, 2007 Center for Health Program Development and Management University of Maryland Baltimore County .
> Seth, I have a similar request. I need to extract the decimal and digits > past the second "*" in the example below. I entered this funtion: Expr1: [quoted text clipped - 156 lines] >> > > > Thanking everyone in advance. >> > > > Sgurdon
|
|
|