pjfatboy 07-17-2003, 05:36 PM I have a Combo List Box on a form in my program. I have provided a way for the user to add to the list. I then save the list in a file. My problem is, ever time the use adds to the list and the file is saved, instead of overwriting the file, the file is duplicated and added to the end of the file. When the form is loaded the list shows the list duplicated. Could someone show me how to overwrite the file instead of adding to the file or could you show me how to just add the new list item. Here is the code I am using to save the list.
Private Sub SaveList()
Dim a As Integer
Dim ff As Integer
Dim Header As String
Dim LCount As Long
Dim numbyte As Byte
ff = FreeFile
Open App.Path & "\list.txt" For Binary As #1
Header = "SavedList"
Put ff, 1, Header
LCount = Form1.ExerciseInputcmb.ListCount
Put ff, , LCount
For a = 0 To Form1.ExerciseInputcmb.ListCount - 1
numbyte = Len(Form1.ExerciseInputcmb.List(a))
Put ff, , numbyte
Put ff, , Form1.ExerciseInputcmb.List(a)
Next a
Close ff
End Sub
_________________________________________________________
Here is how I add to the list.Thank for any help.
Private Sub OKButton_Click()
Form1.ExerciseInputcmb.AddItem AddExercise, ListIndex
AddTodlg.ExerCombo.AddItem AddExercise, ListIndex
Removedlg.ECombo.AddItem AddExercise, ListIndex
Call SaveList
Unload Me
End Sub
Squishy 07-17-2003, 05:43 PM I've never used Binary, but Open App.Path & "\list.txt" For Output As ff will overwrite the file...
passel 07-17-2003, 05:53 PM I have a Combo List Box on a form in my program. I have provided a way for the user to add to the list. I then save the list in a file. My problem is, ever time the use adds to the list and the file is saved, instead of overwriting the file, the file is duplicated and added to the end of the file. When the form is loaded the list shows the list duplicated. Could someone show me how to overwrite the file instead of adding to the file or could you show me how to just add the new list item. Here is the code I am using to save the list.
Private Sub SaveList()
Dim a As Integer
Dim ff As Integer
Dim Header As String
Dim LCount As Long
Dim numbyte As Byte
ff = FreeFile
Open App.Path & "\list.txt" For Binary As #1
Header = "SavedList"
Put ff, 1, Header
LCount = Form1.ExerciseInputcmb.ListCount
Put ff, , LCount
For a = 0 To Form1.ExerciseInputcmb.ListCount - 1
numbyte = Len(Form1.ExerciseInputcmb.List(a))
Put ff, , numbyte
Put ff, , Form1.ExerciseInputcmb.List(a)
Next a
Close ff
End Sub
_________________________________________________________
Here is how I add to the list.Thank for any help.
Private Sub OKButton_Click()
Form1.ExerciseInputcmb.AddItem AddExercise, ListIndex
AddTodlg.ExerCombo.AddItem AddExercise, ListIndex
Removedlg.ECombo.AddItem AddExercise, ListIndex
Call SaveList
Unload Me
End Sub
You made a mistake.
You get a file ID number:
' ff = FreeFile
but then you open the file as #1
-------------------
(Edit: Ignore the next line, continue reading below.)
---------------------
You never close #1, so you just keep adding to it.
I'm not sure why you're not getting file already open errors, or
some kind of error on the close.
You should change your open line to:
Open App.Path & "\list.txt" For Binary As ff
P.S. Actually, I don't see how the code in the question can be working
at all, Oh, yes I can. More than likely, ff ends up being 1 so the fact
that you use a literal 1 doesn't cause a problem, but you should fix it
anyway because it's a potential problem.
P.P.S Except for the #1 problem I mentioned, this code looks like it
should work, you specify "1" for your first write to the binary file, so it
should be overwritting the file.
You can ignore what I first said about not closing the file, and appending
to it because ff has to be equal to 1 or you would be getting other errors.
You don't show how you read from the file. Since this code looks good
to me, I don't really thing you are writting the data twice to the file.
How do you read the file. Are you clearing your list, before adding items
from the file? Maybe you're not appending to the file, you're appending
to your list.
' You should put a breakpoint in and step through the code, both when
you write and when you read, to examine what is going on.
pjfatboy 07-19-2003, 11:15 PM I have a Combo List Box on a form in my program. I have provided a way for the user to add to the list. I then save the list in a file. My problem is, ever time the use adds to the list and the file is saved, instead of overwriting the file, the file is duplicated and added to the end of the file. When the form is loaded the list shows the list duplicated. Could someone show me how to overwrite the file instead of adding to the file or could you show me how to just add the new list item. Here is the code I am using to save the list.
Private Sub SaveList()
Dim a As Integer
Dim ff As Integer
Dim Header As String
Dim LCount As Long
Dim numbyte As Byte
ff = FreeFile
Open App.Path & "\list.txt" For Binary As #1
Header = "SavedList"
Put ff, 1, Header
LCount = Form1.ExerciseInputcmb.ListCount
Put ff, , LCount
For a = 0 To Form1.ExerciseInputcmb.ListCount - 1
numbyte = Len(Form1.ExerciseInputcmb.List(a))
Put ff, , numbyte
Put ff, , Form1.ExerciseInputcmb.List(a)
Next a
Close ff
End Sub
_________________________________________________________
Here is how I add to the list.Thank for any help.
Private Sub OKButton_Click()
Form1.ExerciseInputcmb.AddItem AddExercise, ListIndex
AddTodlg.ExerCombo.AddItem AddExercise, ListIndex
Removedlg.ECombo.AddItem AddExercise, ListIndex
Call SaveList
Unload Me
End Sub
You made a mistake.
You get a file ID number:
' ff = FreeFile
but then you open the file as #1
-------------------
(Edit: Ignore the next line, continue reading below.)
---------------------
You never close #1, so you just keep adding to it.
I'm not sure why you're not getting file already open errors, or
some kind of error on the close.
You should change your open line to:
Open App.Path & "\list.txt" For Binary As ff
P.S. Actually, I don't see how the code in the question can be working
at all, Oh, yes I can. More than likely, ff ends up being 1 so the fact
that you use a literal 1 doesn't cause a problem, but you should fix it
anyway because it's a potential problem.
P.P.S Except for the #1 problem I mentioned, this code looks like it
should work, you specify "1" for your first write to the binary file, so it
should be overwritting the file.
You can ignore what I first said about not closing the file, and appending
to it because ff has to be equal to 1 or you would be getting other errors.
You don't show how you read from the file. Since this code looks good
to me, I don't really thing you are writting the data twice to the file.
How do you read the file. Are you clearing your list, before adding items
from the file? Maybe you're not appending to the file, you're appending
to your list.
' You should put a breakpoint in and step through the code, both when
you write and when you read, to examine what is going on.
I found the problem. In my Load_Form Sub I have the AddItem List and I have the Load_Form also reading and loading the List from the file, so the program was loading the list twice. I eliminated the AddItem List and just let the combo box populate with the List from the file. That took care of the problem. Thanks for the help.
|