Hi guys I was wondering I use the following code to load to a texbox from a textfile
VB Code:
Open App.Path & "\status.txt" For Append As #1 Write #1, "Manual" Close #1
but how do I load to a listbox?
Thanks!!
Printable View
Hi guys I was wondering I use the following code to load to a texbox from a textfile
VB Code:
Open App.Path & "\status.txt" For Append As #1 Write #1, "Manual" Close #1
but how do I load to a listbox?
Thanks!!
Code:Private Sub Command1_Click()
dim line as string
fno = FreeFile()
Open App.Path & "\yourfile.txt" For Input As #fno
Do While Not EOF(fno)
Line Input #fno, Line
ListBox.AddItem Line
Loop
End Sub
sorry, 2 lines have been altred in my code.
1. declaration of line variable
2. an unwanted endif
Thank you :)
Can list boxes on QueryUnload Events write to a textfile as well?
Listbox doesn't write anything to anywhere - you do that. :)
Code:Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim i As Integer
Open App.Path & "\status.txt" For Append As #1
For i = 0 To List1.ListCount - 1
Print #1, List1(i)
Next i
Close #1
End Sub
Sorry to bump this, but instead of making a new thread about a followup question I had about this I was just wondering..
I have a form called ignore and the query unload is part of the main form, form2. the list1 is on the ignore form. So my question is how do I get whatever is written to the list1 on the ignore form to be wirtten to the text file when the program is closed?
Thanks1!!
When dealing with controls from another form in the same application just simply call the forms name first
Casey.Code:For i = 0 To Form1.List1.ListCount - 1
Print #1, Form1.List1(i)
Next i
I'm guessing that the ignore form is still running while the main form is. If so, you can simply use the above code, and modify it to print the ignore forms list.
Example:
Or use the query unload on the ignore form, so that any time it's closed, it prints to the designated text file.Code:Open App.Path & "\status.txt" For Append As #1
For i = 0 To IgnoreForm.List1.ListCount - 1
Print #1, IgnoreForm.List1(i)
Next i
Close #1
Thanks, I put in the code, but I get an error stating "Wrong number of arugments or invalid property assignment" and it highlights the ".List1" in the line Print #1, IgnoreForm.List1(i)Quote:
Originally Posted by Neato
????
Did you change 'IgnoreForm' to the name of your actual form?
yes, the actual name is just ignore
Sorry about the delay, phone call.. Anyways.. Replace that line with:
Print #1, Ignore.List1.List(i)
And that should take care of your problem.
Thank you so much !!!!!
Edit: Sorry to both you again but I was having a small problem with loading text to the list1 listbox.
The following code was posted in this threadVB Code:
dim line as string fno = FreeFile() Open App.Path & "\yourfile.txt" For Input As #fno Do While Not EOF(fno) Line Input #fno, Line ListBox.AddItem Line Loop
Now while I use this code I get the error file is already open. I think that is because the load the textfile to the listbox is used before the write to listbox later on.
Does the textfile in the textfile to listbox get closed when it opens? Would that explain why when I get the error file is already open when It comes to the listbox to textfile code?
Thansk1!1
When you open a file you should close it after you have finished with it so you dont get the error every time you try to open it again.
Casey.Code:fno = FreeFile()
Open App.Path & "\yourfile.txt" For Input As #fno
Do While Not EOF(fno)
Line Input #fno, Line
ListBox.AddItem Line
Loop
Close #fno
Thank you Casey, I changed the code a bit so it looks like this
VB Code:
fno = FreeFile() Open App.Path & "\ignorelist.txt" For Input As #fno Do While Not EOF(fno) Line Input #fno, line Ignore.List1.AddItem line Loop Close #fno
When I press the command button, I get the error arugment not optional and it highlights the "line" in the "Ignore.List1.AddItem line" line.
Would that mean I can't load to a list1 on another form this way?