Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Access File management

Status
Not open for further replies.

TinkerBear

Programmer
Oct 1, 2001
8
US
Hello,

I can make a data entry side to a random access file...it will write all the data i want into the file just fine.

But i want the user to be able to enter info, then later on shufle through them, and delete them. Im sure its a really simple concept/algorithm, but I just cant get it to work...here is some of the code i used to setup the file.

Open "File Spec" For Random As #1 Len = Len(teamOne)
~
~
~
lastrecord = LOF(1) / Len(teamOne)
Put #1, lastrecord + 1, teamOne

How would i shuffle and delete specific records.

Thanks
Tink
 
Hi Tink,
There is a problem with trying to delete records from a random access file. This is what msdn has to say about it

Deleting Records
You could delete a record by clearing its fields, but the record would still exist in the file. Usually you don’t want empty records in your file, because they waste space and interfere with sequential operations. It is better to copy the remaining records to a new file, and then delete the old file.

To remove a deleted record in a random-access file

1.Create a new file.
2.Copy all the valid records from the original file into the new file.
3.Close the original file and use the Kill statement to delete it.
4.Use the Name statement to rename the new file with the name of the original file.

As to shuffling,you could create an array of shuffled numbers and use them to put records randomly out of the original file and then put them sequentially to the new file.

Hope this helps

Jon
 
Hmmmmmmmmmmmmm,

That's what MS SAYS ... What it DOES is something else (at least in the 'low end' products). Include a FLAG field in the record. To "Delete" the record, set the flag true. Include code in your app to check the flag field when manipulating records. It can be a bit tricky, but you should be able to avoid showing deleted records and still process the file 'correctly'. In the 'best case' scenario, you would check the flag field when adding NEW records, and overwrite deleted records with new ones.

On the other hand, you COULD just create an MDB file and let MS. do the dirty work via the Jet engine.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Ok, so in addition to the fields i need in there, i add an "invisible" flag, that is true...

Then when the user clicks is looking at previous entries and clicks delete, it will delete that flag and the amount of entries above it like

i have 10 fields, 1 flag. 11 fields total. If he clicks delte it will delete that flag and the ten fields above it right?

Im not sure how I would actually go about that with the actual deleting.

I could make all the fields cept the flag null and make the flag false. Then when they go to add a record, it will search for any false flags, and put the entry in there...

That sound workable? Sounds like a pain to me =] But anythings worth a shot...

As far as the mdb goes, i do have a book on visual basic 6 database programming, buts its a really bad book...is there any online "tutorials" or references you know about I could look at?

Thx
Tink
 
Hmmmmmmmmmmmmmmmmmm,


Close. The Flag is correct. The usage can be in many forms, but what you describe is not the way I would do it.

On the record 'add' operation, the flag would be left (set to) FALSE. When a record was DELETED, set the FALG to True. When Traversing records, Check the Flag. If it is false, the record is valid, so process it however (load into a variable "Set" for use (display or perform some operation on the group). If the flag is TRUE, just ignore the record. For example, if you 'get' the next record and the Flag is TRUE, just do another 'GetNext' until you either find a record where the flag is false or you find (get to) EOF. When you add a new record, also traverse the group to see if there are any "Deleted" records. If any are found, Replace the FIRST one found with the record to be added (making sure you set the Flag to FALSE. MS A. does (parts of) this. It doesn't normally 'replace' deleted records with new additions, which is (part of) why the MDB file doesn't shrink when records (or recordsets) are deleted. Ms. A. (MDB) files have a 'special' routine (COmpact) which - when invoked- goes through the process of creating a 'new' file and copying all of the NOT deleted records fromt he old file to the new file, deletiing the old file and re-naming the old file with the new name -as described earlier.

Of course, you can get more sophisticated than my description, by adding a seperate "file" (table) which keeps track of some of the bookkeeping (such as the 'record numbers of deleted records, or a index list of key values), or you can elaborate the basic file and make it a bi-directional linked list, so the deletion consist of simply removing (or setting to an invalid value) the Next and previous pointers.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top