home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


FileInput also invokes the SetFileName subroutine. This simple routine saves the file name in the variable FileName for later use. It then resets the main form’s caption so the user can see the name of the file in the form’s banner. Finally, it invokes the AddRecentFile subroutine to add the file name to the recent file list. The recent file list is described later.

FileInput then invokes the ComputeTotals subroutine. This routine updates the expense report’s total and amount due fields.

Const FILE_IDENTIFIER = “ExpenseReporter 1.0 data file”

Function FileWrite(fname As String) As Boolean
Dim fnum As Integer
Dim i As Integer
Dim num_rows As Integer

    ‘ Open the file.
    On Error GoTo WriteOpenError
    fnum = FreeFile()
    Open fname For Output As #fnum
   
    ‘ Write the data into the file.
    On Error GoTo WriteError
   
    Write #fnum, FILE_IDENTIFIER
   
    Write #fnum, NameText.Text, IDText.Text, _
        DeptText.Text, ProjText.Text, _
        FromDateText.Text, ToDateText.Text, _
        NotesText.Text, PrepaidText.Text, _
        AdvanceText.Text
   
    ‘ See how many rows are nonblank.
    num_rows = 0
    For i = 0 To MaxRow
        If RowNotBlank(i) Then _
            num_rows = num_rows + 1
    Next i
   
    ‘ Save the number of rows.
    Write #fnum, num_rows
   
    ‘ Save the nonblank rows.
    For i = 0 To MaxRow
        If RowNotBlank(i) Then
            Write #fnum, DateText(i).Text, _
                LocationText(i).Text, _
                CategoryCombo(i).ListIndex, _
                DescrCombo(i).Text, _
                AmountText(i).Text
        End If  ‘ End if the row is nonblank.
    Next i  ‘ End saving the nonblank rows.
   
    ‘ Close the file.
    Close #fnum
   
    ‘ Save the file name.
    SetFileName fname
   
    ‘ The data is now safe.
    DataModified = False
   
    FileWrite = True
    Exit Function
   
WriteOpenError:
    Beep
    MsgBox “Error opening file ” & fname & “.”, _
        vbOKOnly + vbExclamation, _
        “File Open Error”
    FileWrite = False
    Exit Function

WriteError:
    Beep
    MsgBox “Error writing to file ” & fname & “.”, _
        vbOKOnly + vbExclamation, _
        “File Write Error”
    Close #fnum
    FileWrite = False
    Exit Function
End Function

Function FileInput(fname As String) As Boolean
Dim fnum As Integer

Dim txt As String
Dim name_text As String
Dim id_text As String
Dim dept_text As String
Dim proj_text As String
Dim from_text As String
Dim to_text As String
Dim remarks_text As String
Dim prepaid_text As String
Dim advance_text As String
Dim date_text As String
Dim location_text As String
Dim category_num As Integer
Dim description_text As String
Dim amount_text As String
Dim num_rows As Integer
Dim i As Integer

    ‘ Open the file.
    On Error GoTo InputOpenError
    fnum = FreeFile()
    Open fname For Input As #fnum
   
    ‘ Read the data from the file.
    On Error GoTo InputError
   
    ‘ Read the file identifier string.
    Input #fnum, txt
    If txt <> FILE_IDENTIFIER Then
        Beep
        MsgBox “Unrecognized file format.”, _
            vbOKOnly + vbExclamation, _
            “File Input Error”
        Close #fnum
        FileInput = True
        IgnoreModify = False
        Exit Function
    End If

    Input #fnum, name_text, id_text, dept_text, _
        proj_text, from_text, to_text, _
        remarks_text, prepaid_text, advance_text

    IgnoreModify = True
    NameText.Text = name_text
    IDText.Text = id_text
    DeptText.Text = dept_text
    ProjText.Text = proj_text
    FromDateText.Text = from_text
    ToDateText.Text = to_text
    NotesText.Text = remarks_text
    PrepaidText.Text = prepaid_text
    AdvanceText.Text = advance_text
   
    ‘ Create any extra rows that are needed.
    Input #fnum, num_rows
    For i = MaxRow + 1 To num_rows
        MakeRow i
    Next i
   
    ‘ Blank any rows that were not just created.
    For i = 0 To MaxRow
        ClearRow i
    Next i
    If MaxRow < num_rows Then MaxRow = num_rows

    ‘ Read the rows.
    For i = 0 To num_rows - 1
        Input #fnum, date_text, location_text, _
            category_num, description_text, _
            amount_text
        DateText(i).Text = date_text
        LocationText(i).Text = location_text
        AmountText(i).Text = amount_text
        CategoryCombo(i).ListIndex = category_num
        DescrCombo(i).Text = description_text
    Next i
   
    ‘ Sort the rows by date.
    SortRows
   
    IgnoreModify = False
   
    ‘ Close the file.
    Close #fnum
   
    ‘ Save the new file name.
    SetFileName fname
   
    ‘ Display the totals.
    ComputeTotals
   
    ‘ The data is safe for now.
    DataModified = False
    FileInput = False
    Exit Function
   
InputOpenError:
    Beep
    MsgBox “Error opening file ” & fname & “.”, _
        vbOKOnly + vbExclamation, _
        “File Open Error”
    FileInput = True
    IgnoreModify = False
    Exit Function

InputError:
    Beep
    MsgBox “Error reading data from file ” & fname & “.”, _
        vbOKOnly + vbExclamation, _
        “File Input Error”
    Close #fnum
    FileInput = True
    IgnoreModify = False
    Exit Function
End Function

Sub SetFileName(Name As String)
    FileName = Name
    Caption = “ExpenseReporter [” & FileName & “]”
    AddRecentFile Name
End Sub

SaveDataAs

The FileInput and FileWrite functions perform the actual job of reading and writing data. Other routines use those functions to implement the file management features that the user sees.

The SaveDataAs function allows the user to save the document into a new file. This function is invoked by the File menu’s Save As command.

SaveDataAs begins by preparing the application’s common dialog box FileDialog. It sets the dialog’s flags that are appropriate when the user is selecting a file for saving. ExpenseReporter uses the flags listed in Table 1.2.

SaveDataAs then sets the initial file name to *.exp so the dialog will display files with a .exp extension. The function sets the dialog’s CancelError property to true so the dialog will generate an error if the user presses the Cancel button. It uses the ON ERROR RESUME NEXT statement to catch the error.

SaveDataAs then presents the common dialog box so the user can select the file that should contain the data. When the dialog is finished, the function checks to see if an error was generated, indicating that the user pressed the Cancel button.

If the user did not cancel, the function sets the dialog’s InitDir property to hold the directory that contains the file selected by the user. This will make the dialog begin the file selection process with that directory the next time the user selects a file.

There are a couple of other strategies for specifying the initial directory for file selection dialogs. Many applications never set the InitDir property. This makes the dialog begin in the application’s default directory every time the user needs to select a file. This approach can be quite annoying. Generally a user will work with several files in the same directory, and forcing the user to relocate that directory each time the dialog appears is a waste of the user’s time.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.