File Handling.

Slides:



Advertisements
Similar presentations
Microsoft Expression Web-Illustrated Unit J: Creating Forms.
Advertisements

Introduction to Visual Basic.NET Uploaded By: M.Sheraz anjum.
Chapter 1: An Introduction to Visual Basic 2012
Tutorial 12: Enhancing Excel with Visual Basic for Applications
The Web Warrior Guide to Web Design Technologies
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
CA 121 Intro to Programming Tariq Aziz and Kevin Jones GUI Programming in Visual Studio.NET Chapter 1 Tariq Aziz and Kevin Jones.
CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.
Using the Visual Basic Editor Visual Basic for Applications 1.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Chapter 31 Visual Basic Controls A Form is a windows-style screen displayed by Visual Basic programs. In a form, a programmer can create objects in a form.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Introduction to Visual Basic. What is Visual Basic? An environment for developing Windows applications Components –A GUI (Graphical User Interface - gooey)
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
The Common Dialog Box. Installing the Common Dialog Box May NOT be your standard VB toolbox.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Introduction to Visual Basic.NET Chapter 2 Introduction to Controls, Events.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Visual Basic.NET BASICS Lesson 5 Exponentiation, Order of Operations, and Error Handling.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 MIS309 Database Systems Introduction to Microsoft Access.
Vocabulary in VB So Far. Assignment: Used to change the value of an object at run time Used to change the value of an object at run time.
Common Dialogs.  What is a Common Dialog/Why use them?  Open Dialog  Save File As Dialog  Color Dialog  Font Dialog  Print Dialog.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET Part I VB.NET Controls VB.NET Events.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
VAT Calculator program Controls Properties Code Results.
Microsoft ® Outlook 2000 Integrating Outlook with Office Applications.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
1.Begin by opening VB 2.Click New Project or the icon Hello World Tutorial NOTE: depending on your version of VB – the images might be slightly different.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Lecture 6 – Working with VBA Sub Procedures Dr Joanna Wyrobek 1.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 1: An Introduction to Visual Basic .NET
Excel Tutorial 8 Developing an Excel Application
Chapter 2: The Visual Studio .NET Development Environment
Chapter 1: An Introduction to Visual Basic 2015
Chapter Topics 15.1 Graphical User Interfaces
3.01 Apply Controls Associated With Visual Studio Form
3.01 Apply Controls Associated With Visual Studio Form
Chapter 1: An Introduction to Visual Basic 2015
Using Procedures and Exception Handling
Chapter 3 Fundamentals of Programming in Visual Basic 3
Variables and Arithmetic Operations
Microsoft Office Access 2003
MODULE 7 Microsoft Access 2010
Microsoft Office Access 2003
Chapter 15: GUI Applications & Event-Driven Programming
Introduction to Programming
Presentation transcript:

File Handling

Data Files Programs that can only get data from the user interface are limited. data must be entered each time. only small amounts of data can be processed. Both limitations can be circumvented by storing data in text files.

Data Files A text file is one that contains only ASCII characters. Operations that can be performed on files: Read Write Append

Microsoft Common Dialog Controls Files as Objects Dealing with files will require specifying their names and paths. To facilitate this the standard Windows Dialog boxes can be used. But VB6 doesn’t automatically have access to these, so before we can deal with files we need to add some functionality. We need to add the Microsoft Common Dialog Controls

Adding Components Click on Project Components…

Adding Controls Scroll down to Microsoft Common Dialog Control 6.0 Check it Click OK

Including Dialog Controls Observe that the icon representing the control for creating the standard Microsoft Windows dialog windows has been added to the toolbox.

Including Dialog Contols Like any other control, The Common Dialog contol must be added to the interface for a project to use it. Simply click the icon in the toolbox And click on the form. The control cannot be resized because it does not appear in the interface.

Common Dialog Control The Common Dialog Control allows VB6 projects to use a set of dialog boxes that are common in Windows applications. The methods in the Common Dialog Control include routines to perform various tasks: Change a font Change colours Open a file Print Provide help Save a file

Dialog Control Properties Like all controls, the Common Dialog has properties: Filter –specifies file types shown InitDir – specifies the initial directory CancelError – determines how clicking “Cancel” will be treated Flags – sets various conditions for the dialog

Dialog Control Properties For a Dialog Control called Test, a program could set properties at runtime like these… cdlTest.Filter = "Text files|*.txt" cdlTest.Flags = cdlOFNFileMustExist cdlTest.InitDir = "C:\My Music"

Common Dialog Control The Dialog Control provides access to the common dialogs, but using these dialogs requires further tools. To read a text file, for example, new classes are needed: FileSystemObject TextStream These classes have properties and methods.

TextStream Methods Close Read ReadAll ReadLine Skip SkipLine Write WriteBlankLines WriteLine

Incorporating New References Adding a new collection of classes simply makes them available. We also need to create References to them.

Incorporating New References Click on Project, then References… This dialog appears.

Incorporating New References Scroll down and Select Microsoft Scripting Runtime

Dealing with Files This gives us the tools, now we need to write the code to use them.

Reading a File To read data from a file: create a reference to an object of the FileSystemObject class Dim aFile As FileSystemObject create a reference to an object of the TextStream class Dim FileContent As TextStream

Reading a File use the common dialog control to obtain the file name cdlTest.ShowOpen create a new FileSystemObject object Set aFile = New FileSystemObject use the OpenTextFile method on the FileSystemObject to obtain a TextStream object Set FileContent = aFile.OpenTextFile(cdlTest.FileName)

Reading a File use the ReadAll method on the TextStream object to obtain the file contents as a string txtFileContent.Text = FileContent.ReadAll close the file FileContent.Close

Reading a File Private Sub cmdOpenFile_Click() Dim aFile As FileSystemObject Dim FileContent As TextStream ' cdlTest.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" cdlTest.Flags = cdlOFNFileMustExist cdlTest.ShowOpen Set aFile = New FileSystemObject Set FileContent = aFile.OpenTextFile(cdlTest.FileName) txtFileContent.Text = FileContent.ReadAll FileContent.Close Exit Sub

Error Handling It’s always best to handle errors with program code rather than system messages (and aborted programs). VB6 provides an easy way to deal with errors. Private Sub ReadAFile(…) ‘branch to error handler if error occurs OnError GoTo ErrorHandler ‘regular subroutine Exit Sub ‘write error handling code ErrorHandler: ‘code to deal with errors End Sub

A Complete Example Private Sub cmdOpenFile_Click() On Error GoTo ErrorHandler Dim aFile As FileSystemObject Dim FileContent As TextStream cdlTest.ShowOpen Set aFile = New FileSystemObject Set FileContent = aFile.OpenTextFile(cdlTest.FileName) txtFileContent.Text = FileContent.ReadAll FileContent.Close Exit Sub ErrorHandler: 'user pressed Cancel - do nothing Dim messg As String messg = Str(Err.Number) & ": " & Err.Description MsgBox (messg) End Sub