Link to home
Start Free TrialLog in
Avatar of Juggler
Juggler

asked on

Split Text Field into 3

Hi

I have an Access db that has a table imported from an external db (not Access).  This table has a free text field with date similar to: BY AIR 01.09.01-3 X APP BY 12.08.01  3 X PRD BY 25.08.01, I  need to split this and add the data to another table: "BY AIR 01.09.01" (field1), "3 X APP BY 12.08.01" (field2), "3 X PRD BY 25.08.01" (field3).  The word AIR can be replaced by either SEA or CAMION. As the data is entered as free text, there can of course be extra spaces, but the data above will always appear.  Is this possible?.  I do not have a very good knowledge of vb, sv any answer will need to be quite detailed.

Thanks for your help

Stephen
Avatar of nico5038
nico5038
Flag of Netherlands image

The main issue will be the reliability of your "separator character(s)":
BY AIR 01.09.01-3 X APP BY 12.08.01  3 X PRD BY 25.08.01
can be split e.g. on the "-" and/or by the "X" and/or by the occurrence of the trailing "##.##.##" (date?)
Another question is the fact whether or not this is a once off excersize. If so, you can choose for a rough split and a little manual correction afterwards.

I would to have to see a little more to make a final judgement about an algorithm that would solve your problem.
You can drop (if allowed) a zipped (part of) the file in my nico5038 mailbox at yahoo.com and I'll have a look.

Nic;o)
Avatar of slymie
slymie

Try something like this:

'--------------------------------------
  Dim data As String
  data = "BY AIR 01.09.01-3 X APP BY 12.08.01  3 X PRD BY 25.08.01"
 
  Dim field1, field2, field3, t
  Dim count As Long, i As Long
  Dim hadspace As Boolean
 
  t = Split(data, "-")
  field1 = Trim(t(0))
 
  t = Split(t(1), " ")
  For i = LBound(t) To UBound(t)
    If Not t(i) = "" Then
      count = count + 1
      If count > 5 Then
        field3 = field3 & " " & t(i)
      Else
        field2 = field2 & " " & t(i)
      End If
    End If
  Next

  field2 = Trim(field2)
  field3 = Trim(field3)
 
  Debug.Print field1 & vbCrLf & field2 & vbCrLf & field3 & vbCrLf
'--------------------------------------

This works in VB, not sure if Access support Split but i dont see why not.

HTH,
Simon
nico5038 is quite right though, this is all assuming you can rely on:

(1) a dash '-' separating the first set of data from the next two

(2) the second field always having 5 space-separated values (extra spaces will be ignored, as long as they are spaces and not tabs or anything like that).

-
Simon
Hi Simon, A2000 does have a Split function, but A97 not ;-(

Nic;o)
if all records have the same length you can use a query like this:


INSERT INTO Tabla3 ( Expr1, Expr2, Expr3 )
SELECT "BY SEA" & Mid([Import],7,9) AS Expr1, Mid([Import],16,20) AS Expr2, Mid([Import],38,19) AS Expr3
FROM Tabla2;


Where
Tabla3 is the name of the table where you want add the records
Expr1, Expr2 and Expr3 are the split fields

Import is the original field
Tabla2 is the source Table



carruina,

The line:
>The word AIR can be replaced by either SEA or CAMION. As the data is
entered as free text, there can of course be extra spaces, but the data above will always appear.  

Implies that "fixed length" is not the case....

Nic;o)
Hi,

If the dates are always present in the ##.##.## format and always at the end of the field you want, then I would favor scanning for the '.' in order to get the data (what Nico alluded to above).

Ken
Send me the text file of the table or just the column using a query.
I shall fix it and send it back to you. devthaa@yahoo.com
Thanks..

ASKER CERTIFIED SOLUTION
Avatar of Nosterdamus
Nosterdamus
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Feel free to set line:
Result = SplitString(DATA1, strField1, strField2, strField3)

to
Result = SplitString(DATA2, strField1, strField2, strField3)

or to
Result = SplitString(DATA1, strField1, strField2, strField3)


If you want to get a correct result for DATA4, then set the lines:

   'To get the correct result for Const DATA4, use:
   'Result = SplitString(DATA4, strField1, strField2, strField3, "/")
   Result = SplitString(DATA1, strField1, strField2, strField3)

to
   'To get the correct result for Const DATA4, use:
   Result = SplitString(DATA4, strField1, strField2, strField3, "/")
   'Result = SplitString(DATA1, strField1, strField2, strField3)


And one more thing:
these 2 lines:

       strField3 = Trim(Right(strTheString, Len(strTheString) - (Len(strField1) + Len(strField2) +
1)))

should be in ONE line (otherwize you'll get an Editor Syntax error)

If you need more, drop a line.

Nosterdamus
Sorry......

or to
Result = SplitString(DATA1, strField1, strField2, strField3)

should be:
or to
Result = SplitString(DATA3, strField1, strField2, strField3)

What is the criteria to change for CAMION or SEA?
Avatar of Juggler

ASKER

Hi

Thanks for all the advise - I will work through the above in the next day or so when I get a bit of spare time!.  Just a couple of quick answers  - this is not going to be a one off exercise, it will be very much ongoing, otherwise a quick fix would be better!.  The criteria of Air, Sea or Camion is the chosen method of transport ( I work for an importer).  Would it be easier to split if the users of the other db could be trained (persuaded) to use particular characters as seperators?, although as the field is free text, the spacing is bound to change!

Thanks all again, I will report back (seek more help!)

Stephen
For Juggler,

It's time to resolve this question, so I will leave a recommendation in Community Support Topic Area that this question is:
- Answered by: nico5038 (50 pts), slymie (50 pts), carruina (50 pts), Nosterdamus (150 pts).

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Thanks!
Nosterdamus
:-)