![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Some applications use separate directories for opening and saving files. This method is convenient if the user is likely to load files in one directory and save them in another. This particularly makes sense if the application supports many different file formats. When loading several files in one format and saving them in a different format, the user may want to load them all from one directory and save them in another. Other applications keep track of different directories for files with different extensions. For example, CorelDRAW! keeps track of the last directories used for files with extensions .BMP, .CDR, .TIF, and many others. This is useful if the user keeps files of different types in different directories, something the user is likely to do when working with many file types.
CorelDRAW! also remembers where these directories are each time the program starts. ExpenseReporter does not save the file search directory each time it selects a file, but it could easily support this feature using the Windows Registry, described later in this chapter. After the user has selected a file, SaveDataAs invokes the FileWrite function to save the data.
Function SaveDataAs() As Boolean
Prepare the file common dialog.
FileDialog.Flags = cdlOFNOverwritePrompt + _
cdlOFNHideReadOnly + cdlOFNExplorer + _
cdlOFNLongNames
FileDialog.FileName = *.exp
FileDialog.CancelError = True
Present the dialog.
On Error Resume Next
FileDialog.ShowSave
If the user canceled, were done.
If Err.Number = cdlCancel Then Exit Function
If there is some other error, say so.
If Err.Number > 0 Then
Beep
MsgBox Error %d reading file name. & _
Err.Description, _
vbOKOnly + vbExclamation, Error
Exit Function
End If
Resume normal error handling.
On Error GoTo 0
Save the directory for next time.
FileDialog.InitDir = _
Left$(FileDialog.FileName, _
Len(FileDialog.FileName) - _
Len(FileDialog.FileTitle))
Save data to the indicated file.
SaveDataAs = FileWrite(FileDialog.FileName)
End Function
SaveDataThe SaveData function is much simpler than SaveDataAs. The SaveData function allows the user to save a document into the same file from which it was loaded. The application calls SaveData when the user selects the Save command from the File menu. If the form variable FileName is blank, the document is new and does not yet have a file name. In that case, SaveData invokes function SaveDataAs so the user can decide where to save the new document. Otherwise, if FileName is not blank, SaveData uses the FileWrite function to save the document into the file FileName.
Function SaveData() As Boolean
If there is no file name, treat this as if
the user had selected Save As...
If FileName = Then
SaveData = SaveDataAs()
Else
SaveData = FileWrite(FileName)
End If
End Function
Some applications disable the File menus Save command whenever there is no known file name for the document. When the user creates a new document, the application blanks the FileName variable and disables the Save command. When the user saves the data or loads an existing document, the application saves the files name in FileName and enables the Save command. Most Windows applications, including ExpenseReporter, do not bother to disable the Save command. They simply treat it the same way they treat the Save As command if the document does not yet have a file name. OpenThe File menus Open command opens an existing document saved on disk. It begins by using the DataSafe routine to decide if it is safe to close the current document. If so, it uses a common dialog to allow the user to select a file, much as the Save As command does. The dialog flags used for opening a file are slightly different from those used for saving data to a file. Because the application is not saving data, it does not need to worry about overwriting existing files so the cdlOFNOverwritePrompt is not necessary. Because the user wants to open an existing file, the flags include cdlOFNFileMustExist. This means the dialog does not allow the user to enter the name of a file that does not already exist. The flags used by ExpenseReporter to open a file are listed in Table 1.3. Once the user has selected a file, the application uses the FileInput function to load the data. The following code shows the event handler that is triggered when the user selects the Open command from ExpenseReporters File menu.
Private Sub mnuFileOpen_Click()
Dim status As Boolean
See if the data is safe.
If Not DataSafe(Do you want to save the changes?) _
Then Exit Sub
Prepare the file common dialog.
FileDialog.Flags = cdlOFNFileMustExist + _
cdlOFNHideReadOnly + cdlOFNExplorer + _
cdlOFNLongNames
FileDialog.FileName = *.exp
FileDialog.CancelError = True
Present the dialog.
On Error Resume Next
FileDialog.ShowOpen
If the user canceled, were done.
If Err.Number = cdlCancel Then Exit Sub
If there is some other error, say so.
If Err.Number > 0 Then
Beep
MsgBox Error %d reading file name. & _
Err.Description, _
vbOKOnly + vbExclamation, Error
Exit Sub
End If
Start waiting.
WaitStart
On Error GoTo FileOpenError
Save the directory for next time.
FileDialog.InitDir = _
Left$(FileDialog.FileName, _
Len(FileDialog.FileName) - _
Len(FileDialog.FileTitle))
Load the indicated file.
status = FileInput(FileDialog.FileName)
FileOpenError:
Do not leave the cursor as an hourglass.
WaitEnd
End Sub
|
||||||||||||||||||||||
|
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.
|