Solved

String variable Incrimentation and ListBoxes in VBA

Posted on 2003-11-09
11
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

732 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