To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Mar 13th, 2005, 01:41 AM   #1
vivian2u
Lively Member
 
Join Date: Feb 05
Posts: 81
vivian2u is an unknown quantity at this point (<10)
Unhappy Split text with length

Hi,
I have a textbox with multiline for the address such as:
18-G, Kuala Kangsar Road, 65890 Ipoh, Perak

I split it with "," so that each line can be saved to the add1, add2, add3, add4, add5, etc...respectively.
The result is:
18-G
Kuala Kangsar Road
65890 Ipoh
Perak

But I would like to split it with the fixed length, like:
18-G, Kuala Kangsar Road,
65890 Ipoh,
Perak

Please help in the VB6 coding. Hope my question is clear to you, thanks in advance.
Viv
vivian2u is offline   Reply With Quote
Old Mar 13th, 2005, 01:45 AM   #2
penagate
Super Moderator
 
Join Date: Jan 05
Location: Sunny Adelaide
Posts: 12,532
penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)
Re: Split text with length

No it isn't really

Can you show us exactly what your input is and what you want the output to be?
penagate is offline   Reply With Quote
Old Mar 13th, 2005, 02:03 AM   #3
vivian2u
Lively Member
 
Join Date: Feb 05
Posts: 81
vivian2u is an unknown quantity at this point (<10)
Re: Split text with length

it is a texbox with multilines setting for user to key in the address.
I would like to split the address in the format like:
18-G, Kuala Kangsar Road,
65890 Ipoh,
Perak

and saved each line of the address to different field such as: add1, add2 and add3....

thanks
vivian2u is offline   Reply With Quote
Old Mar 13th, 2005, 02:06 AM   #4
penagate
Super Moderator
 
Join Date: Jan 05
Location: Sunny Adelaide
Posts: 12,532
penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)
Re: Split text with length

So each line goes into a different variable.

What result do you want?
penagate is offline   Reply With Quote
Old Mar 13th, 2005, 02:21 AM   #5
vivian2u
Lively Member
 
Join Date: Feb 05
Posts: 81
vivian2u is an unknown quantity at this point (<10)
Re: Split text with length

i want to save each line to the different variable in the database.

so that it can be used for the report. I would like to display the address in seperated line and not a whole one line.
vivian2u is offline   Reply With Quote
Old Mar 13th, 2005, 02:38 AM   #6
penagate
Super Moderator
 
Join Date: Jan 05
Location: Sunny Adelaide
Posts: 12,532
penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)penagate has much to be proud of (1500+)
Re: Split text with length

You want to access the database from VB? In that case, I can't help you, sorry.
Which bit of the coding do you need help with?
penagate is offline   Reply With Quote
Old Mar 13th, 2005, 05:46 AM   #7
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
Re: Split text with length

Your description of the problem is a little vague, but the following code should get you started.

You need to change "yourdatabase.mdb" to the name of your database. Inside the database, create a table with 6 fields - idnum as an autonumbered primary key and 5 fields named add1, add2, add3, add4 and add5. You need to create a form with a textbox called txtAddress and a commandbutton called cmdSave, then add a reference to the Microsoft ADO 2.x Library.

VB Code:
  1. Option Explicit
  2. Dim objConn As ADODB.Connection
  3. Private Sub cmdSave_Click()
  4.     Dim strAddressLines() As String
  5.     Dim intPointer As Integer
  6.     Dim objRS As ADODB.Recordset
  7.     If txtAddress.Text <> "" Then
  8.         strAddressLines = Split(txtAddress.Text, ",")
  9.        
  10.         Call ConnectToDatabase(App.Path & "\yourdatabase.mdb")
  11.        
  12.         Set objRS = New ADODB.Recordset
  13.         objRS.Open "Addresses", objConn, adOpenStatic, _
  14.           adLockPessimistic, adCmdTable
  15.         objRS.AddNew
  16.        
  17.         For intPointer = 0 To UBound(strAddressLines)
  18.             If intPointer < 5 Then
  19.                 objRS.Fields("add" & CStr(intPointer + 1)) = strAddressLines(intPointer)
  20.             Else
  21.                 MsgBox "Too many address lines", vbOKOnly + vbExclamation, App.Title
  22.                 Exit For
  23.             End If
  24.         Next intPointer
  25.        
  26.         objRS.Update
  27.         If objRS.State <> adStateClosed Then
  28.             objRS.Close
  29.         End If
  30.         Set objRS = Nothing
  31.        
  32.         Call DisconnectFromDatabase
  33.     End If
  34. End Sub
  35. Private Sub ConnectToDatabase(ByVal strPath As String)
  36.     Set objConn = New ADODB.Connection
  37.    
  38.     objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  39.       "Data Source=" & strPath & ";" & _
  40.       "Mode=Share Deny None;"
  41.      
  42.     objConn.Open
  43. End Sub
  44. Private Sub DisconnectFromDatabase()
  45.     If objConn.State <> adStateClosed Then
  46.         objConn.Close
  47.     End If
  48.     Set objConn = Nothing
  49. End Sub

