VB2022 VB2019 VB6 VB Sample Code About Us

Lesson 17 : Creating Files

17.1 Introduction

Up until Lesson 13, our focus has been on creating programs that accept data at runtime, with the drawback that the data vanishes once the program ends. However, in this upcoming lesson, we will delve into a valuable skill: learning how to create and store files. By utilizing a customized VB program, we'll be able to write these files to a storage device and subsequently retrieve their contents through reading operations. This new knowledge will greatly enhance our programming capabilities.

17.2 Creating a File

To create a file , we use the following command

Open "fileName" For Output As #fileNumber

Each file created must have a file name and a file number for identification. As for the file name, you must also specify the path where the file will reside. For example:

Open "c:\My Documents\sample.txt" For Output As #1

will create a text file by the name of sample.txt in My Document folder in C drive. The accompanied file number is 1. If you wish to create a HTML file , simply change the extension to .html

Open "c:\My Documents\sample.html" For Output As # 2

17.2.1 Sample Program : Creating a text file

Private Sub create_Click() 
Dim intMsg As String
Dim StudentName	As String
Open "c:\My Documents\sample.txt" For Output As #1 
 intMsg = MsgBox("File sample.txt opened")
 StudentName = InputBox("Enter the student Name")
 Print #1, StudentName 
 intMsg = MsgBox("Writing a" & StudentName & "to sample.txt")
Close #1 
 intMsg = MsgBox("File sample.txt closed")
End Sub

* 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 the user 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 1: Reading file

Private Sub Reading_Click()
Dim variable1 As String
Open "c:\My Documents\sample.txt"
For Input As  #1 
 Input  #1, variable1
 Text1.Text = variable1
Close  #1 
End Sub 

* This program will open the sample.txt file and display its contents in the Text1 textbox.

17.3.2 Sample Program 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 much easier than the previous examples.Many operations are handled by the common dialog box. The following is the program:

Dim linetext As String
Private Sub open_Click()
CommonDialog1.Filter = "Text files{*.txt)|*.txt"
If CommonDialog1.FileName <>"" Then
 Open CommonDialog1.FileName For Input As #1
 Input #1, linetext
 Text1.Text = Text1.Text & linetext
Loop Until EOF(1)
End If
Close #1<
End Sub
Private Sub save_Click()
CommonDialog1.Filter = "Text files{*.txt)|*.txt"
If CommonDialog1.FileName <>"" Then
 Open CommonDialog1.FileName For Output As #1
 Print #1, Text1.Text
Close #1
End If
End Sub

*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 the save file dialog box. Text1.Text = Text1.Text & linetext is to read the data and display them in the Text1 textbox

The Output window is displayed beneath.

Figure 17.1

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy