splitting string field into fields access query OR vba

Hi,
I have a field with this string:
:20:111111251540002D
:23B:CREASDD
:32A:09A1237CHF41912,05
:33B:CHASD1912,05
:50K:/CH110023412400243526
MSFSDFER KFSFUTDFR
BUDFSFERSTR. 2
8113 BOSDFSFSLSEN
:53A:UBSWCHZH80A
:57D:SFS SDFSDFS
RASDASDA
BRANCH 000
GOGOGOG
:59:/43434
DFSDFL GRSDFOUP
SDFDF + DF LTFDD
DFD HDFDN 343423
GOGOER
:71A:OER


how can i split this string into new field in such a query by this rule:
bring me all the string after ":20:" Until the next ":", etc
result :
field_:20: =111111251540002D
field_:23B:=CREASDD
field_:32A:=09A1237CHF41912,05
ETC.....
drtopservAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
@drtopserv

using the information you posted, you only need a query to get what you need.

see  http:#a40527946
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
Select MyField From Table1 Where Left(MyField, 1)=":"
0
 
drtopservAuthor Commented:
Sorry,seems not understand my Q.
What i`m trying to tell, that this exact long string in the field needed to be devided to to many strings and put them in other fields (mentioned in the result . look at the result that i need when executing the solution.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
SimonCommented:
Please provide some more examples of the field content and the table structure that you want to split it into, as it is not completely clear from your example and we don't want to waste time providing a solution that you don't want.

e.g.
Do you want to split the field at every newline (apparently not)
Do you want to split the field at every [newline followed by colon then alphanumeric string then another colon] (yes?)
please clarify: do all examples of this data have the same "field identifiers" (20,23B,32A,33B,50K...)
should the 50k field include everything up to 53A:
0
 
HooKooDooKuCommented:
I don't know how something this complex could be done in SQL, but it seems a pretty strait-forward solution to simply do some VB code in a loop with the InStr() function and the optional 'start' parameter.  Something like this:
Str = YourBigString
'Find the Start of the 1st Field
P1 = InStr( Str, ":")
Do While P1 > 0
  'Find End of the Next Field
  P2 = InStr(P1+1, Str, ":")
  If P2 = 0 Then
    Exit Do  'String not formated as expected
  End If
  FieldName = "Field_" & Mid$( Str, P1, P2 - P1 + 1 )
  'Find Data
  P1 = P2 + 1
  P2 = InStr( P1, Str, ":" )
  If P2 > 0 Then
    DataStr = Mid$( Str, P1, P2 - P1 ) 'No +1 because the : found starts the next field name
  Else
    DataStr = Mid$( Str, P1 )  'Extracts to end of string
  End If
  'P2 is now the start of the next field name
  P1 = P2 'If no final : found, the while loop will end
  Fields(FieldName).Value = DataStr
Loop

Open in new window

0
 
Jamie GarrochConnect With a Mentor PowerPoint Consultant & DeveloperCommented:
You ciuld use the Split function in VBA to load each line of the string into an array and then address that array in the way you want:

Function SplitString (myString As String) As Single
  Dim myArray() as String
  myArray = Split (myString, vbCrLf)
  SplitString = Ubound(myArray)
End Function

Open in new window


The vbCrLf delimiter would need checking as I can't see what character is used in your example.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Slight tweak to what Jamie suggested to give you what you want:

 Function SplitString (myString As String,intToken as Integer) As String
 
  Dim myArray() as Variant

  myArray = Split (myString,":")
  SplitString = myArray(intToken)

End Function

  Your call would be the string/field with the entire date, and the nth token you want returned from it.

Jim.
0
 
Rey Obrero (Capricorn1)Commented:
you can simply use a query, f1 is the field that contains the string

select f1, left(f1,instrrev(f1,":")) as result1, mid(f1,instrrev(f1,":")+1) as result2
from yourTable
where instr(f1,":") >0

with this info
:20:111111251540002D

the result will be

f1                                           result1    result2
:20:111111251540002D    :20:          111111251540002D



i already posted this yesterday and the thread disappeared????
0
 
drtopservAuthor Commented:
kewl Rey,
but i need to keep going and extract others  :Anystring:
means:
f1                                                          result1    result2
:20:111111251540002D                    :20:          111111251540002D
f1                                                          result1    result2
:23B:CREASDD                                   :23B:        CREASDD
f1                                                         result1    result2                    
:50K:/CH110023412400243526     :50K:        /CH110023412400243526    
MSFSDFER KFSFUTDFR                                    MSFSDFER KFSFUTDFR
BUDFSFERSTR. 2                                               BUDFSFERSTR. 2
8113 BOSDFSFSLSEN                                       8113 BOSDFSFSLSEN

Etc.....
0
 
drtopservAuthor Commented:
to clarify this :
the field i mention in my question has the long string :
:20:111111251540002D
:23B:CREASDD
:32A:09A1237CHF41912,05
:33B:CHASD1912,05
:50K:/CH110023412400243526
MSFSDFER KFSFUTDFR
BUDFSFERSTR. 2
8113 BOSDFSFSLSEN
:53A:UBSWCHZH80A
:57D:SFS SDFSDFS
RASDASDA
BRANCH 000
GOGOGOG
:59:/43434
DFSDFL GRSDFOUP
SDFDF + DF LTFDD
DFD HDFDN 343423
GOGOER
:71A:OER

that i would like to split into many new fields,and the delimiter should me ->  :numeronly OR numer&Char:
as you can see : :20:  ,  :23B:  , :32A:   ,  :33B:   ,   :50K:   ,  :53A:  ,  :57D:
0
 
aikimarkConnect With a Mentor Commented:
The following function parses your strings with a regexp object.
Public Function Q_28589320(parmString) As Object
    Dim oRE As Object
    Dim oMatches As Object
    Dim oM As Object
    Dim oSM As Object
    Dim lngSM As Long
    Dim oDic As Object
    
    If oRE Is Nothing Then
        Set oRE = CreateObject("vbscript.regexp")
        oRE.Global = True
        oRE.Pattern = "(:[^:]*:)([^:]*)"
    End If
    
    Set oDic = CreateObject("scripting.dictionary")
    
    If IsNull(parmString) Then
        Set Q_28589320 = oDic
        Exit Function
    End If
    
    If oRE.test(parmString) Then
        Set oMatches = oRE.Execute(parmString)
        For Each oM In oMatches
            With oM
                oDic(.submatches(0)) = .submatches(1)
            End With
        Next
    End If
    Set Q_28589320 = oDic
    Set oDic = Nothing
End Function

Open in new window


I tested the function with the following code:
Sub main()
    Dim strThing As String
    Dim dicThing As Object
    Dim vItem As Variant
    
    strThing = ":20:111111251540002D" & vbCrLf & _
                ":23B:CREASDD" & vbCrLf & _
                ":32A:09A1237CHF41912,05" & vbCrLf & _
                ":33B:CHASD1912,05" & vbCrLf & _
                ":50K:/CH110023412400243526" & vbCrLf & _
                "MSFSDFER KFSFUTDFR" & vbCrLf & _
                "BUDFSFERSTR. 2" & vbCrLf & _
                "8113 BOSDFSFSLSEN" & vbCrLf & _
                ":53A:UBSWCHZH80A" & vbCrLf & _
                ":57D:SFS SDFSDFS" & vbCrLf & _
                "RASDASDA" & vbCrLf & _
                "BRANCH 0" & vbCrLf & _
                "GOGOGOG" & vbCrLf & _
                ":59:/43434" & vbCrLf & _
                "DFSDFL GRSDFOUP" & vbCrLf & _
                "SDFDF + DF LTFDD" & vbCrLf & _
                "DFD HDFDN 343423" & vbCrLf & _
                "GOGOER" & vbCrLf & _
                ":71A:OER"
    Debug.Print strThing
    Debug.Print "==================================="
    
    Set dicThing = Q_28589320(strThing)
    For Each vItem In dicThing
        Debug.Print vItem, dicThing(vItem);
    Next
End Sub

Open in new window


The test produced the following results in the Immediate window
:20:111111251540002D
:23B:CREASDD
:32A:09A1237CHF41912,05
:33B:CHASD1912,05
:50K:/CH110023412400243526
MSFSDFER KFSFUTDFR
BUDFSFERSTR. 2
8113 BOSDFSFSLSEN
:53A:UBSWCHZH80A
:57D:SFS SDFSDFS
RASDASDA
BRANCH 0
GOGOGOG
:59:/43434
DFSDFL GRSDFOUP
SDFDF + DF LTFDD
DFD HDFDN 343423
GOGOER
:71A:OER
===================================
:20:          111111251540002D
:23B:         CREASDD
:32A:         09A1237CHF41912,05
:33B:         CHASD1912,05
:50K:         /CH110023412400243526
MSFSDFER KFSFUTDFR
BUDFSFERSTR. 2
8113 BOSDFSFSLSEN
:53A:         UBSWCHZH80A
:57D:         SFS SDFSDFS
RASDASDA
BRANCH 0
GOGOGOG
:59:          /43434
DFSDFL GRSDFOUP
SDFDF + DF LTFDD
DFD HDFDN 343423
GOGOER
:71A:         OER

Open in new window

0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<that i would like to split into many new fields,and the delimiter should me ->  :numeronly OR numer&Char:>>

  So you can't count on the colon always being there as the start of a new section or was that a typo?   Did you mean to type this:

and the delimiter should me ->  :numeronly: OR :numer&Char:

If you will always have a leading/trailing colon, then use Split().

Token one then will be the code, token two the value for it (you'll have two entries in the array for each element; 1 & 2 first code and value, 3 & 4 second code and value, 5 & 6 third code and value, etc).

Jim.
0
 
Rey Obrero (Capricorn1)Commented:
i don't understand the result you want to get..

what is the problem with my query ?
0
 
Rey Obrero (Capricorn1)Commented:
try this query


select f1, IIf(InStr([f1],":")>0,Left([f1],InStrRev([f1],":")),"") as Result1, IIf(InStr([f1],":")>0,Mid([f1],InStrRev([f1],":")+1),[f1]) as Result2
from yourTable;
0
 
Nick67Commented:
Like this?
Sample attached
Ugly.mdb
0
 
drtopservAuthor Commented:
Nick67 YOU ALmost reach the point, still need small fix :
IN :

ResultID      UglyID      DelimiterValue      UglyText
5      1      :50K:      /CH110023412400243526

in table tblResults , should contain :
UglyText
/CH110023412400243526
 MSFSDFER KFSFUTDFR
 BUDFSFERSTR. 2
 8113 BOSDFSFSLSEN

and not ONLY : /CH110023412400243526
means should contain the string until the next delimiter " :53A:"
0
 
aikimarkCommented:
@drtopserv

Have you tested or have any questions about my solution?
0
 
Rey Obrero (Capricorn1)Commented:
did you try my query at

http:#a40527946
0
 
Nick67Commented:
Easily enough fixed.
Ugly text just has to be held and concatenated until the next colon is encountered and THEN written to the table.  You need me to fix that, or do you have it cased?
0
 
Nick67Connect With a Mentor Commented:
So like this!
Altered sample attached
Ugly-v1.mdb
0
 
drtopservAuthor Commented:
You all are great helpers
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.