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
JugglerAsked:
Who is Participating?
 
NosterdamusConnect With a Mentor Commented:
Hi Juggler,

Place the following code in a new Module, and run sub Test().

Option Compare Database
Option Explicit

Public Const SEPARATOR_CHAR = "."

Public Function SplitString(strTheString As String, _
                            ByRef strField1 As String, _
                            ByRef strField2 As String, _
                            ByRef strField3 As String, _
                            Optional strSeparator As String = SEPARATOR_CHAR) As Integer
    Const TERMINATED_OK = 0
    Const ERR_FORMAT = -1
    Const MAX_SEPARATOR_CHARS = 6
   
    On Error GoTo Err_SplitString
   
    Dim strTemp As String
    Dim nTotalSeparatorChars As Byte
    Dim nFieldLen As Long
   
    'Set default result to return
    nTotalSeparatorChars = 0
    strTemp = strTheString
   
    'Count how meny SEPARATOR_CHARs in string
    nFieldLen = InStr(1, strTemp, strSeparator, vbTextCompare)
    While nFieldLen > 0
        nTotalSeparatorChars = nTotalSeparatorChars + 1
        strTemp = Right(strTemp, Len(strTemp) - nFieldLen)
        nFieldLen = InStr(1, strTemp, strSeparator, vbTextCompare)
        If nFieldLen < 3 Then
            'Format of date expected is ##.##.##, therefore, nFieldLen MUST
            'be => 3!
            nFieldLen = 0
        End If
    Wend
   
    'Determine if enough SEPARATOR_CHARS in string
    If nTotalSeparatorChars = MAX_SEPARATOR_CHARS Then
        'Ok to split
        strTemp = strTheString
        nFieldLen = InStr(1, strTemp, strSeparator, vbTextCompare) + 5
        strField1 = Left(strTemp, nFieldLen)
        strTemp = Right(strTheString, Len(strTheString) - Len(strField1) - 1)
        nFieldLen = InStr(1, strTemp, strSeparator, vbTextCompare) + 5
        strField2 = Left(strTemp, nFieldLen)
        strField3 = Trim(Right(strTheString, Len(strTheString) - (Len(strField1) + Len(strField2) + 1)))
        strField1 = Trim(strField1)
        strField2 = Trim(strField2)
       
    Else
        'Wrong string format
        SplitString = ERR_FORMAT
    End If
   
Exit_SplitString:
    Exit Function

Err_SplitString:
    SplitString = Err.Number
    GoTo Exit_SplitString
End Function



Sub Test()
    Const DATA1 = "BY AIR 01.09.01-3 X APP BY 12.08.01  3 X PRD BY 25.08.01"
    Const DATA2 = "BY SEA 13.10.01-3 X APP BY 12.09.01  3 X PRD BY 24.09.01"
    Const DATA3 = "BY CAMION 09.11.01-3 X APP BY 14.10.01  3 X PRD BY 20.10.01"
    'The following will result an error
    Const DATA4 = "BY AIR 01/09/01-3 X APP BY 12/08/01  3 X PRD BY 25/08/01"
   
    Dim strField1 As String
    Dim strField2 As String
    Dim strField3 As String
    Dim Result As Integer
   
    'To get the correct result for Const DATA4, use:
    'Result = SplitString(DATA4, strField1, strField2, strField3, "/")
    Result = SplitString(DATA1, strField1, strField2, strField3)
    If Result = 0 Then
        'Here you can add the fields into the table, like
        '   Dim RSt As Recordset
        '   Set Rst = CurrentDB.OpenRecordSet(MyTable)
        '   With Rst
        '      .AddNew
        '      !Field1 = strField1
        '      !Field2 = strField2
        '      !Field3 = strField3
        '      .Update
        '   End With
        '
        'or
        MsgBox "The original string is:" & vbCrLf & vbCrLf & _
               "Field1: " & strField1 & vbCrLf & _
               "Field2: " & strField2 & vbCrLf & _
               "Field3: " & strField3, vbOKOnly, "Split String Result"
    Else
        'Here you can write a command to insert the problematic string into
        'an Err_Table for later inspection
        'or
        MsgBox "Error " & IIf(Result = -1, " with the format", "No: " & Result), _
               vbCritical + vbOKOnly, "Split String Error"
    End If
End Sub


Hope this helps,

Nosterdamus
0
 
nico5038Commented:
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)
0
 
slymieCommented:
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
slymieCommented:
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
0
 
nico5038Commented:
Hi Simon, A2000 does have a Split function, but A97 not ;-(

Nic;o)
0
 
carruinaCommented:
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



0
 
nico5038Commented:
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)
0
 
kenspencerCommented:
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
0
 
devthaCommented:
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..

0
 
NosterdamusCommented:
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
0
 
NosterdamusCommented:
Sorry......

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

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

0
 
carruinaCommented:
What is the criteria to change for CAMION or SEA?
0
 
JugglerAuthor Commented:
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
0
 
NosterdamusCommented:
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
0
 
devthaCommented:
:-)
0
 
NetminderCommented:
Per recommendation, force-accepted by
Netminder
CS Moderator

nico5038: points for you at http://www.experts-exchange.com/jsp/qShow.jsp?ta=msaccess&qid=20298826
slymie: points for you at http://www.experts-exchange.com/jsp/qShow.jsp?ta=msaccess&qid=20298830
carruina: points for you at http://www.experts-exchange.com/jsp/qShow.jsp?ta=msaccess&qid=20298834
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.