Deleting Records from files

Posted on 2005-04-19
Medium Priority
Last Modified: 2010-05-02
I first define this in a module

Type Customer  
   ID As Integer
   Name As String
End Type

And then I do this:

Dim MyRecord As Customer, RecordNumber

Open "C:\TESTFILE.txt" For Random As #1 Len = Len(MyRecord)
For RecordNumber = 1 To 5  
   MyRecord.ID = RecordNumber  
   MyRecord.Name = "My Name" & RecordNumber  
   Put #1, RecordNumber, MyRecord  
Next RecordNumber
Close #1

Then I can use Get for getting the information... Suppose I want to delete or edit a record in the text file, is there any way to do that?
Question by:bijuabraham
LVL 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 13821408
You have three basic options:

1) Open the file in random mode.  Read all the records sequentially.  As you read, echo them to a second random file.  Do not write any records that are to be "deleted" to the second file.  When done, delete the original and rename the second.

2) Open the file in random mode.  Start at the record to be deleted.  Read the next record and store it in memory.  Put that record back into the file at the position of the record to be deleted.  Now move up to the next record and repeat until you reach the end of the file.  This "bubbles down" the records to fill the gap.

3) Modify your record UDT so that it contains a "deleted" flag.  Instead of removing deleted records from the file you simply mark them as deleted and leave them in place.  When processing records, ignore those with the deleted flag set.  When you want to add a new record, you first walk the file and see if their is a "deleted" record that you can replace with the new records information.


Expert Comment

ID: 13833045
why dont you use an existing database technology, instead of writing your own?

"INSERT INTO tblPeople VALUES(1234, 'Fred Smith')"

looks much easier to me than opening and writing text files. if youve never used databases then you should; theyre dead easy and even if youre not comfortable with them, the time you spend learning them is a far better spend of time than the time spent writing a complicated module to read and write text files...

also, there are database drivers by microsoft for text files; they can read and write and delete from an ordinary text file as if it were a database, but you really should use a more mature technology like access (light work) or msde (heavy work)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question