473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Spliting one string into multiple and inserting into multiple colunms

I want to know the macro or query to split the string in one single colunm and insert those into multiple colunms(that may be a sentence)
like for example

colunm1 contains:

1234 daily !@#$(these words are separated by space)

these things must be splited in to
1234,daily,!@#$ and inserted into three different colunms
colunm2 colunm3 colunm4
1234 daily !@#$

these colunms are in same table.
Dec 17 '07 #1
12 4521
Minion
108 Expert 100+
One way to do this is to use a parsing function like the one below. This function is passed a string and a list of delimeters (such as " " for space). It will then go through the string and split the string into an array. Once you have the array use a for...next loop to populate the remaining fields.

Here is the code of the parsing function. It shouldn't need much if any editing to get working:

Expand|Select|Wrap|Line Numbers
  1. Public Function parse(ByVal inString, Optional ByVal delimiters)
  2. 'Take a string, and return it as a one dimensional array
  3. ' of individual values as delimited by any of several
  4. ' characters. None of those characters are returned in
  5. ' the result. Provide a default list of delimiters, which
  6. ' should come from registry. But allow override.
  7.  
  8.     Dim delimitList, oneChar, aWord, codeCount
  9.     Dim arrayCodes()
  10.  
  11.     If IsMissing(delimiters) Then
  12.         'We should get these from Registry
  13.         delimitList = " ,/!|"
  14. 'Characters recognized as delimiters
  15.  
  16.     Else
  17.         delimitList = delimiters
  18. 'user can override if needed
  19.  
  20.     End If
  21.     Dim i, j, k
  22.     i = Len(inString)
  23.     For j = 1 To i
  24. 'Read one character at a time
  25.  
  26.         oneChar = VBA.Strings.Mid(inString, j, 1)
  27.         k = InStr(delimitList, oneChar)
  28. 'Is this one a delimiter?
  29.         If k = 0 Then
  30.             aWord = aWord & oneChar
  31. 'If is isn't, add to the current word
  32.         End If
  33.         If k <> 0 Or j = i Then
  34. 'If it is, or if we're finished
  35.             If aWord > "" Then
  36.                 codeCount = codeCount + 1
  37.                 ReDim Preserve arrayCodes(codeCount)
  38.                 arrayCodes(codeCount) = aWord
  39. 'Save new word
  40.                 aWord = ""
  41.             End If
  42.         End If
  43.     Next j
  44.     parse = arrayCodes
  45. 'Return the array
  46. End Function
  47.  
Hope this helps

- Minion -
Dec 17 '07 #2
hi minion
thankyou for your response
But i am not understanding this please give a detailed explination
on this ,actually i do not know anything about macro please help me
Dec 24 '07 #3
missinglinq
3,532 Expert 2GB
You really need to provide a little more information. Is the original string always going to be three words long? Are the words always going to be separated by a comma?

Welcome, both of you, to TheScripts!

Linq ;0)>
Dec 24 '07 #4
ADezii
8,834 Expert 8TB
I want to know the macro or query to split the string in one single colunm and insert those into multiple colunms(that may be a sentence)
like for example

colunm1 contains:

1234 daily !@#$(these words are separated by space)

these things must be splited in to
1234,daily,!@#$ and inserted into three different colunms
colunm2 colunm3 colunm4
1234 daily !@#$

these colunms are in same table.
Here is compact code that will work as long as you have only 3 values in [Column1] separated by spaces. It assumes your Table Name is tblTest, and your Field Names are [Column1], [Column2], [Column3], and [Column4] where [Column1] contains the multiple values. Any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  2. Dim varSplit As Variant, intCounter As Integer
  3.  
  4. Set MyDB = CurrentDb()
  5. Set MyRS = MyDB.OpenRecordset("tblTest", dbOpenDynaset)
  6.  
  7. If MyRS.RecordCount > 0 Then
  8.   Do While Not MyRS.EOF
  9.   varSplit = Split(MyRS![Column1], " ")
  10.     MyRS.Edit
  11.       For intCounter = 0 To UBound(varSplit)    'will be 2
  12.         MyRS![Column2] = varSplit(0)
  13.         MyRS![Column3] = varSplit(1)
  14.         MyRS![Column4] = varSplit(2)
  15.       Next
  16.     MyRS.Update
  17.     MyRS.MoveNext
  18.   Loop
  19. End If
  20.  
  21. MyRS.Close
  22. Set MyRS = Nothing
