SlideShare a Scribd company logo
1 of 15
Sequential file handling
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 Documentssample.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 Documentssample.html" For Output As # 2
Sample Program : Creating a text file
Private Sub cmdCreate_Click() Dim intMsg As String
Dim StudentName As String
Open "c:My Documentssample.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
Reading a file
To read a file created, 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.
Sample Program: Reading file
Private Sub cmdReading_Click() Dim variable1 As String
Open "c:My Documentssample.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.
Common Dialog Control
The common dialog control provides a standard set of dialog boxes for operations
such as opening and saving files, setting print options, and selecting colors and
fonts. The control also has the ability to display Help by running the Windows Help
engine.
Figure 7.11 The common dialog control
The common dialog control provides an interface between Visual Basic and the
procedures in the Microsoft Windows dynamic-link library Commdlg.dll. To create a
dialog box using this control, Commdlg.dll must be in your Microsoft Windows
System directory.
You use the common dialog control in your application by adding it to a form and
setting its properties. The dialog displayed by the control is determined by the
methods of the control.
At run time, a dialog box is displayed or the Help engine is executed when the
appropriate method is invoked; at design time, the common dialog control is
displayed as an icon on a form. This icon can't be sized.
The common dialog control allows you to display these commonly used dialog
boxes:
 Open
 Save As
 Color
 Font
 Print
To use the common dialog control
1. If you haven't already done so, add the common dialog control to the toolbox by
selecting Components from the Project menu. Locate and select the control in
the Controls tabbed dialog, then click the OK button.
2. On the toolbox, click the CommonDialog control and draw it on a form.
When you draw a common dialog control on a form, it automatically resizes
itself. Like the timer control, the common dialog control is invisible at run time.
3. At run time, use the appropriate method, as listed in the following table, to
display the desired dialog.
Method Dialog
ShowOpen Open
ShowSave Save As
ShowColor Color
ShowFont Font
ShowPrinter Print
ShowHelp Invokes Windows
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 cmdOpen_Click()
CommonDialog1.Filter = "Text files{*.txt)|*.txt"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Form1.Caption = CommonDialog1.FileName
Open CommonDialog1.FileName For Input As #1
Do
Input #1, linetext
Text1.Text = Text1.Text & linetext
Loop Until EOF(1)
End If Close #1
End Sub
Private Sub cmdSave_Click()
CommonDialog1.Filter = "Text files{*.txt)|*.txt"
CommonDialog1.ShowSave
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Output As #1
Print #1, Text1.Text
Close #1 End If
Form1.Caption = "Saved to " & CommonDialog1.FileName
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
statementCommonDialog1.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 shown below:
Open save file vb6 program
Open save file vb6 program
Open save file vb6 program
Open save file vb6 program
Open save file vb6 program

More Related Content

What's hot

Java and internet fundamentals.
Java and internet fundamentals.Java and internet fundamentals.
Java and internet fundamentals.mali yogesh kumar
 
Programming in c
Programming in cProgramming in c
Programming in cvineet4523
 
Shell and its types in LINUX
Shell and its types in LINUXShell and its types in LINUX
Shell and its types in LINUXSHUBHA CHATURVEDI
 
CO PO MAPPING CO3I DMS 22319
CO PO MAPPING CO3I DMS 22319CO PO MAPPING CO3I DMS 22319
CO PO MAPPING CO3I DMS 22319ARVIND SARDAR
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++Nitin Jawla
 
Operating system architecture
Operating system architectureOperating system architecture
Operating system architectureSabin dumre
 
.net CLR
.net CLR.net CLR
.net CLRDevTalk
 
Text Editor for System software
Text Editor for System softwareText Editor for System software
Text Editor for System softwarekalaivanan vanan
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals INick Buytaert
 
Fundamentals of Computer Design including performance measurements & quantita...
Fundamentals of Computer Design including performance measurements & quantita...Fundamentals of Computer Design including performance measurements & quantita...
Fundamentals of Computer Design including performance measurements & quantita...Gaditek
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorialMohit Saini
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Parallel and distributed Computing
Parallel and distributed Computing Parallel and distributed Computing
Parallel and distributed Computing MIANSHOAIB10
 
Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...Mustafa Kamel Mohammadi
 

What's hot (20)

