The basic steps for performing file IO in VB are:
' Open file for writing
Open "filename" For Output As #1
' Write something to this file
Write #1, "some text"
' Close this file
Close #1
Notice that the file is given an ID by which it is referred to after it is opened. It is important that each open file have a unique ID.
In this task, we will add code for saving a file. This involves writing our own procedures, using the common dialog control and file IO.
Option Explicit ' Global variables Dim hasFileName As Boolean Dim currentFileName As String Dim isModified As BooleanThis will add three global variables that keep track of whether or not the file has been saved or modified and what its name is. These are required to allow our program to decide between saving automatically with the current name or asking the user for a name. We already know what Option Explicit will do.
Private Sub Form_Load()
' This is the point at which we should initialise our globals
hasFileName = False
currentFileName = ""
isModified = False
End Sub
Private Sub mnuFileNew_Click()
txtDocument.Text = ""
' Set globals
hasFileName = False
currentFileName = ""
isModified = False
End Sub
Private Sub txtDocument_Change()
' When we change the text, the isModified global must
' change to reflect the fact that the text has now
' been modified
isModified = True
End Sub
Private Sub save(filename As String)
' Open file for writing
Open filename For Output As #1
' Write the contents of the text field to this file
Write #1, txtDocument.Text
' Close this file
Close #1
' Set globals
hasFileName = True
currentFileName = filename
isModified = False
End Sub
Notice that this is a private sub procedure.
Private Sub mnuFileSaveAs_Click()
' We will need a variable for the file name
Dim filename As String
' User will always want to choose the file name
' So we open the common dialog for saving
codDialog.ShowSave
' Get the filename the user chose
filename = codDialog.filename
If filename = "" Then
' user did not choose a filename
Exit Sub
Else
' Call the save sub routine with this parameter
save filename
End If
End Sub
Private Sub mnuFileSave_Click()
' Whether to save or not depends on the state of the globals
' If we have a name and are modified, we can simply save with
' that name
If hasFileName Then
If isModified Then
save currentFileName
Else
Exit Sub
End If
Else
If isModified Then
' This file has no name so we need to work as if the
' user had pressed "save as"
mnuFileSaveAs_Click
Else
MsgBox "Nothing to save yet"
Exit Sub
End If
End If
End Sub
Notice we have used a number of concepts from previous exercises, including if statements and message boxes.
' Set up error handlers
On Error GoTo error_marker
' In here will go some code that may create an error
' ...
' ...
' this section of code will end this sub routine
exit_marker:
Close #2
Exit Sub
error_marker:
Select Case Err.Number
Case 53 ' File not found error has occurred
MsgBox "That file does not exist"
Case Else
MsgBox "There was an error"
Err.Raise Err
End Select
GoTo exit_marker
' After the error handling, the function will simply end
End Sub
The On Error line specifies where to jump to when an error occurs. Each specific error is given a number. You use the Select statement like a special type of if. The general form of a Select statement is:
Select Case "condition" Case "one posibility" "code" Case "another possibility" "code" Case "another possibility" "code" Case Else "code for if none of the possibilies are true" End SelectYou write code in the select statement to handle all the errors you expect to get. In the else section, write a general error handler to deal with all the other cases.
In this task, we will add code for opening a file, in the process, we will have to do our first bit of error handling.
' We will need a variable for the file name
Dim filename As String
Dim fileContents As String
' User will always want to choose the file name
' So we open the common dialog for opening files
codDialog.ShowOpen
' Get the filename the user chose
filename = codDialog.filename
' Set up error handlers
On Error GoTo open_errors
' Ends procedure if user pressed cancel or did not enter filename
If filename = "" Then
GoTo open_exit
End If
' Open the file with the chosen name
Open filename For Input As #2 ' This file can be read but not written
' Read the contents of the text field to this file
Input #2, fileContents
txtDocument.Text = fileContents
' Set globals
isModified = False
currentFileName = filename
hasFileName = True
' this section of code will end this sub routine
open_exit:
Close #2
Exit Sub
open_errors:
Select Case Err.Number
Case 53 ' File not found error has occurred
MsgBox "That file does not exist"
Case Else
MsgBox "There was an error"
Err.Raise Err
End Select
GoTo open_exit