Solved

String variable Incrimentation and ListBoxes in VBA

Posted on 2003-11-09
11
310 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now