Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a variable

Status
Not open for further replies.

Rieekan

Programmer
Apr 2, 2001
737
US
First off, I'd just like to say that I'm not (usually) a VB or ASP programmer, but I was given this problem to try and solve.

The end-user of this system wants to be able to enter a variable length text message that will be stored into (multiple) text columns in an Access Database. I can accomplish this with Perl and PHP, but have yet to find something that will do a similar function in ASP. Below are the requirements this must fall into.

1) String will be a variable length
2) String needs to be split on the space prior to the 255th character
3) The resulting strings need to be placed into an array that can be read and parsed for database insertion in a looped manner.

As always, if you could comment any code, that would be appreciated!

- Rieekan
 
Here is an example function on how to do this. Rather than fixing the length of the "segments" into which you break the original string to 255 characters I made that a variable passed into the function with the string. Let me know if this helps or if you need clarification.

'==========================================================

Private Sub ParseStringToArray(strInputString As String, lngStringLength As Long)

' Declare variable(s)
Dim arrStrings() As String ' Variable length array
Dim lngArrayLength As Long ' Variable to hold the array size
Dim lngCounter As Long ' Variable for parsing array in For loop

' Initialize array length to 0 elements
lngArrayLength = 0

' Set the initial array size as empty
ReDim arrStrings(lngArrayLength)

' Loop throught the input string getting lngStringLength characters at a time
For lngCounter = 1 To Len(strInputString) Step lngStringLength
' Increment the array size to include 1 more element
' Be sure to use the "Preserve" keyword to keep the existing data
' when the array is resized
lngArrayLength = lngArrayLength + 1
ReDim Preserve arrStrings(lngArrayLength)

' Load the last array element with the next set of characters from the string
arrStrings(UBound(arrStrings)) = Mid(strInputString, lngCounter, lngStringLength)
Next

' Insert whatever code you want to use the information in the array here.
' To go through the entire array, just use a For...Next loop going from
' the array LBound to the array UBound.

End Sub
 
I'm just curious...if you're splitting up a message longer than 255 characters to which it sounds like you will do that quite a few times...why don't you just use a 'memo' field in the database to store it all together? -Ovatvvon :-Q
 
Hah, I knew that question would come up. I really have no idea, but I'm sure it has something to do with the original developer only setting up text files. I was just given the problem of finding a way to allow more than 255 characters to be entered.

I have asked that they change the DB Data Type itself, but so far, that has been frowned on.

The short of it is: I'm just the worker bee and make something work out of what I'm given.

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top