Is there a Labels Collection?

Tim_Myth
09-06-2003, 05:38 PM
I'm using VB 6, and I have a form containing about 250 labels. The captions of the labels are updated as the data changes. I would like to write the caption of each label to a text file when the program is closed out and read them in again the next time the program is opened. I hoped this would be a simple task, but the following code does not work.


Private Sub form_unload(cancel As Integer)
Dim fs, a
Dim X As Integer
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\MyDomInfo.txt", True)
a.writeline ("This is a test.")
For Each Label In frmDomBrowser.Label1
a.writeline (frmDomBrowser(X).Caption)
Next
a.Close
MsgBox ("Done")
End Sub


Any suggestions? (For ease of programming, I named each label descriptively rather than sticking with teh standard Label1, Label2, Label3, etc. naming. Doh!)

Squirm
09-06-2003, 06:01 PM
You're using a For Each loop but are then accessing the control by index, using the variable X which will never change from its default value of 0. Perhaps this is more in order?

Dim theLbl As Label

For Each theLbl In frmDomBrowser.Label1
a.writeline theLbl.Caption
Next theLbl

You also have a lot of unneeded parenthesis, not to mention the use of the FSO in this instance is utterly pointless. You would be better off in the long run learning how to use VB's native file I/O.

Tim_Myth
09-06-2003, 06:02 PM
If at first you don't succeed, just fiddle with the code some more. ;)

This worked for writing it to the file:


Private Sub form_unload(cancel As Integer)
Dim fs, a
Dim X As Variant
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\MyDomInfo.ToD", True)
On Error Resume Next
For Each X In frmDomBrowser
a.writeline (X.Caption)
Next
a.Close
End Sub


Now if I can read it in!

bk2003
09-06-2003, 06:02 PM
Use my code as example:

Private Sub form_unload(cancel As Integer)
Dim fs As Object
Dim a As Object
Dim X As Integer
' Set fs = CreateObject("Scripting.FileSystemObject")
' Set a = fs.CreateTextFile("c:\MyDomInfo.txt", True)
' a.writeline ("This is a test.")
For Each lbl In frmDomBrowser
If TypeName(lbl) = "Label" Then Debug.Print lbl.Caption
' a.writeline (frmDomBrowser(X).Caption)
Next
' a.Close
MsgBox ("Done")
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum