Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cutting a string onto multiple lines.

Posted on 2004-03-23
13
Medium Priority
?
672 Views
Last Modified: 2013-12-25
I'm trying to cut a string and have it on multiple lines.
I want to do this in a regular multiline textbox by performing a carriage return on every 50th character.
HOWEVER, I don't want to cut the string in a middle of a word.  So it would instead cut on the word
before.

I have this 90% working... it works for some words, but not all.

   Do While strText <> ""
     strSnip = Left(strText, 50)
     If Len(strText) > 50 Then
        If Mid(strText, 51, 1) <> " " Then
           'intJ = InStrRev(strText, " ", 51)
           If intJ > 0 Then
              strSnip = Left(strText, intJ - 1)
              strText = Mid(strText, intJ + 1)
           Else
              strText = Mid(strText, 51)
           End If
        Else
           strText = Mid(strText, 51)
        End If
     Else
        strText = ""
     End If
     strDone = strDone & strSnip & vbCrLf
  Loop
  Format50 = strDone
0
Comment
Question by:NO_CARRIER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 10663938
Hi NO_CARRIER,
To save my time, can you please post a sentence where it does not work?

Dabas
0
 
LVL 2

Assisted Solution

by:MePhantom
MePhantom earned 240 total points
ID: 10664164
Hi NO_CARRIER,
 
Other than the fact that you have an important line commented out the code seems to work fine.
Also If you don't want the space indenting next line change 51 to 52.

   Do While strtext <> ""
     strSnip = Left(strtext, 50)
     If Len(strtext) > 50 Then
        If Mid(strtext, 51, 1) <> " " Then
           intJ = InStrRev(strtext, " ", 51) '************ Un comment this line
           If intJ > 0 Then
              strSnip = Left(strtext, intJ - 1)
              strtext = Mid(strtext, intJ + 1)
           Else
              strtext = Mid(strtext, 51)
           End If
        Else
           strtext = Mid(strtext, 52) 'If you don't want the space indenting next line
        End If
     Else
        strtext = ""
     End If
     strDone = strDone & strSnip & vbCrLf
  Loop


Cheers!
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10668950
hmm... that's what I thought too.  I remember I had it working last year... but someone fuddled around in my code and not it's messed up.

on compile it gives an error on the commented line... "Expected array"...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10670384
I declare the array with
Dim InStrRev() as String

... but on compile it gives a Time mismatch on this line:
  intJ = InStrRev(strtext, " ", 51) '************ Un comment this line


doubled points to 140.
0
 
LVL 27

Accepted Solution

by:
Dabas earned 320 total points
ID: 10670636
NO_CARRIER:
You should not declare InStrRev
It is a function that comes with .NET

InStrRev Function
See Also
InStr Function | Strings | String Manipulation
Requirements
Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)
Returns the position of the first occurrence of one string within another, starting from the right side of the string.

Public Function InStrRev(
   ByVal StringCheck As String,
   ByVal StringMatch As String,
   Optional ByVal Start As Integer = -1,
   Optional ByVal Compare As CompareMethod = CompareMethod.Binary
) As Integer
Parameters
StringCheck
Required. String expression being searched.
StringMatch
Required. String expression being searched for.
Start
Optional. Numeric expression that sets the one-based starting position for each search, starting from the left side of the string. If Start is omitted, –1 is used, which means that the search begins at the last character position. Search then proceeds from right to left.
Compare
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed. See Settings for values.


Dabas
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10671089
I'm using VB6 ...
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10671099
If I do not declare I get the error, "Expected Array" highlighting InStrRev...
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10671134
This is from VB6 help

InStrRev Function
     

Description

Returns the position of an occurrence of one string within another, from the end of string.

Syntax

InstrRev(stringcheck, stringmatch[, start[, compare]])

The InstrRev function syntax has these named arguments:

Part Description
stringcheck Required. String expression being searched.
stringmatch Required. String expression being searched for.
start Optional. Numeric expression that sets the starting position for each search. If omitted, –1 is used, which means that the search begins at the last character position. If start contains
Null, an error occurs.
 
compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed. See Settings section for values
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10671151
Maybe your VBA is not up to date.

Press F2 to go into your object browser
Search for InStrRev

If it does not appear then your VBA is out of date.

This function was supplied with VB6 and did not exist in VB5

Dabas
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10671160
My msvbvm60.dll is dated 28 Aug 2002 and is version 6.0.92.37
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10671407
Hmm... I did F2 (really cool feature by the way, never seen this screen before)... and it does have InStrRev in it.  I'm using VB6 Retail... (not VBA)...

but it should still work, no?

Ah-ha!... I found it.  You got me thinking... I did a project-wide search for InStrRev .. and it's declared globally in a module.  I commented that line and it works again!

Thanks Dabas!
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10671499
<BIG SMILE>
Glad to be of help.
I would not be able to live without F2.
Indirectly you ARE using VBA. If you go Project->References, you will see that one of the references is to "Visual Basic for Applications"
Also in the F2 screen, you will see that when this function is found, it is shown as belonging to the VBA library.

Dabas
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10671556
Actually, I did notice that it was part of the VBA library.  That's interesting... I always thought VBA was just a stripped down version of VB.. but independant of.  This is definately more efficient.. the more I work with VB the more I like it.  (unfortunately I'm still developing a taste for Java...) :/ ... in either case.  Thanks again for helping.  It's tough debugging when it's not your code --- or in my case, when someone else has been fiddling around in my code.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question