Solved

Easy Question

Posted on 2003-11-12
8
228 Views
Last Modified: 2010-05-01
I'm tired and my brain just can't seem to figure this out.. what i want it to do is:

you have a list of words loaded in a list box:

word1
word2
word3
word4
ect...

what i want it to do is add
w_o_r_d_1
w__o__r__d__1
w___o___r___d___1

ect.. i want the length to go no higher then 32, and then likewise for the rest of the words in the list...

i can't seem to figure out how to get it to stop at a length of no higher then 32 and i'm sick of trying it... help?

Thanks
0
Comment
Question by:learning_t0_pr0gram
8 Comments
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9737433
i increased points cuz i need this by tonight
0
 
LVL 6

Expert Comment

by:prasitlee
ID: 9737476
Dear learning_t0_pr0gram,
     Just use the function len() to check if your word lenght is greater than 32 or not.
     And use the function mid() to get the 33rd Character to the last character in the word.
     I would give you the sample code as following.
     
     strWord = "w___o___r___d___1"
     If len(strWord) > 32 then  
          strRemainingChars = mid(33,strWord)
     end if
   
    If you still need any help, please let me know.
                                                                                Meng
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9737500
ya but if i got 130 words in the list, that would be a lot to type, isn't there a shorter way of doing it?

here is the code i use to make it w_o_r_d_1

For w = 0 To Len(Text4.Text) - 2
Text4.Text = Left(Text4.Text, v) & "_" & Mid(Text4.Text, v + 1, Len(Text4.Text))
v = v + 2
Next

and to tell it to do it for each item

for x = 0 to list1.listcount - 1

i just can't get it to stop at 32 characters...
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Accepted Solution

by:
monvelasquez earned 60 total points
ID: 9737513
I have a piece of code here... i'm not sure what you want to do with the generated
words but in here i added them to the list...

--[CODE]--------------------------------------------------------------------------------------

Const MAX_LEN = 32

Dim i, j As Integer               ' just some looping variables
Dim intWordCount As Integer       ' total number of initial words
Dim strCurrentWord As String      ' current word in process
Dim int_ As Integer               ' number of underscores to insert
Dim strNewWord As String          ' new word to insert
Dim blnMaxLenBridged As Boolean   ' flag checking if the max lenght was reached

  ' Get the initial list of words
  intWordCount = List1.ListCount
 
  ' loop through the words
  For i = 0 To intWordCount - 1
 
    ' get the current word
    strCurrentWord = List1.List(i)
   
    int_ = 1   ' number of underscores to insert
    blnMaxLenBridged = Len(strCurrentWord) >= MAX_LEN  ' check if the length > 32
   
    'check for maximum length
    Do Until blnMaxLenBridged
     
      ' start inserting underscores
      strNewWord = Left(strCurrentWord, 1)      ' start the new word with the first character
      For j = 2 To Len(strCurrentWord)
        strNewWord = strNewWord & String(int_, "_") & Mid(strCurrentWord, j, 1)
      Next j

      ' check the length
      If Len(strNewWord) >= MAX_LEN Then
       
        ' tell that the max length was reached
        blnMaxLenBridged = True
     
      Else
       
        ' add the new word to the list
        List1.AddItem strNewWord
        'increase the number of undercores
        int_ = int_ + 1
     
      End If 'Len(strNewWord) >= MAX_LEN
     
    Loop 'Do Until blnMaxLenBridged
 
  Next i

--[/CODE]-------------------------------------------------------------------------------------

_____________
Raymond:\>_
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9737545
it works, thanks... a little different style then what i use, but i understand it, thanks man...
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9737604

You have to calculate Expected word length before you add lines to it.
Formula for doing this is:

Spaces = X                                                       'Number of underscores that have to be put between each two chars
CharsNumber = Len(Text)                                  'Number of characters in a string
Betweens = CharsNumber - 1                             'Number of "betreens" - (places between characters where you put underscores

ExpectedLength = Charsnumber + Betweens * Spaces

Try this example:  (It does not use your algorythm for adding underscores)

-------------------------------------------------------------------
Private Sub Command1_Click()
    Dim temp As String, temp2 As String
    Dim spaces As Integer
    Dim ExpectedLength As Integer
   
    For X = 0 To List1.ListCount - 1
        spaces = X + 1
        temp = List1.List(X)
        temp2 = ""
       
        ExpectedLength = Len(temp) + (Len(temp) - 1) * spaces
        If ExpectedLength > 32 Then Exit Sub
        For Y = 1 To Len(temp)
           
            Select Case spaces
            Case 1
                temp2 = temp2 & Mid(temp, Y, 1) & "_"
            Case 2
                temp2 = temp2 & Mid(temp, Y, 1) & "__"
            Case 3
                temp2 = temp2 & Mid(temp, Y, 1) & "___"
            Case 4
                temp2 = temp2 & Mid(temp, Y, 1) & "____"
            Case 5
                temp2 = temp2 & Mid(temp, Y, 1) & "_____"
            Case 6
                temp2 = temp2 & Mid(temp, Y, 1) & "______"
            Case 7
                temp2 = temp2 & Mid(temp, Y, 1) & "_______"
            End Select
        Next Y
        temp2 = Mid(temp2, 1, Len(temp2) - spaces)
        List2.AddItem temp2
    Next X

End Sub
-----------------------------------------------------------------------
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9737615
SORRY!

My example doesnt do what you asked! I'll rewrite it.
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9737689
This is it!

Add two listboxes, and a button, and paste this:

-----------------------------------------------------------------------------------------------------------
Private Sub Command1_Click()
    Dim temp As String, temp2 As String
    Dim spaces As Integer
    Dim ExpectedLength As Integer
    Dim SpaceString As String
   
    SpaceString = "_____________________________________________________"       'Used to extract the right number of backscores
   
   
    For X = 0 To List1.ListCount - 1                                            'Get every word from list1
        temp = List1.List(X)

        spaces = 1                                                              'Theese 3 lines are used to prevent
        ExpectedLength = Len(temp) + (Len(temp) - 1) * spaces                   'proccessing a word which would have more than
        If ExpectedLength > 32 Then GoTo skip                                   '32 chars after first pass
             
        Do
            temp2 = ""
            For Y = 1 To Len(temp)                                               'Add spaces after each character
                temp2 = temp2 & Mid(temp, Y, 1) & Mid(SpaceString, 1, spaces)
            Next Y
                       
            temp2 = Mid(temp2, 1, Len(temp2) - spaces)                           'Remove underscores after the last char
            List2.AddItem temp2
           
            spaces = spaces + 1
       
        ExpectedLength = Len(temp) + (Len(temp) - 1) * spaces                    'Calculate expected length before next pass
        If ExpectedLength > 32 Then GoTo skip
       
        Loop While ExpectedLength < 32
skip:
    Next X

End Sub
----------------------------------------------------------------------------------
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

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…
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 …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

766 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