Dec 24 '07 #5
You really need to provide a little more information. Is the original string always going to be three words long? Are the words always going to be separated by a comma?

Welcome, both of you, to TheScripts!

Linq ;0)>
hi,
the string length will be differing,And the string is not separated by comma,It is separated by SPACE
Thank You
Dec 26 '07 #6
Here is compact code that will work as long as you have only 3 values in [Column1] separated by spaces. It assumes your Table Name is tblTest, and your Field Names are [Column1], [Column2], [Column3], and [Column4] where [Column1] contains the multiple values. Any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  2. Dim varSplit As Variant, intCounter As Integer
  3.  
  4. Set MyDB = CurrentDb()
  5. Set MyRS = MyDB.OpenRecordset("tblTest", dbOpenDynaset)
  6.  
  7. If MyRS.RecordCount > 0 Then
  8.   Do While Not MyRS.EOF
  9.   varSplit = Split(MyRS![Column1], " ")
  10.     MyRS.Edit
  11.       For intCounter = 0 To UBound(varSplit)    'will be 2
  12.         MyRS![Column2] = varSplit(0)
  13.         MyRS![Column3] = varSplit(1)
  14.         MyRS![Column4] = varSplit(2)
  15.       Next
  16.     MyRS.Update
  17.     MyRS.MoveNext
  18.   Loop
  19. End If
  20.  
  21. MyRS.Close
  22. Set MyRS = Nothing
Hi
thankyou for your reply
In the Colunm1 string is not constant and It varies alway
thankyou
Dec 26 '07 #7
Here is compact code that will work as long as you have only 3 values in [Column1] separated by spaces. It assumes your Table Name is tblTest, and your Field Names are [Column1], [Column2], [Column3], and [Column4] where [Column1] contains the multiple values. Any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  2. Dim varSplit As Variant, intCounter As Integer
  3.  
  4. Set MyDB = CurrentDb()
  5. Set MyRS = MyDB.OpenRecordset("tblTest", dbOpenDynaset)
  6.  
  7. If MyRS.RecordCount > 0 Then
  8.   Do While Not MyRS.EOF
  9.   varSplit = Split(MyRS![Column1], " ")
  10.     MyRS.Edit
  11.       For intCounter = 0 To UBound(varSplit)    'will be 2
  12.         MyRS![Column2] = varSplit(0)
  13.         MyRS![Column3] = varSplit(1)
  14.         MyRS![Column4] = varSplit(2)
  15.       Next
  16.     MyRS.Update
  17.     MyRS.MoveNext
  18.   Loop
  19. End If
  20.  
  21. MyRS.Close
  22. Set MyRS = Nothing

Hi,
Can you give this same thing to use in excel
And the other thing is my string in the column1 will be varying
it may be
case1:hi john hw r u
case2:wt r u doning
case3 can we meet today or not
case4:when can we meet so
like the above the strings in my column1 will be changing,
if you can give me solution for this please give me in access and excel. OR
please give the previous solution to use in EXCEL
thankyou
Dec 26 '07 #8
ADezii
8,834 Expert 8TB
Hi,
Can you give this same thing to use in excel
And the other thing is my string in the column1 will be varying
it may be
case1:hi john hw r u
case2:wt r u doning
case3 can we meet today or not
case4:when can we meet so
like the above the strings in my column1 will be changing,
if you can give me solution for this please give me in access and excel. OR
please give the previous solution to use in EXCEL
thankyou
  1. I'm asuming that your Fields are already defined in your Table namely: Column1, Column2, Column3, Column4, Column5, etc. Is this the case, and if so how many pre-defined Columns exist in the Table?
  2. You previously stated that the values in Column1 could also be delimited by a Comma in addition to a space, does this still hold?
  3. Would the Delimiter be consistent for all the Records in a Table, or could you possibly have the scenario listed below?
    Expand|Select|Wrap|Line Numbers
    1. Record 1    Value1, Value2, Value3, etc.       'Comma Delimiter
    2. Record 2    Value1 Value2 Value3, etc          'Space Delimiter
Dec 26 '07 #9
missinglinq
3,532 Expert 2GB
Two things:

First, we've all been concentrating on parsing your string and noone's actually asked: Exactly what are you trying to do here? As ADezii asked, how many columns are you going to have to cover ever possibilty in Column1?

