[Tutorial] Reading/Writing Random Access Files
Author |
Message |
BioHazardousWaste
|
Posted: Wed Mar 03, 2004 10:27 pm Post subject: [Tutorial] Reading/Writing Random Access Files |
|
|
OK.. Forgive me if this tutorial is not pefect, because it is the first one i've written... here goes nothing.
First off, I must warn you, this tutorial is a little advanced, containing types, records, and file handling... so if your new to VB you will problably be confused as hell.
Random Access files are VERY useful, they allow you to write huge amounts of data to a file (generally a database file), and then read certain parts of the file back in. There are three parts to a Random Access File, and I will explain them in the following order:
1) The Record(s)
2) Writing to a File
3) Reading From A File
1) The Record(s)
The record is the most important part of this process. It holds all your information, in a nice easy to get to place. If you know how to create records allready, feel free to skip this part, otherwise read on.
RECORDS CANNOT BE CREATED IN A FORM!!! Never, they must be created in a module. To add a module to your project (in VB6.0 anyways) Right-Click on a blank spot in your Project Explorer, go to Add and click Module. Choose Module, click open. The code for your new module will be displayed. In this module you must create your records. This is done using the Type and End Type command, with your variables in between. For string variables, you must also specify how long the string is by using the * sign. e.x. for a 30 character string you would use VARNAME * 30.
EXAMPLE:
code: |
Type RecContactInfo
Name as String * 75
Address as String * 75
Address2 as String * 75
PhoneNum as String * 75
CellNum as String * 75
End Type
|
In this example we have 5 variables, Name, Address, Address2, PhoneNum, and CellNum, all strings, 75 characters long. (Note: You do not have to dim these, although you may want to in order to create arrays)
The next thing you have to do, is to declare this type as a record in the general section of the form you want to handle the data in.
EXAMPLE: I want the user to enter this information into previously created textboxes in frmInfo, then save to my record. The first thing I have to do is to decleare a variable as the record... in this case I might use
code: |
Dim CInfo as RecContactInfo
|
{do this in the general section}. With CInfo as a variable name, and RecContactInfo as the same name as the type I created in my module.
The next thing you have to do is assign data to the variables we created in our record. This is the easy part, you use the Record as any other object.
EXAMPLE: After the user is finished, I want to assign the text in the textbox name to the variable Name in my record.
To assign it to another textbox I would use:
code: |
txtOtherTextBox.text = txtName.text
|
Similarily, to assign it to a variable in a record I would use
code: |
CInfo.Name = txtName.text
|
In this case CInfo is the variable name of our record, and Name is the variable we want to assign the text to.
2) The second thing I want to do is save this record to disk. This is a little more advanced then saving a "normal" file, but not too much.
The command is :
code: | Open (Location) For Random As [#] Len = Len(Record)
Put [#] filenumber, [recnumber ], varname
Close [#]
|
EXAMPLE: I want to save this users information to the file C:\ContactInfo.dat I would use
code: | Open "c:\ContactInfo.dat" for Random As #1 Len = Len(CInfo)
Put #1 , , CInfo
Close #1
|
Open opens the file specified in location for use by my program, for Random tells it to open it as a Random Access File, as #1 is what file # it should be. (VB can have multiple files open at once, it keeps them straight by assigning a file # to each, which is what you are doing here), thats all old stuff, the catch is, you MUST specifiy the file length. In this example we tell VB that Length (Len) equals the length of our record (= Len(CInfo).
On the nextline we write our record to the file using the Put command, it is almost the same as in a normal file, except we must specify how many records we will be writing. So we use Put #1 to tell VB to write to Open File #1 (in this case {c:\ConatctInfo.dat}, then we specify the number of records we will be writing. I left the value blank {, ,}, so VB assumes I will be writing only one record, and lastly we tell it the name of the record to write. This name MUST be the variable you have declared in the general part of the form, not the type name. {e.g. CInfo, not RecCInfo}, and lastly close the file {Close #1}
NOTE: DO NOT ask me about writing more then 1 record at one time, I have never done it, and see no need to do it (except to save code space)
Now you have successfully written the users full name into a random access file.
3) Lastly, we want to read that information back into your record from the file. To do this, we open the file and get the information from it. Here is the command:
code: | Open (Location) For Random As [#] Len = Len(Record)
Get [#] filenumber, [recnumber ], varname
Close [#]
|
Now, if you were paying attention, you would noticed that the command to save the record, and the command to open the record only differ by one word. Put has been Changed to Get.... Yes, that means that as soon as you load data from a file, it overwites everything in the current record in your program.
EXAMPLE: I want to return the users name that I saved to the file c:\ConactInfo.dat into a textbox called Name. To do this, the first thing I have to do is read the record from a file.
code: |
Open "C:\ContactInfo.dat" for Random as #1 Len = Len(CInfo)
Get #1, , CInfo
Close #1
|
{If you don't understand this code, read the above section... saving to a file, the only difference is, instead of saving to a file, using the command Put, we are reading from a file using the command Get.}
Lastly, we want to assign the value that was just written into our record to the text box. We do it like this:
code: |
txtName.text = CIfno.Name
|
In this example txtName is the name of our textbox, CInfo is our variable name of our record {previously declared in the general section} and .Name is the variable inside the record that we want to take the data from.
This is the end of my first, very long tutorial. Congratulations, you can now Create A Record, Save a Record to a file, and Read Data back into the record. Now you should be really happy, and I am really sick of typing!!
Description: |
If you STILL can't do random access files, here is the working example I have been refering to throughout this tutorial. |
|
Download |
Filename: |
Random Access File Example.zip |
Filesize: |
1.55 KB |
Downloaded: |
1802 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
|
|
|
|
BioHazardousWaste
|
Posted: Thu Mar 04, 2004 9:21 am Post subject: (No subject) |
|
|
Quote: now, if say I was to do
Open "c:\ContactInfo.dat" for Random As #1 Len = Len(CInfo)
Put #1 , 2 , CInfo '//write to 2nd record
Close #1
I believe it would, but I am not positive, play around with it, or I will get back to you on Friday.
Quote:
and can I delete a record without rewriting the entire file?
I do not know of a way to do this, if you needed to the best thing I could think of would be to have a database control program which reads in all your records, lists them in a listbox, you could then choose which ones you want to delete. Have it read the file in, then output it with everything except for the records you want to delete.... Thats the only way I know of, and my manuals don't say anything about it sorry.
|
|
|
|
|
|
Tony
|
Posted: Thu Mar 04, 2004 5:22 pm Post subject: (No subject) |
|
|
that's alright... guess I can't expect much from a datafile
I just know that with a DB, you can just run a SQL statement to delete a record or two
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
BioHazardousWaste
|
Posted: Fri Mar 05, 2004 3:56 pm Post subject: (No subject) |
|
|
Its not really nessecary to delete a record, if you use WHICH record it is as a variable... ex. have a text box for which record spot you want to record in, and if it exists have the name in a textbox underneath it. Then, whenever you want to delete a record, just save a new one over top. Should work... let me kno if you need code/ example proggy
|
|
|
|
|
|
|
|