Java and internet fundamentals.
Java and internet fundamentals.Java and internet fundamentals.
Java and internet fundamentals.
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Shell and its types in LINUX
Shell and its types in LINUXShell and its types in LINUX
Shell and its types in LINUX
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Relational Calculus
Relational CalculusRelational Calculus
Relational Calculus
 
Vb.net ide
Vb.net ideVb.net ide
Vb.net ide
 
CO PO MAPPING CO3I DMS 22319
CO PO MAPPING CO3I DMS 22319CO PO MAPPING CO3I DMS 22319
CO PO MAPPING CO3I DMS 22319
 
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDYC AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDY
 
Compiler design
Compiler designCompiler design
Compiler design
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Operating system architecture
Operating system architectureOperating system architecture
Operating system architecture
 
.net CLR
.net CLR.net CLR
.net CLR
 
Text Editor for System software
Text Editor for System softwareText Editor for System software
Text Editor for System software
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
Fundamentals of Computer Design including performance measurements & quantita...
Fundamentals of Computer Design including performance measurements & quantita...Fundamentals of Computer Design including performance measurements & quantita...
Fundamentals of Computer Design including performance measurements & quantita...
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Parallel and distributed Computing
Parallel and distributed Computing Parallel and distributed Computing
Parallel and distributed Computing
 
Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...
 

Similar to Open save file vb6 program

Putting dialog boxes to work
Putting dialog boxes to workPutting dialog boxes to work
Putting dialog boxes to workchunky.sarath
 
I B.Sc CS questions & answers
I B.Sc CS questions & answersI B.Sc CS questions & answers
I B.Sc CS questions & answersHepsijeba
 
FILE TYPE/RUN/ FOLDER OPTION
FILE TYPE/RUN/FOLDER OPTIONFILE TYPE/RUN/FOLDER OPTION
FILE TYPE/RUN/ FOLDER OPTIONAife Ocubillo
 
Html introduction
Html introductionHtml introduction
Html introductionRanjithaM32
 
Creating a text editor in delphi, a tutorial
Creating a text editor in delphi, a tutorialCreating a text editor in delphi, a tutorial
Creating a text editor in delphi, a tutorialErwin Frias Martinez
 
windows and its components
windows and its componentswindows and its components
windows and its componentsprachi1210
 
LECTURE 12 WINDOWS FORMS PART 2.pptx
LECTURE 12 WINDOWS FORMS PART 2.pptxLECTURE 12 WINDOWS FORMS PART 2.pptx
LECTURE 12 WINDOWS FORMS PART 2.pptxAOmaAli
 
Lesson introduction
Lesson introductionLesson introduction
Lesson introductionMr_Burns
 
TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)Esmeraldo Jr Guimbarda
 
The Basics of Visual Studio Code.pdf
The Basics of Visual Studio Code.pdfThe Basics of Visual Studio Code.pdf
The Basics of Visual Studio Code.pdfcalfonzodaly
 
Microsoft Word Basics2.pptx
Microsoft Word Basics2.pptxMicrosoft Word Basics2.pptx
Microsoft Word Basics2.pptxSAStudio2
 
Visual c++ 2010_tutorial
Visual c++ 2010_tutorialVisual c++ 2010_tutorial
Visual c++ 2010_tutorialAyub Abd Rahman
 
Prep 2-booklet-2nd-term-2016-2017
Prep 2-booklet-2nd-term-2016-2017Prep 2-booklet-2nd-term-2016-2017
Prep 2-booklet-2nd-term-2016-2017khawagah
 

Similar to Open save file vb6 program (20)

Putting dialog boxes to work
Putting dialog boxes to workPutting dialog boxes to work
Putting dialog boxes to work
 
Batch FIles
Batch FIlesBatch FIles
Batch FIles
 
VB Dot net
VB Dot net VB Dot net
VB Dot net
 
Visual studio.net
Visual studio.netVisual studio.net
Visual studio.net
 
I B.Sc CS questions & answers
I B.Sc CS questions & answersI B.Sc CS questions & answers
I B.Sc CS questions & answers
 
FILE TYPE/RUN/ FOLDER OPTION
FILE TYPE/RUN/FOLDER OPTIONFILE TYPE/RUN/FOLDER OPTION
FILE TYPE/RUN/ FOLDER OPTION
 
Html introduction
Html introductionHtml introduction
Html introduction
 
