-
Sometimes I get an error when performing this code, anyone know how to avoid this error? Trying to Append Open File
Private Sub Save_Click()
Dim i As Integer
Open "events.txt" For Append As #1
For i = 0 To Events.ListCount
Print #1, Events.List(i)
Next i
For i = 0 To Players.ListCount
Print #1, Players.List(i)
Next i
'Save the text from Text1 as well
Print #1, Session.text
Print #1, Map.text
Print #1, MaxScore.text
Print #1, MaxTime.text
Print #1, MaxTons.text
Close #1
End Sub
-
<?>
what is the error...is it nothing in the listindex?
-
This could be the error, The listitems are starting from 0, not 1.
Code:
For i = 0 To Events.ListCount - 1
Print #1, Events.List(i)
Next i
For i = 0 To Players.ListCount - 1
Print #1, Players.List(i)
Next i
-
The error happens randomly and it says "Trying to Append All ready open file"
Then it closes....... Ok If I restart my app its ok.
The textfiles and listboxes contain information yes..... It doesn't crash if they don't either
Basically I think that if I open events.txt (which I did) and then close that for someone reason Appen seems to think its still open after I close it.
-
Instead of #1 you should put a filenumber you know is free
Like this:
Code:
Dim ff as integer
ff=Freefile
Open "events.txt" For Append As ff
.
.
Print #ff, Events.List(i)
-
I know that on occasion, I can find myself getting an error like that when I am working in a hurry and not using the FreeFile function. You might give it a try, doing something like this:
Code:
Private Sub Save_Click()
Dim i As Integer
Dim FF as integer
FF=FreeFile
Open "events.txt" For Append As FF
'If you loss the Next i bit and just make it Next,
'you will notice a speed increase on large loops.
If Events.ListCount<>0 then
For i = 0 To Events.ListCount
Print #FF, Events.List(i)
Next
End if
If Players.ListCount<>0 then
For i = 0 To Players.ListCount
Print #FF, Players.List(i)
Next
End if
'Save the text from Text1 as well
Print #FF, Session.text & Map.text & MaxScore.text & _
MaxTime.Text & MaxTons.Text
Close FF
End Sub
Hope this is helpful.