Splitting a string!

N

Niklas Östergren

When I´m importing a delimeter textfile into a table I get one field holding
following string:

"Niklas,Östergren,Man,2004-12-24,Erika,Svensson,Kvinna,2004-12-25,Johan,Sven
sson,Man;2004-12-31"

This is names, gender and DoB for familymembers. Sometimes this string is
empty sometimes it holds 2 peoples name etc. soemtimes 4,5 ... whatever.

The thing is that I don´t know how many family members this string holds.
The only thing I know is that it´s seperated with coma´s and that each
person have 4 values (FirstName,LastName,Gender,DoB).

I have got help with how to split this up from Ken Snell (MVP) yesterday and
this work´s just fine. But how do I know how many times I need to loop
through this string to split data for all family members?

Anyone have any idéa?

TIA!
// Niklas
 
A

Andi Mayer

When I´m importing a delimeter textfile into a table I get one field holding
following string:

"Niklas,Östergren,Man,2004-12-24,Erika,Svensson,Kvinna,2004-12-25,Johan,Sven
sson,Man;2004-12-31"

This is names, gender and DoB for familymembers. Sometimes this string is
empty sometimes it holds 2 peoples name etc. soemtimes 4,5 ... whatever.

The thing is that I don´t know how many family members this string holds.
The only thing I know is that it´s seperated with coma´s and that each
person have 4 values (FirstName,LastName,Gender,DoB).

I have got help with how to split this up from Ken Snell (MVP) yesterday and
this work´s just fine. But how do I know how many times I need to loop
through this string to split data for all family members?
I don#t know what Ken was writing, but I would do:

dim ary() as string
dim I as long
ary=split(theString,",")
for I=0 to ubound(ary)
'do hat you like with ary(I)
next I


If you expect an answer to a personal mail, add the word "manfred" to the first 10 lines in the message
MW
 
Å

Åsa Holmgren

Hej Niklas,

the following code show how to loop through the string. You need to use ADO
or something to get the data into the right table.

Private Sub Test()
Dim sRow As String
Dim sFirstName As String
Dim sLastName As String
Dim sGender As String
Dim sDate As String
Dim lIdx As Long
Dim vnt As Variant

' String taken from your field
sRow =
"Niklas,Östergren,Man,2004-12-24,Erika,Svensson,Kvinna,2004-12-25,Johan,Sven
sson,Man,2004-12-31"

' Split string into array
vnt = Split(sRow, ",")

' Check to see if the array has (x * 4) items,
' if not - show error message
If UBound(vnt) Mod 4 = 3 Then
' Loop trough the array
' Here is where you process the data...
For lIdx = 0 To UBound(vnt) Step 4
sFirstName = vnt(lIdx)
sLastName = vnt(lIdx + 1)
sGender = vnt(lIdx + 2)
sDate = vnt(lIdx + 3)
Next lIdx
Else
MsgBox "Invalid string"
End If
End Sub

PS Om du vill ställa frågor till svenska utvecklare på svenska kan du titta
in på www.pellesoft.se
Vi kanske ses där!
DS
 
N

Niklas Östergren

Yes, offcourse! Why didn´t I think of it. Using an array! :-(

Thank´s a lot Andi! Now I´ll be able to fix this so thank´s a lot for
helping out!

// Niklas

By the way this is what Ken handed over to me yesterday:
'===================================
Public Function SplitMyImportedData(strOriginalString As String, _
strDelimiter As String, intFieldNumber As Integer)

Dim varArray As Variant

varArray = Split(strOriginalString, strDelimiter)

SplitMyImportedData = Trim(varArray(intFieldNumber - 1))

End Function
'===============================================
 
N

Niklas Östergren

Thank´s a lot Åsa!

When I saw your reply I said to myself:
- O good why didn´t I thought of that. I do know the technic a little bit
anyway!

What´im talking about is "using array´s" the rest of if am I allready
familiar with.

Thank´s a lot for helping out!

// Niklas

P.S Inte visste jag att det fanns en svenbsk sida. Har tittat in en snabbis
men har inte fått klart för mig hur man blir medlem och om det kostar något?
DS
 
Å

Åsa Holmgren

Det är gratis!
/Åsa

Niklas Östergren said:
Thank´s a lot Åsa!

When I saw your reply I said to myself:
- O good why didn´t I thought of that. I do know the technic a little bit
anyway!

What´im talking about is "using array´s" the rest of if am I allready
familiar with.

Thank´s a lot for helping out!

// Niklas

P.S Inte visste jag att det fanns en svenbsk sida. Har tittat in en snabbis
men har inte fått klart för mig hur man blir medlem och om det kostar något?
DS


"Niklas,Östergren,Man,2004-12-24,Erika,Svensson,Kvinna,2004-12-25,Johan,Sven
"Niklas,Östergren,Man,2004-12-24,Erika,Svensson,Kvinna,2004-12-25,Johan,Sven
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top