Solved

String variable Incrimentation and ListBoxes in VBA

Posted on 2003-11-09
11
309 Views
Last Modified: 2007-12-19
Hi,

 I am having a hard time with VB for applications trying to copy all the text in a ListBox on a UserDefined form, and writting it to a file on a specified path ...

 The problem is when I write to the file, through a For To loop, that loops through the items in my ListBox, stores them in a global String, and writes that string to a file ... the out come of this is always one item being writen to the file, because the ListBox items inseted over write each others ...

 Please advice me on a way to increment string values in VBA, like in Java for example using ' String +=' so that I can have all the items in the ListBaox, assigned to my global String variable, using the For loop.

Or maybe you can help me get all of the text raws displayed in a ListBox, and assign them all at once to a String variable ...

Thank you  
0
Comment
Question by:RaedKhlaifat
11 Comments
 
LVL 5

Accepted Solution

by:
fantasy1001 earned 500 total points
ID: 9711827
You can use array instead:

dim strAry() as string

Then in your loop:
for i = 0 to listbox.itemcount - 1
   strary(i) = ...
next

~ fantasy ~
0
 
LVL 12

Expert Comment

by:jyokum
ID: 9711831
dim myString as String

myString = "blah"
myString = myString & " more text"
myString = myString & " foo"

debug.print myString

this will result in:   blah more text foo
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9711851
Dim i
Dim str

For i = 0 To List1.ListCount - 1
str = str & List1.List(i) & vbCrLf
Next
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9712025
Why not just print each item directly to a line in the file.

Dim ff As Integer, i As Integer
ff = FreeFile
Open "C:\Somefile.txt" For Output As #ff
For i = 0 To List1.ListCount - 1
Print #ff, List1.List(i)
Next i
Close #ff
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 8

Expert Comment

by:bukko
ID: 9714325

In case it wasn't clear, the VBA equivalent of the Javascript

    String1 += String2;

is

    String1 = String1 & String2

Regards

bukko

0
 
LVL 6

Expert Comment

by:VK
ID: 9714413
Hello RaedKhlaifat !

It's not clear what you mean:
---------------------------------

You can concatenate strings by this way for example:

Dim strTemp As String
strTemp = strTemp & chr(int(rnd*255))

Incrementing a String is an other topic:

"ABC" incremented by one is then "ABD"

This is more complicated:

Public Function IncrementString(Text As String, Min As Byte, Max As Byte)
    Dim strTemp As String
    Dim Count As Byte
   
    Dim i As Long
    Dim n As Long
    Dim x As Boolean

    Select Case True
        Case Len(Text) = 0
        Case Min > Max
        Case Else
            Count = Max - Min + 1
            n = Asc(Right(Text, 1)) - Min
            x = (n + 1 = Count)
            strTemp = Chr(Min + ((n + 1) Mod Count))
            For i = Len(Text) - 1 To 1 Step -1
                n = (Asc(Mid(Text, i, 1)) - Min + IIf(x, 1, 0)) Mod Count
                x = (n = 0)
                strTemp = Chr(Min + n) & strTemp
            Next
            If x Then
                strTemp = Chr(Min) & strTemp
            End If
    End Select
    IncrementString = strTemp
End Function

Usage:

? IncrementString("ABC",65,90)
AABD
? IncrementString("AZZ",65,90)
BAA
? IncrementString("ZZZ",65,90)
AAAA

Because it seems that you have only one item in your file:
------------------------------------------------------------------

1. You might have overwritten the content of your global string with the text of the next listitem every time in the loop.
    Instead append your global string by the text . See how to concatenate strings in vb above.
2. You might have forgotten to add (chr(13) & chr(10))  = CR+LF = vbNewline to the end of the text of each listitem before appending to the file. In this case there should be only a big row containing all the text.

V.K.
0
 

Author Comment

by:RaedKhlaifat
ID: 9718815
Thank you all for your comments,

 I ahve just used an array of strings as a buffer, which clears itself after each usage, and that solved the problem, even though I was hoping for a more Robust solution, such as String A += String B , and whatever strings that are paased to String A will be added to its value, but it seems like it doesn't work that way in VBA, like in Java.

 Anyway, thank you all for your effort, it has all been very useful.

Cheers
Raed Khlaifat
0
 
LVL 8

Expert Comment

by:bukko
ID: 9721397
It DOES work that way in VBA, but the syntax is different.

As posted earlier, the VBA equivalent of "A += B" is "A = A & B"

Never mind, good luck in the future.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

705 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