I would recommend looking at some tutorials if you have never worked with databases before through VB.
eg. http://www.developerkb.com/modules/w...hp?category=23
olamm2k is offline   Reply With Quote
Old Mar 13th, 2005, 08:34 AM   #8
vivian2u
Lively Member
 
Join Date: Feb 05
Posts: 81
vivian2u is an unknown quantity at this point (<10)
Re: Split text with length

Thank you for the coding.

I would like to split the text at "," , For example: 265, Kuala Kangsar Road, 65000 Ipoh. The result will be:

265
Kuala Kangsar Road
65000 Ipoh

I would like to split the text at "," but with the fixed length (30). The result
I would like to have is:

265, Kuala Kangsar Road
65000 Ipoh

Please help
vivian2u is offline   Reply With Quote
Old Mar 13th, 2005, 08:43 AM   #9
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
Re: Split text with length

That's more difficult to do - why not have separate text boxes for each line of the address? You'd find it a lot easier to code.
olamm2k is offline   Reply With Quote
Old Mar 13th, 2005, 09:26 AM   #10
MartinLiss
Administrator
 
MartinLiss's Avatar
 
Join Date: Sep 99
Location: Looking over your shoulder from San Jose, CA
Posts: 29,638
MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)
Re: Split text with length

Is this what you want?

VB Code:
  1. Dim intPos As Integer
  2.     Dim strTest As String
  3.     Dim strAddress As String
  4.    
  5.     strAddress = "18-G, Kuala Kangsar Road, 65890 Ipoh, Perak"
  6.    
  7.     Do Until Len(strAddress) < 31
  8.         strTest = Left$(strAddress, 30)
  9.         intPos = InStrRev(strTest, ",")
  10.         Debug.Print Left$(strAddress, intPos)
  11.         strAddress = Right$(strAddress, Len(strAddress) - intPos)
  12.         ' get rid of the leading space
  13.         If Left$(strAddress, 1) = " " Then
  14.             strAddress = Right$(strAddress, Len(strAddress) - 1)
  15.         End If
  16.     Loop
  17.     ' Show the last part
  18.     Debug.Print strAddress
__________________
Tips, Examples & Tutorials:
A valuable forum toolGenerate unique TreeView keysTreeView with "open" and "closed folder" iconsTime code using GetTickCountHow to trap the Tab keyScroll a formNumberBox ActiveX controlColor a ListView rowAn InputBox formHow to use SaveSetting and GetSettingA program registration schemeSpellcheck a TextboxResize controlsOpen Windows Explorer at Last Visited PathA Blackjack GameCount lines of codePrivate Message ViewerCopy/Paste VB CodePaste VB Code Add-InInsert Procedure Names Add-InA calculator for the game of SpiderMy review of REALbasic 2008VB6 Debug TutorialPicture ViewerVBF Photo Contest Winners

Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
Marty
If someone helped you today then please consider rating their post.
MartinLiss is offline   Reply With Quote
Old Mar 13th, 2005, 09:38 AM   #11
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
Re: Split text with length

Just to point out, though - if a line is longer than 30 characters, it will go into an unending loop.
olamm2k is offline   Reply With Quote
Old Mar 13th, 2005, 10:18 AM   #12
MartinLiss
Administrator
 
MartinLiss's Avatar
 
Join Date: Sep 99
Location: Looking over your shoulder from San Jose, CA
Posts: 29,638
MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)
Re: Split text with length

OK, here is a fix.

VB Code:
  1. Dim intPos As Integer
  2.     Dim strTest As String
  3.     Dim strAddress As String
  4.    
  5.     strAddress = "18-G, Kuala Kangsar Road, 65890 Ipoh, Perak ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
  6.    
  7.     Do Until Len(strAddress) < 31
  8.         strTest = Left$(strAddress, 30)
  9.         intPos = InStrRev(strTest, ",")
  10.         If intPos = 0 Then
  11.             'no comma so look for a space instead
  12.             intPos = InStrRev(strTest, " ")
  13.         End If
  14.         If intPos = 0 Then
  15.             ' no space either so just artificially break it
  16.             intPos = 30
  17.         End If
  18.         Debug.Print Left$(strAddress, intPos)
  19.         strAddress = Right$(strAddress, Len(strAddress) - intPos)
  20.         ' get rid of the leading space
  21.         If Left$(strAddress, 1) = " " Then
  22.             strAddress = Right$(strAddress, Len(strAddress) - 1)
  23.         End If
  24.     Loop
  25.     ' Show the last part
  26.     Debug.Print strAddress
