In order to save developers who are writing applications for Windows from having to create certain dialog boxes from scratch, Windows provides a set of common dialog boxes. Among them are the File dialog box, which you use to open, save, or browse for files on your computer or computer network; the Browse For Folder dialog box, which you use to specify a folder name; and the Color dialog box, which allows you to choose a specific color.
In the wizard you're creating, you'll take advantage of the File and Browse For Folder dialog boxes. The code provided in the practice files allows you to use these dialog boxes without knowing too much about the code itself, which uses more advanced Windows-based programming techniques. You can learn more about the specifics of this code and Windows programming using the Visual Basic programming language by referring to books on the Visual Basic and Windows application programming interfaces (APIs).
The file modFlDlg.bas contains code that allows you to display the Windows system File dialog box, which is the same dialog box as the Import File dialog box you just used. The name of the imported standard module is modFileDialog, and it also contains a user-defined type that you can use to set properties such as the caption in the title bar of the File dialog box.
Private Sub cmdBrowse_Click() Dim sFileName As String Dim udtFileDialog As FileDialog With udtFileDialog .CustomFilter = "Access Database (*.mdb)" & - Chr$(0) & "*.mdb" & Chr$(0) & Chr$(0) .DefaultExt = "*.mdb" .Title = "Browse" sFileName = modFileDialog - .WinFileDialog(udtFileDialog, 1) End With If Len(sFileName) > 0 Then txtDataSource.Text = sFileName End If End Sub |
The variable udtFileDialog is declared as the user-defined type FileDialog, which is declared as public in the modFileDialog module. You use the user-defined type to set the filters that are displayed in the Files Of Type drop-down list in the File dialog box.
The default extension is then set to the filter extension that the Files Of Type drop-down list will display first. When you click the cmdBrowse control, you use the Access Database type filter. The title bar caption of the File dialog box is set to the text "Browse."
When you display the dialog box, the folder you'll see by default is the most recently used folder. The section File Management in Chapter 4 describes how to set which folder is initially displayed in this dialog.
The value of sFileName is then set to the value returned by the function WinFileDialog, which displays the File dialog box. The If…Then condition block determines whether you clicked Cancel in the File dialog box or selected a valid file and clicked Open. If you selected a valid file, the program enters its full path in the txtDataSource text box. If you clicked Cancel, the value of sFileName is an empty string.
.CustomFilter = "Document Template (*.dot)" & - Chr$(0) & "*.dot" & Chr$(0) & Chr$(0) .DefaultExt = "*.dot" |
When you click the cmdBrowseTemplate control, the filters used are of the Document Template type.
txtTemplate.Text = sFileName |
If a valid file was selected, its full path is entered in the txtDataSource text box.
Private Sub cmdBrowseFolder_Click() Dim sPath As String sPath = modBrowseFolder.BrowseForFolder If Len(sPath) > 0 Then txtDestination.Text = sPath End If End Sub |
The function BrowseForFolder in the modBrowseFolder module displays the Windows system Browse For Folder dialog box shown below. (Your Browse For Folder dialog box will look different.)
The file modBrwse.bas contains code that allows you to display the Windows system Browse For Folder dialog box. The name of the imported standard module is modBrowseFolder.
View Object