Deleting Records from files

Posted on 2005-04-19
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 85

    Accepted Solution

    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.

    LVL 6

    Expert Comment

    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)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    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…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now