[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Split Text Field into 3

Posted on 2001-08-01
16
Medium Priority
?
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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
 
LVL 7

Accepted Solution

by:
Nosterdamus earned 600 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

650 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