__________________
Tips, Examples & Tutorials:
A valuable forum toolGenerate unique TreeView keysTreeView with "open" and "closed folder" iconsTime code using GetTickCountHow to trap the Tab keyScroll a formNumberBox ActiveX controlColor a ListView rowAn InputBox formHow to use SaveSetting and GetSettingA program registration schemeSpellcheck a TextboxResize controlsOpen Windows Explorer at Last Visited PathA Blackjack GameCount lines of codePrivate Message ViewerCopy/Paste VB CodePaste VB Code Add-InInsert Procedure Names Add-InA calculator for the game of SpiderMy review of REALbasic 2008VB6 Debug TutorialPicture ViewerVBF Photo Contest Winners

Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
Marty
If someone helped you today then please consider rating their post.
MartinLiss is offline   Reply With Quote
Old Mar 13th, 2005, 10:51 AM   #13
szlamany
MS SQL Powerposter
 
szlamany's Avatar
 
Join Date: Mar 04
Location: CT
Posts: 12,269
szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)
Re: Split text with length

There should be no comma delimiter between a house (or lot#) and the street name.

Are the users in your locale used to entering: 123, MAIN STREET??

Is that comma needed?
__________________

*** Read the sticky in the DB forum about how to get your question answered quickly!! ***

Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

Some Informative Links:
[ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
[ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ VB.Net Database Class ] [ Loading Pictures from DB ]


MS MVP 2006, 2007, 2008
szlamany is offline   Reply With Quote
Old Mar 13th, 2005, 11:54 AM   #14
vivian2u
Lively Member
 
Join Date: Feb 05
Posts: 81
vivian2u is an unknown quantity at this point (<10)
Re: Split text with length

yes, the user used to put delimiter between a house (or lot#) and the street name.
vivian2u is offline   Reply With Quote
Old Mar 13th, 2005, 11:56 AM   #15
MartinLiss
Administrator
 
MartinLiss's Avatar
 
Join Date: Sep 99
Location: Looking over your shoulder from San Jose, CA
Posts: 29,638
MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)MartinLiss is a name known to all (1000+)
Re: Split text with length

Is my code anything like what you want?
__________________
Tips, Examples & Tutorials:
A valuable forum toolGenerate unique TreeView keysTreeView with "open" and "closed folder" iconsTime code using GetTickCountHow to trap the Tab keyScroll a formNumberBox ActiveX controlColor a ListView rowAn InputBox formHow to use SaveSetting and GetSettingA program registration schemeSpellcheck a TextboxResize controlsOpen Windows Explorer at Last Visited PathA Blackjack GameCount lines of codePrivate Message ViewerCopy/Paste VB CodePaste VB Code Add-InInsert Procedure Names Add-InA calculator for the game of SpiderMy review of REALbasic 2008VB6 Debug TutorialPicture ViewerVBF Photo Contest Winners

Please go to the Thread Tools menu and click Mark Thread Resolved when you have your answer.
Marty
If someone helped you today then please consider rating their post.
MartinLiss is offline   Reply With Quote
Old Mar 13th, 2005, 12:06 PM   #16
vivian2u
Lively Member
 
Join Date: Feb 05
Posts: 81
vivian2u is an unknown quantity at this point (<10)
Re: Split text with length

Thank you. I need to try it out first. Heres are my coding, since i dont know how to fixed the length, i make it in other way. It might not in the good standard but it works. I will try yours too.

Dim strLineArr() As String
Dim strWordArr() As String
Dim intLineIdx As Integer, intWordIdx As Integer

strLineArr() = Split(txtAddress.Text, vbCrLf)

For intLineIdx = 0 To UBound(strLineArr)
strWordArr() = Split(strLineArr(intLineIdx), ",")
If Len(strWordArr(0)) < 5 Then
rs_add(1) = strWordArr(0) & "," & "" & strWordArr(1) & ","
For intWordIdx = 2 To UBound(strWordArr)
rs_add(intWordIdx) = strWordArr(intWordIdx) & ","
Next
Else
For intWordIdx = 0 To UBound(strWordArr)
rs_add(intWordIdx) = strWordArr(intWordIdx) & ","
Next
End If
Next
vivian2u is offline   Reply With Quote
Old Mar 13th, 2005, 12:08 PM   #17
szlamany
MS SQL Powerposter
 
szlamany's Avatar
 
Join Date: Mar 04
Location: CT
Posts: 12,269
szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)szlamany is a name known to all (1000+)
Re: Split text with length

Quote:
Originally Posted by vivian2u
yes, the user used to put delimiter between a house (or lot#) and the street name.
Since the comma is required between the house/lot# and street name then you cannot use SPLIT - that's obvious.

Do you always have a house/lot# in your data entry - or can there be a street name by itself?
__________________

*** Read the sticky in the DB forum about how to get your question answered quickly!! ***

Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

Some Informative Links:
[ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
[ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ VB.Net Database Class ] [ Loading Pictures from DB ]


MS MVP 2006, 2007, 2008
szlamany is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:53 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.