String variable Incrimentation and ListBoxes in VBA


 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  
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use array instead:

dim strAry() as string

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

~ fantasy ~

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dim myString as String

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

debug.print myString

this will result in:   blah more text foo
Dim i
Dim str

For i = 0 To List1.ListCount - 1
str = str & List1.List(i) & vbCrLf
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

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

    String1 += String2;


    String1 = String1 & String2



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
            If x Then
                strTemp = Chr(Min) & strTemp
            End If
    End Select
    IncrementString = strTemp
End Function


? IncrementString("ABC",65,90)
? IncrementString("AZZ",65,90)
? IncrementString("ZZZ",65,90)

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.

RaedKhlaifatAuthor Commented:
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.

Raed Khlaifat
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.