Solved

Split Text Field into 3

Posted on 2001-08-01
16
458 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:Juggler
  • 4
  • 3
  • 2
  • +5
16 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 6339914
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
 
LVL 3

Expert Comment

by:slymie
ID: 6339916
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
 
LVL 3

Expert Comment

by:slymie
ID: 6339922
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
 
LVL 54

Expert Comment

by:nico5038
ID: 6339951
Hi Simon, A2000 does have a Split function, but A97 not ;-(

Nic;o)
0
 
LVL 3

Expert Comment

by:carruina
ID: 6340051
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
 
LVL 54

Expert Comment

by:nico5038
ID: 6340235
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
 
LVL 3

Expert Comment

by:kenspencer
ID: 6340340
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
 
LVL 6

Expert Comment

by:devtha
ID: 6340458
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 7

Accepted Solution

by:
Nosterdamus earned 150 total points
ID: 6341043
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
 
LVL 7

Expert Comment

by:Nosterdamus
ID: 6341075
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
 
LVL 7

Expert Comment

by:Nosterdamus
ID: 6341078
Sorry......

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

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

0
 
LVL 3

Expert Comment

by:carruina
ID: 6343542
What is the criteria to change for CAMION or SEA?
0
 

Author Comment

by:Juggler
ID: 6346537
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
 
LVL 7

Expert Comment

by:Nosterdamus
ID: 6985853
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
 
LVL 6

Expert Comment

by:devtha
ID: 6986050
:-)
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6999314
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now