|
|
17.1 IntroductionUp until lesson 13 we are only creating programs that can accept data at runtime, when the program is terminated, the data also disappear. Is it possible to save data accepted by a VB program into a storage device, such as a hard disk,a pen drive or CDRW? The answer is possible. In this chapter, we will learn how to create files by writing them into a storage device and then retrieve the data by reading the contents of the files using a customized VB program. 17.2 Creating files
17.2.1 Sample Program : Creating a text file
* The above program will create a file sample.txt in the My Documents' folder and ready to receive input from users. Any data input by users will be saved in this text file. |
| 17.3 Reading a file To read a file created in section 17.2, you can use the input # statement. However, we can only read the file according to the format when it was written. You have to open the file according to its file number and the variable that hold the data. We also need to declare the variable using the DIM command. 17.3.1 Sample Program: Reading file Private Sub Reading_Click() End Sub * This program will open the sample.txt file and
display its contents in the Text1 textbox.
|
|
|
Example 17.3.2 Creating and Reading files
using Common Dialog Box This
example uses the common dialog box to create and read the text file, which
is
|
The syntax CommonDialog1.Filter = "Text files{*.txt)|*.txt" ensures that
only the textfile is read or saved .The statement CommonDialog1.ShowOpen is to display
the
open file dialog box and the statement CommonDialog1.ShowSave is to display
thesave file dialog box. Text1.Text = Text1.Text & linetext is to read the data
and display
them in the Text1 textbox The Output window is shown below:
|