Creating a text editor in delphi, a tutorial
Creating a text editor in delphi, a tutorialCreating a text editor in delphi, a tutorial
Creating a text editor in delphi, a tutorial
 
Chapter 01
Chapter 01Chapter 01
Chapter 01
 
windows and its components
windows and its componentswindows and its components
windows and its components
 
LECTURE 12 WINDOWS FORMS PART 2.pptx
LECTURE 12 WINDOWS FORMS PART 2.pptxLECTURE 12 WINDOWS FORMS PART 2.pptx
LECTURE 12 WINDOWS FORMS PART 2.pptx
 
Lesson introduction
Lesson introductionLesson introduction
Lesson introduction
 
TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
 
Qtp launch
Qtp launchQtp launch
Qtp launch
 
The Basics of Visual Studio Code.pdf
The Basics of Visual Studio Code.pdfThe Basics of Visual Studio Code.pdf
The Basics of Visual Studio Code.pdf
 
Microsoft Word Basics2.pptx
Microsoft Word Basics2.pptxMicrosoft Word Basics2.pptx
Microsoft Word Basics2.pptx
 
Visual c++ 2010_tutorial
Visual c++ 2010_tutorialVisual c++ 2010_tutorial
Visual c++ 2010_tutorial
 
HTML
HTMLHTML
HTML
 
Prep 2-booklet-2nd-term-2016-2017
Prep 2-booklet-2nd-term-2016-2017Prep 2-booklet-2nd-term-2016-2017
Prep 2-booklet-2nd-term-2016-2017
 

Recently uploaded

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 

Recently uploaded (20)

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 

Open save file vb6 program

  • 1.
  • 3. 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 Documentssample.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 Documentssample.html" For Output As # 2
  • 4. Sample Program : Creating a text file Private Sub cmdCreate_Click() Dim intMsg As String Dim StudentName As String Open "c:My Documentssample.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
  • 5. Reading a file To read a file created, 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. Sample Program: Reading file Private Sub cmdReading_Click() Dim variable1 As String Open "c:My Documentssample.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.
  • 6. Common Dialog Control The common dialog control provides a standard set of dialog boxes for operations such as opening and saving files, setting print options, and selecting colors and fonts. The control also has the ability to display Help by running the Windows Help engine. Figure 7.11 The common dialog control The common dialog control provides an interface between Visual Basic and the procedures in the Microsoft Windows dynamic-link library Commdlg.dll. To create a dialog box using this control, Commdlg.dll must be in your Microsoft Windows System directory. You use the common dialog control in your application by adding it to a form and setting its properties. The dialog displayed by the control is determined by the methods of the control. At run time, a dialog box is displayed or the Help engine is executed when the appropriate method is invoked; at design time, the common dialog control is displayed as an icon on a form. This icon can't be sized.
  • 7. The common dialog control allows you to display these commonly used dialog boxes:  Open  Save As  Color  Font  Print To use the common dialog control 1. If you haven't already done so, add the common dialog control to the toolbox by selecting Components from the Project menu. Locate and select the control in the Controls tabbed dialog, then click the OK button. 2. On the toolbox, click the CommonDialog control and draw it on a form. When you draw a common dialog control on a form, it automatically resizes itself. Like the timer control, the common dialog control is invisible at run time. 3. At run time, use the appropriate method, as listed in the following table, to display the desired dialog.
  • 8. Method Dialog ShowOpen Open ShowSave Save As ShowColor Color ShowFont Font ShowPrinter Print ShowHelp Invokes Windows
  • 9. 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 cmdOpen_Click() CommonDialog1.Filter = "Text files{*.txt)|*.txt" CommonDialog1.ShowOpen If CommonDialog1.FileName <> "" Then Form1.Caption = CommonDialog1.FileName Open CommonDialog1.FileName For Input As #1 Do Input #1, linetext Text1.Text = Text1.Text & linetext Loop Until EOF(1) End If Close #1 End Sub
  • 10. Private Sub cmdSave_Click() CommonDialog1.Filter = "Text files{*.txt)|*.txt" CommonDialog1.ShowSave If CommonDialog1.FileName <> "" Then Open CommonDialog1.FileName For Output As #1 Print #1, Text1.Text Close #1 End If Form1.Caption = "Saved to " & CommonDialog1.FileName 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 statementCommonDialog1.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 shown below: