1. Senior Member
Join Date
Dec 1969
Posts
189

## can someone please explain this?

I have this function:<BR><BR>Function TrimDescription(text)<BR> If InStr(text, " ") &#062; 0 Then<BR> If Len(text) &#062; 150 Then<BR> text = Left(text, 150)<BR> pos = 150<BR> Do Until Mid(text, pos, 1) = " "<BR> pos = pos - 1<BR> Loop<BR> TrimDescription = Left(text, pos - 1)<BR> End If<BR> TrimDescription = text<BR> ElseIf Len(text) &#062; 150 Then<BR> TrimDescription = Left(text, 150)<BR> Else<BR> TrimDescription = text<BR> End If<BR>End Function<BR><BR>And while this works......<BR> &#060;%test = rs_getProducts("description")%&#062;<BR> &#060;%=TrimDescription(test)%&#062;<BR><BR>Thi s does not.......<BR>&#060;%=TrimDescription(rs_getProduc ts("description"))%&#062;<BR><BR>I don&#039;t understand why. The second scenario just returns an empty string. Any insight would be greatly appreciated. Thanks.

2. Senior Member
Join Date
Dec 1969
Posts
5,104

## Not a solution.. but, easier to read:

Not a solution... just "tabbed" (with spaces) your function:<BR><BR>Function TrimDescription(text) <BR>If InStr(text, " ") &#062; 0 Then <BR> If Len(text) &#062; 150 Then <BR> text = Left(text, 150) <BR> pos = 150 <BR> Do Until Mid(text, pos, 1) = " " <BR> pos = pos - 1 <BR> Loop <BR> TrimDescription = Left(text, pos - 1) <BR> End If<BR><BR> TrimDescription = text<BR><BR>ElseIf Len(text) &#062; 150 Then<BR> TrimDescription = Left(text, 150)<BR>Else<BR> TrimDescription = text<BR>End If<BR>End Function

3. Senior Member
Join Date
Dec 1969
Posts
1,285

## RE: Not a solution.. but, easier to read:

just by quickly looking over it, it looks like they just want to get a piece of a string delimited by a space<BR><BR>if that&#039;s the case, they probably should have used Split()

4. Senior Member
Join Date
Dec 1969
Posts
196

## RE: can someone please explain this?

I couldn&#039;t quite understand your function, to me it looks like this would do the same thing:<BR><BR>Function TrimDescription(oldtext)<BR> text = trim(oldtext)<BR> If Len(text) &#062; 150 Then text = Left(text,150)<BR> TrimDescription = text<BR>End Function<BR><BR><BR><BR>- jørn

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## An explanation and a simplification

rs_getProducts("description") is a what?<BR><BR>You think it&#039;s a string, right?<BR><BR>Nope. It&#039;s an *OBJECT*. It&#039;s an instance of ADODB.Field, to be precise.<BR><BR>Now, when you assign that object to an ordinary (non-reference) variable, the Automation defaults take over and what *actually* happens is:<BR><BR>test = rs_getProducts.Fields.Item("description").value<BR ><BR>(of which the important part is the .value).<BR><BR>But when you pass the object reference as an argument to a function...well, it *stays* an object reference!<BR><BR>So you could do:<BR><BR>&#060;%=TrimDescription( CSTR(rs_getProducts("description")) )%&#062;<BR>**or**<BR>&#060;%=TrimDescription( rs_getProducts("description").value )%&#062;<BR><BR>and I bet it would work.<BR><BR>************************************* *********<BR><BR>Having said that, let me simplify the hell out of your code:<BR><BR>&#060;%<BR>Function TrimDescription( text )<BR>&nbsp; &nbsp; If Len(text) &#062; 150 Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; spaceAt = InStrRev( text, " ", 150 )<BR>&nbsp; &nbsp; &nbsp; &nbsp; If spaceAt &#060; 130 Then spaceAt = 150<BR>&nbsp; &nbsp; &nbsp; &nbsp; text = Left( text, spaceAt )<BR>&nbsp; &nbsp; End If<BR>&nbsp; &nbsp; TrimDescription = text<BR>End Function<BR>%&#062;<BR><BR>The number 130 in there is arbitrary. We start looking for a space at character number 150, searching *backwards* (InStrRev -- look it up!). If we don&#039;t find a space after character 130, then we just chop the string at 150. Kachunk. Otherwise, we chop the string at the last space before character 150.<BR><BR>Hokay?<BR><BR><BR><BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
196

## oops...

Misread your code abit, so my replacement wouldn&#039;t work the way you wanted. Think I&#039;ll head off to bed in shame now :)<BR><BR>Look at Bill&#039;s answer instead, much better.<BR><BR><BR><BR>- jørn

7. Senior Member
Join Date
Dec 1969
Posts
189

## cool - thanks!

That&#039;s interesting - thanks for the tip. Sometimes I wish VBscript didn&#039;t have so many defaults for collections.<BR><BR>My function may look a little silly (especially after seeing yours!) - I was trying to trim a long text string, but I didn&#039;t want to cut it off in the middle of a word, so that&#039;s what I&#039;m trying to do when I look for a " " character. Your function looks a lot better than mine - I hope you don&#039;t mind if I use it!<BR><BR>Thanks a lot

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## You will get your wish...

&#062; Sometimes I wish VBscript didn&#039;t have so many defaults for collections.<BR><BR>Cheer up. No defaults at all in VB.NET. For anything.<BR><BR>You&#039;ll get to write it all out, fully. (Of course, VS.NET makes that pretty easy to do, but still...)<BR><BR>****************<BR><BR>The bug in your code is the same I would have had if I hadn&#039;t put in that arbitrary 130 value: If the text has a space in the 2nd character and then no spaces until character 162 or so, you&#039;ll end up chopping the text to 1 character.<BR><BR>I kind of arbitrarily decided that if I don&#039;t find a space in the last 20 characters then there is something funny about the data, so I won&#039;t chop it any shorter than the given max.<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
189

## you da man!

!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•