KjBleau
asked on
converting text field to 3 columns
Hello experts,
This is probably easy peazy for some of you, but I busted the brain trust.
I have one field that needs to be broken into 3 columns in a query.
The cell contains data formatted as thus;
HD_Data
[Category (class)]
VMAC > WMS > Package - Small Package (UPS/FEDEX/Custom)
VMAC > WMS > ePMU
VMAC > WMS > ePMU
VMAC > WMS
VMAC > Inventory
VMAC > EDI
VMAC > Inventory > Paper - Inventory / Purchasing / Receiving / Usage
VMAC > Inventory > Ink Inventory / Purchasing / Receiving / Usage
VMAC > Finance > General Ledger
VMAC > Finance > Account Payable
VMAC
Need to have it look like this, 3 fields, containing the part or the blanks
HD_Data
[Assigned to - Group] [Function] [Issue]
VMAC WMS Package - Small Package (UPS/FEDEX/Custom)
VMAC WMS ePMU
VMAC WMS ePMU
VMAC WMS WMS
VMAC Inventory
VMAC EDI
VMAC Inventory Paper - Inventory / Purchasing / Receiving / Usage
VMAC Inventory Ink Inventory / Purchasing / Receiving / Usage
VMAC Finance General Ledger
VMAC Finance Account Payable
VMAC
This is probably easy peazy for some of you, but I busted the brain trust.
I have one field that needs to be broken into 3 columns in a query.
The cell contains data formatted as thus;
HD_Data
[Category (class)]
VMAC > WMS > Package - Small Package (UPS/FEDEX/Custom)
VMAC > WMS > ePMU
VMAC > WMS > ePMU
VMAC > WMS
VMAC > Inventory
VMAC > EDI
VMAC > Inventory > Paper - Inventory / Purchasing / Receiving / Usage
VMAC > Inventory > Ink Inventory / Purchasing / Receiving / Usage
VMAC > Finance > General Ledger
VMAC > Finance > Account Payable
VMAC
Need to have it look like this, 3 fields, containing the part or the blanks
HD_Data
[Assigned to - Group] [Function] [Issue]
VMAC WMS Package - Small Package (UPS/FEDEX/Custom)
VMAC WMS ePMU
VMAC WMS ePMU
VMAC WMS WMS
VMAC Inventory
VMAC EDI
VMAC Inventory Paper - Inventory / Purchasing / Receiving / Usage
VMAC Inventory Ink Inventory / Purchasing / Receiving / Usage
VMAC Finance General Ledger
VMAC Finance Account Payable
VMAC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<No Points wanted>
Pete,
FWIW,
For me, ...Your code works fine as a function(s):
Public Function SplitField1(Category As Variant) As Variant
SplitField1 = Split([Category], ">")(0)
End Function
Public Function SplitField2(Category As Variant) As Variant
SplitField2 = Split([Category] & ">", ">")(1)
End Function
Public Function SplitField3(Category As Variant) As Variant
SplitField3 = Split([Category] & ">", ">")(2)
End Function
...Then in a query do something like this:
SELECT YourTable.ID, YourTable.Category, SplitField1([Category]) AS AssignedToGroup, SplitField2([Category]) AS Function, SplitField3([Category]) AS Issue
FROM YourTable;
Jeff
Pete,
FWIW,
For me, ...Your code works fine as a function(s):
Public Function SplitField1(Category As Variant) As Variant
SplitField1 = Split([Category], ">")(0)
End Function
Public Function SplitField2(Category As Variant) As Variant
SplitField2 = Split([Category] & ">", ">")(1)
End Function
Public Function SplitField3(Category As Variant) As Variant
SplitField3 = Split([Category] & ">", ">")(2)
End Function
...Then in a query do something like this:
SELECT YourTable.ID, YourTable.Category, SplitField1([Category]) AS AssignedToGroup, SplitField2([Category]) AS Function, SplitField3([Category]) AS Issue
FROM YourTable;
Jeff
ASKER
Thanks Jeff, I was kinda figuring it was a function that was required, but I'm not up on declaring functions in queries.
Can you simply add the block of text from "Public .." to ".. "End Function" somewhere in the SQL statement?
I am working on nested if statements (e.g.: Assigned to: IIf(InStr(1,[Category (class)],">")>0,Left([Cate gory (class)],InStr(1,[Category (class)],">")-1)) but my head is spinning.
Can you simply add the block of text from "Public .." to ".. "End Function" somewhere in the SQL statement?
I am working on nested if statements (e.g.: Assigned to: IIf(InStr(1,[Category (class)],">")>0,Left([Cate
ASKER
I solved it with IIF - thanks for the tips and the lead on functions, it appears that they are much more robust in that there were only a (relatively) few lines of code compared to this;
Assigned to: IIf((InStr(1,[Category (class)],">")=0),[Category (class)],IIf((InStr(1,[Cat egory (class)],">")+InStr(InStr( 1,[Categor y (class)],">")+1,[Category (class)],">"))=InStr(1,[Ca tegory (class)],">"),Left([Catego ry (class)],InStr(1,[Category (class)],">")-1),IIf(InStr (1,[Catego ry (class)],">")=0,[Category (class)],IIf((InStr(1,[Cat egory (class)],">")+InStr(InStr( 1,[Categor y (class)],">")+1,[Category (class)],">"))>InStr(1,[Ca tegory (class)],">"),Left([Catego ry (class)],InStr(1,[Category (class)],">")-1),"false")) ))
Function: IIf((InStr(1,[Category (class)],">")=0),"",IIf((I nStr(1,[Ca tegory (class)],">")+InStr(InStr( 1,[Categor y (class)],">")+1,[Category (class)],">"))=InStr(1,[Ca tegory (class)],">"),"",IIf(InStr (1,[Catego ry (class)],">")=0,[Category (class)],IIf((InStr(1,[Cat egory (class)],">")+InStr(InStr( 1,[Categor y (class)],">")+1,[Category (class)],">"))>InStr(1,[Ca tegory (class)],">"),Mid$([Catego ry (class)],InStr(1,[Category (class)],">")+2,(InStr(InS tr(1,[Cate gory (class)],">")+1,[Category (class)],">")-InStr(1,[Cat egory (class)],">")-2)),"false") )))
Issue: IIf((InStr(1,[Category (class)],">")=0),"",IIf((I nStr(1,[Ca tegory (class)],">")+InStr(InStr( 1,[Categor y (class)],">")+1,[Category (class)],">"))=InStr(1,[Ca tegory (class)],">"),"",IIf(InStr (1,[Catego ry (class)],">")=0,[Category (class)],IIf((InStr(1,[Cat egory (class)],">")+InStr(InStr( 1,[Categor y (class)],">")+1,[Category (class)],">"))>InStr(1,[Ca tegory (class)],">"),Right$([Cate gory (class)],Len([Category (class)])-InStr(InStr(1,[C ategory (class)],">")+1,[Category (class)],">")-1),"false")) ))
Assigned to: IIf((InStr(1,[Category (class)],">")=0),[Category
Function: IIf((InStr(1,[Category (class)],">")=0),"",IIf((I
Issue: IIf((InStr(1,[Category (class)],">")=0),"",IIf((I
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for KjBleau's comment #a38296038
for the following reason:
it was my own solution, and I didn't much like it. Brute force.
Accepted answer: 0 points for KjBleau's comment #a38296038
for the following reason:
it was my own solution, and I didn't much like it. Brute force.
ASKER
ok, my brute force solution was like the cologne, it stunk .. I'm trying to create a function now
ASKER
Wow, amazing what the nakit light of day will do for your head.
I did learn to create a function (turns out that, that is the simple part ) but I am getting an error
" Run-time error '9': subscript out of range"
due (I belive) to some of the values in the [Category (class)] field NOT having a bracket.
in fact, there are a few that are NULL value
Looking (again) at the "split" function to see if I can figure it out, but any help from the 2 orginal guru's would be appreciated.
I did learn to create a function (turns out that, that is the simple part ) but I am getting an error
" Run-time error '9': subscript out of range"
due (I belive) to some of the values in the [Category (class)] field NOT having a bracket.
in fact, there are a few that are NULL value
Looking (again) at the "split" function to see if I can figure it out, but any help from the 2 orginal guru's would be appreciated.
FWIW, I am sure Pete could have gotteon his solution working for you.
ASKER
Thanks <all> I am happy to point the points to Pete per Jeff's request.
I have temporarily negated the blanks and the fields with no bracket in the text by eliminating those records from the query <WHERE (((InStr(1,[Category (class)],">"))>"0"))> but that is only stop gap since the records are there and (from the perspective of the data) valid.
Any more great hints .. did I say thanks for the function?
Ken
I have temporarily negated the blanks and the fields with no bracket in the text by eliminating those records from the query <WHERE (((InStr(1,[Category (class)],">"))>"0"))> but that is only stop gap since the records are there and (from the perspective of the data) valid.
Any more great hints .. did I say thanks for the function?
Ken
Ken, I am not sure what happened here....
The function I posted (That is based 100% on Pete's post) worked fine for me.
See the attached sample
So test this sample DB.
If it works, ...and you can get it working in your DB, then please award the points to Pete.
;-)
JeffCoachman
Database54.mdb
The function I posted (That is based 100% on Pete's post) worked fine for me.
See the attached sample
So test this sample DB.
If it works, ...and you can get it working in your DB, then please award the points to Pete.
;-)
JeffCoachman
Database54.mdb
ASKER
I didn't mean to award any points. The function worked with the data provided in the sample. Unfortunately, the sample didn't provide the two other types: blank and values without a bracket. When it comes across those, it fails.
Then how about posting some actual data... in a sample DB?
ASKER
Columns A -> J is what comes in CSV
HD-Data.xlsx
HD-Data.xlsx
<The function worked with the data provided in the sample. Unfortunately, the sample didn't provide the two other types: blank and values without a bracket. When it comes across those, it fails. >
You will have to post a clear example of what this means.
I tried the function on the data in the spreadsheet and nothing "Failed"
See attached file, qryHD_Data
Database54.mdb
You will have to post a clear example of what this means.
I tried the function on the data in the spreadsheet and nothing "Failed"
See attached file, qryHD_Data
Database54.mdb
ASKER
was missing "option explicit" in my moduel .. not sure if that was the issue, but added it and it works!
ASKER
I'm gonna keep toying with SPLIT though