Solved

Cutting a string onto multiple lines.

Posted on 2004-03-23
13
657 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
  • 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 60 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
 
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 80 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now