Secondly, you have cost a number of experts here a great deal of time! You initially stated your problem, showing an example string with three parts, separated by commas. When asked if this is the format the string will always be in, you responded a day and a half later, saying that no, the string will vary in length and no, despite your example being separated by commas, they will actually be separated by spaces! Then you come back again, demanding an answer to the problem using Excel!

Please take the time to read the Posting Guidelines paying special attention to the section titled “Give as much detail as possible!

Linq ;0)>
Dec 26 '07 #10
ADezii
8,834 Expert 8TB
Two things:

First, we've all been concentrating on parsing your string and noone's actually asked: Exactly what are you trying to do here? As ADezii asked, how many columns are you going to have to cover ever possibilty in Column1?

Secondly, you have cost a number of experts here a great deal of time! You initially stated your problem, showing an example string with three parts, separated by commas. When asked if this is the format the string will always be in, you responded a day and a half later, saying that no, the string will vary in length and no, despite your example being separated by commas, they will actually be separated by spaces! Then you come back again, demanding an answer to the problem using Excel!

Please take the time to read the Posting Guidelines paying special attention to the section titled “Give as much detail as possible!

Linq ;0)>
Nicely said, missinglinq.
Dec 26 '07 #11
Two things:

First, we've all been concentrating on parsing your string and noone's actually asked: Exactly what are you trying to do here? As ADezii asked, how many columns are you going to have to cover ever possibilty in Column1?

Secondly, you have cost a number of experts here a great deal of time! You initially stated your problem, showing an example string with three parts, separated by commas. When asked if this is the format the string will always be in, you responded a day and a half later, saying that no, the string will vary in length and no, despite your example being separated by commas, they will actually be separated by spaces! Then you come back again, demanding an answer to the problem using Excel!

Please take the time to read the Posting Guidelines paying special attention to the section titled “Give as much detail as possible!

Linq ;0)>
Hi,
Actually,
1.I will get a string into my first column1
2.I don't no its length
3.I am having n-number of columns in my table or sheet
4.My delimiter is "space"
5.My database may be access or excel
So please suggest me the solution.
thankyou
Dec 27 '07 #12
Minion
108 Expert 100+
Hi,
Actually,
1.I will get a string into my first column1
2.I don't no its length
3.I am having n-number of columns in my table or sheet
4.My delimiter is "space"
5.My database may be access or excel
So please suggest me the solution.
thankyou
Thank you for clearifing what it is your looking for somewhat. However, the medium for your database will greatly determine what type of solution is produced. If we were to give you a suggestion for how to solve your problem in access it would differ from one given for access. You also mentioned that you have limited to no knowledge of VBA or Macros so any solution you are given will be a challenge for you to implement. These are just a couple of the challenges still facing your situation; however I'll keep working on a solution that you should be able to utilize as more information is made available. Namely we need to know exactly what you have, what you will need, and what program you will need it for as the code will differ quite a bit.

Keep us posted.

- Minion -
Dec 27 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Steven Bethard | last post by:
I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. ...
1
by: mike | last post by:
Hello all, any advice or links to pages about how to have multiple language asp pages? For example, you have a database driven website that you want customers around the world to use. How do...
9
by: Sreejith S S Nair | last post by:
hi there, I have a panel control which contain more than 10 label controls. these label controls are added dynamically by user. In this application user can slit a control into not less than...
0
by: Sreejith S S Nair | last post by:
hi, Setp one. i have one panel control in my form. I am spliting this panel control into two part say working region and holding region. This spliting is only logic spliting. That is i...
4
by: Diffident | last post by:
Hello All, I have a string of around 150 characters. string test = "asjhdkashdkjahsdjkhaskjdhaskjdhasjdhasd........."; //(upto 150 characters) I want to split the above string into multiple...
6
by: Diffident | last post by:
Dear Bruce, Can you please explain me how compiler would be able to optimize "+" for the below code? The reason why I did not want to use string concatenation was that I wanted it to be a...
9
by: dan | last post by:
within a loop i am building a sql insert statement to run against my (programatically created) mdb. it works but it seems unreasonably SLOW! Sorry, dont have the code here but the jist is very...
2
by: Mersilla | last post by:
Dear All, I am currently new to perl and is experiencing difficulties with splitting and array with multiple conditions. For example , I would like to spilt an element, which contains one or...
1
by: kalia | last post by:
I have a text file and i want to Split the file into mulitple files based off the city and then create new files with the city name. I am able to read the file and also chnaged the semi colon to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.