Hi, I have a database system which works by entering a customers first name and second name, a1, a2, a3, postcode, etc..... each customers information is stored in separate *.txt files generated using the first and second name of the customer. deleting the customer files them self are not the problem as I use a simple KILL statement (if this is the wrong way to go about this please let me know)
The the .txt file I am having trouble with is the one I am using to hold all the full names. these are writin out each time a customer is added and are placed on a new line every time and I should point out that they are being writin (as APPEND) as one STRING each time, which is takin from both the first and second name (text boxes)
here is the code for the txt (that writes out the customer names):
Code:
Dim CustomersNames(0 To 999999) As String
Code:
Dim i As Integer
i = 0
Open "CUSTOMERS_NAMES.TXT" For Input As 1#
Do While Not EOF(1)
Input #1, CustomersNames(i)
cmbFindCus.AddItem CustomersNames(i)
Loop
cmbFindCus.Text = CustomersNames(0)
Close #1
End Sub
here is the same file but for input into the program:
Code:
Dim i As Integer
i = 0
Open "CUSTOMERS_NAMES.TXT" For Input As 1#
Do While Not EOF(1)
Input #1, CustomersNames(i)
cmbFindCus.AddItem CustomersNames(i)
i = i + 1
Loop
cmbFindCus.Text = CustomersNames(0)
Close #1
and finally this is how I am trying and failing to delete one of the entity's:
Code:
Dim i As Integer
i = 0
Kill (App.Path & "\" & cmbFindCus.Text & ".txt")
Open "CUSTOMERS_NAMES.txt" For Output As 1#
Do While Not EOF(1)
If i = cmbFindCus.ListIndex Then
Write #1, (cmbFindCus.Text); (cmbFindCus.ListIndex)
i = i + 1
End If
Write #1, CustomersNames(i)
i = i + 1
Loop
cmbFindCus.Text = CustomersNames(0)
Close 1#
I should also add that when I test using this code it deletes everything that is in the file.
In a nutshell, there is no quick way to delete a "record"
in a .TXT file.
But, here is a concept for a brute force method.
Let's say you have 100 "records" (ie, 100 lines) in the file,
and you want to delete line #76.
1. Copy lines 1 to 75 of MyFile.txt to a new file, say Temp.txt.
2. Skip line 76
3. Copy lines 77 to 100 of MyFile.txt to Temp.txt.
4. Then, "clear" out MyFile.txt, and copy all lines from Temp.txt to MyFile.txt
TY spoo, that does seem long winded but it seems pretty easy =] so in more coding terms it would be:
1) open for input
2) copy lines 0 - combo.listindex -1 into array
3) copy lines (combo.listindex) + 1...... till EOF
4) clear original
5) write out to file agian
first of all would this 5 step thing work and I also have two other questions:
1) what sort of coding would I need to copy entities to a temp array?? a quick code snip-it would be awsome
2) how do I clear a .txt I did this by accident in my attempt of deleting one entity XD but is there a clean way??
As for writing to temp array, maybe something like this ...
Code:
' 0. set default values
nn = combo.ListCount
aa = 0
fname = "CUSTOMERS_NAMES.TXT"
' 1. create temp array
Dim aaTemp()
ReDim aaTemp(nn)
' 2. open file from which to read
Open fname For Input as #1
' 3. proceed
For ii = 0 to nn - 1
Line Input #1, txt
If ii <> combo.ListIndex Then
aa = aa + 1
aaTemp(aa) = txt
End If
Next ii
Close #1
Conceptually, it goes through fname in one pass, reading
line by line, and skips over the line that = combo.ListIndex.
Note: I did not test this, so some tweaking may be required.
I am using basically the same approach you did in your
own code snippets, but with slight modifications to match
with what I am more familiar with. Either way should be ok.
Right I don't understand what I am doing wrong but it just clears out the whole txt file?
Code:
Dim WhatLine As Integer, DeletedCus As Integer
DeletedCus = cmbFindCus.ListIndex
WhatLine = 0
Dim aaTemp()
ReDim aaTemp(cmbFindCus.ListCount)
Open "CUSTOMERS_NAMES.txt" For Input As #1
Do While Not EOF(1)
If WhatLine = DeletedCus Then
WhatLine = WhatLine + 1
End If
Line Input #1, CustomersNames(WhatLine)
WhatLine = WhatLine + 1
Loop
Close #1
WhatLine = 0
Open "CUSTOMERS_NAMES.txt" For Output As 1#
Do While Not EOF(1)
Write #1, aaTemp(WhatLine)
WhatLine = WhatLine + 1
Loop
Close 1#
Last edited by seditives; Jun 20th, 2011 at 02:35 PM.
1. You never populated aaTemp() ..
2. I think you want a separate counter for the array -- I used aa
3. For your output, you used 1# instead of #1
4. For your output, I think you want to use a loop based on the temp array, not on the file itself
So, something like this, perhaps
Code:
Dim WhatLine As Integer, DeletedCus As Integer
Dim aa As Integer ' array counter
aa = 0
DeletedCus = cmbFindCus.ListIndex
WhatLine = 0
Dim aaTemp()
ReDim aaTemp(cmbFindCus.ListCount)
Open "CUSTOMERS_NAMES.txt" For Input As #1
' 1. pop array
Do While Not EOF(1)
Line Input #1, txt
If WhatLine = DeletedCus Then
b = nada ' do nothing
Else
aaTemp(aa) = txt ' populate array with text
aa = aa + 1 ' increment array counter
End If
WhatLine = WhatLine + 1
Loop
Close #1
' 2. re-pop file
WhatLine = 0
Open "CUSTOMERS_NAMES.txt" For Output As #1
For ii = 0 to aa - 1
Write #1, aaTemp(ii)
Next ii
Close #1
As a possible glitch #5, I think you need to specify the full file path,
such as "c:\myDir\mySubDir\CUSTOMERS_NAMES.txt"
Please allow me to be the devil's advocate on this thread. Why delete the "record" in the first place?
Instead, hold a 1-byte position on the record indicating whether to ignore or read it. If the record must be changed, append the new record to the file as the one that should be read in, and update the old record to ignore it.
That way you avoid building a new file every time a record is updated. You can always periodically update the file with a new one that is packed a little tighter but not every time the file is updated.
Right spoo your code half worked, the problem is it seems to be rediming each line of the file or something along those lines. Here is what the file turns out like:
quote: the two numbers on each line are representing first and second name
as you can see it has got rid of the correct line but it seems to put 2 extra "" each time ? this is causing the combo box to display a couple of empty lines in between each customer???
Last edited by seditives; Jun 21st, 2011 at 06:55 AM.
OK, I am worn thin from this deleting of the line entity lol but its not your thought spoo as I have learned a lot from your posts but I think it's about time I just handed this in for an expert like your self to run through like you said because I am starting to pull my hair out lol (it sucks being a newbie to programing ) anyway I have attached the txt file with test information in. and also a little project that I have set up for the soul purpose of making this god damn thing work I have gave you all of the basic needs all that needs to be done is the coding for just one file.
I am sure this is a easier option instead of me uploading the whole project or keep going back and forward on posts.
thanks in advanced, Seditives
Last edited by seditives; Jun 21st, 2011 at 07:45 AM.
don't worry Spoo you have helped me a lot anyway. And I am not directing all the questions to you they are open to any one who can help =] are see what I can do for now and are post if I come up with a good solution =P
I am starting to get there now but still have some problems with the coding.
this is how i am coding it at the moment
Code:
Dim CustomersNames(0 To 999999) As String
Private Sub cmdDelete_Click()
Dim i As Integer
Dim DeletedCus As Integer
i = 0
DeletedCus = Combo1.ListIndex
Open "C:\Users\Alex Winter\Desktop\Project1\CUSTOMERS_NAMES.txt" For Input As 1#
Do While Not EOF(1)
Input #1, CustomersNames(i)
If i = DeletedCus Then
i = i + 1
End If
List1.AddItem CustomersNames(i)
i = i + 1
Loop
Close 1#
i = 0
Open "C:\Users\Alex Winter\Desktop\Project1\CUSTOMERS_NAMES.txt" For Output As 1#
For i = 0 To List1.ListCount
Write #1, CustomersNames(i)
Next i
Close 1#
End Sub
I am having two issues:
1) The "List1" shows all of the customers and leaves a blank row where the deleted customer used to be
2) When a check the txt file after I have stoped running the program it still shows the customer I wanted to delete but with a blank entity under
for example if I wanted to delete John Adam from 5 other customers it should show in the .txt file as:
I am sorry to say Spoo but it is still having a problem I have replaced the If statement with yours and this does seem to sort the space problem at least in the List Box but now doesn't change the txt file at all????
I have thought the the code through and have came to the conclusion that it may be the fact that I am using the same dim'd array "CustomerNames" to INPUT and OUTPUT the Information, so the program may just be skiping over the List Box part and just writing the same thing back out agian, this is because I have read in from the file on form load (for the ComboBox) using the same dim'd array.
In easy to understand terms (as I have been told I am not good at explaining code XD):::::::
1) Program opens
2) Reads in all info from the txt file into the one array I have (CustomersNames)
3) Click on a customer name from the drop down
4) I press the delete button
5) the code you suggested runs, but, only to the benefit of the ListBox
6) the program now forgets about that because its has done as it has been asked like a good boy.... or girl (I so hope programs aren't girls no offense if you are a girl reading this )
7) the program then goes on to do the next thing set by its master (the writing out to the same file) but as it doesn't know my intentions it just uses the original CustomersNames array that was kindly introduced to the program by me at the start
Sorry for being extremely patronizing but I hope this made you understand this the way my funny little brain saw this problem
Last edited by seditives; Jun 22nd, 2011 at 01:17 PM.
A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov
Aha .. I was wondering what CustomersNames(i) was all about.
Now it is clearer
Yes, so in case you have not already done so, it appears that
you also want to change your Write algo from ...
Code:
For i = 0 To List1.ListCount
Write #1, CustomersNames(i)
Next i
... to something like this
Code:
For i = 0 To List1.ListCount
Write #1, List1.ListIndex(i)
Next i
I have not worked with ListBoxes, so I'm not sure
it that is the right syntax or property, but it should
be close (hopefully)
Also note that while you are properly doing ...
Input #1
Write #1
... you still have a syntax error in your Open and Close statements
you have: Open 1#
should be: Open #1
... same for Close
EDIT:
Apparently these syntax errors are not fatal, as you
are at least able to populate the ListBox using Open 1#.
But, I think you should change the code to prevent
unexpected results.