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


The SaveSetting subroutine saves a value in the Registry. SaveSetting takes as parameters the name of the application, the section within the application where the value should be stored, the name of the data item, and the item’s value. The following code saves the value C:\8_20_97.exp for the File1 item in the RecentFiles section of the ExpenseReporter application:

SaveSetting “ExpenseReporter”, “RecentFiles”, “File1”, _
    “C:\8_20_97.exp”

Experienced Registry users can use the REGEDIT program that comes with Windows 95 and Windows NT to examine and modify the Registry. The entry created by the previous code would be located in HKEY_CURRENT_USER\Software\VB and VBA Program Settings\ExpenseReporter\RecentFiles\File1.

Users who are not familiar with the Registry should not fool around with REGEDIT. Looking at entries can do no harm, but modifying entries can seriously damage the operating system. A system with a badly corrupted Registry may be unable to boot.

ExpenseReporter uses SaveSetting to save the names of the four most recently used files in the Registry. It saves the files every time one of the files changes rather than before exiting so the list will be up to date even if the application crashes or the computer halts unexpectedly.

Sub SaveRecentFiles()
Dim i As Integer
Dim key As String

    For i = 1 To 4
        key = “File” & Format$(i)
        SaveSetting APPNAME, “RecentFiles”, key, RecentFile(i)
    Next i
End Sub

LoadRecentFiles

When the application starts, it must reload the recent file list saved to the Registry by subroutine SaveRecentFiles. The GetSetting function returns the value of an entry in the Registry. This routine takes as parameters the name of the application, the section within the application where the value is stored, the name of the data item, and a default value to return if the value is not found.

ExpenseReporter uses the LoadRecentFiles subroutine to retrieve the recent file list when it first starts. As it reads the files it arranges them so that nonblank entries come first in the list. After it has loaded the list, it calls subroutine UpdateRecentFileMenu to fill in the recent file entries in the File menu.

Sub LoadRecentFiles()
Dim i As Integer
Dim pos As Integer
Dim key As String

    pos = 1
    For i = 1 To 4
        key = “File” & Format$(i)
        RecentFile(pos) = GetSetting(APPNAME, “RecentFiles”, key, “”)
        If RecentFile(pos) <> “” Then pos = pos + 1
    Next i

    ‘ Blank any that were not loaded.
    For i = pos To 4
        RecentFile(i) = “”
    Next i

    ‘ Update the File menu’s list.
    UpdateRecentFileMenu
End Sub

AddRecentFile

ExpenseReporter uses subroutine AddRecentFile to add a file name to the recent file list when the file is opened or created. If the name is already in the list, this routine just moves the name to the first position. If the name is not already in the list, it puts it in the number one position and bumps the other names down one spot, discarding the last name in the list. After it has rearranged the list, AddRecentFile calls subroutines UpdateRecentFileMenu and SaveRecentFiles to update the File menu and save the new file list in the Registry.

Sub AddRecentFile(ByVal fname As String)
Dim i As Integer
Dim j As Integer

    ‘ Don’t save empty file names.
    If fname = “” Then Exit Sub
   
    ‘ See if the file is already in the list.
    For i = 1 To 3
        If fname = RecentFile(i) Then Exit For
    Next i
   
    ‘ Move the other files down one position.
    For j = i To 2 Step -1
        RecentFile(j) = RecentFile(j - 1)
    Next j
   
    ‘ Add this file to the top.
    RecentFile(1) = fname
   
    ‘ Update the File menu’s list.
    UpdateRecentFileMenu

    ‘ Update the Registry.
    SaveRecentFiles
End Sub

RemoveRecentFile

Subroutine RemoveRecentFile takes a file name out of the recent file list, moving the other files up one position so the nonblank entries are at the top of the list. ExpenseReporter invokes RemoveRecentFile if it tries to open a file and fails.

For example, suppose the user selects the first recent file from the File menu but that file has been deleted. The program will fail to open the file. Because the program will be unable to open the file in the future, it removes the file from the list so that the user will not try to open it again later.

Sub RemoveRecentFile(Index As Integer)
Dim i As Integer

    For i = Index To 3
        RecentFile(i) = RecentFile(i + 1)
    Next i
    RecentFile(4) = “”
       
    ‘ Update the File menu’s list.
    UpdateRecentFileMenu

    ‘ Update the Registry.
    SaveRecentFiles
End Sub

Summary

This chapter explains fundamental programming topics that are in some sense localized within the application. Creating scrolling regions, loading data files, and building recent file lists do not require complicated interactions among different parts of the application.

Chapter 2 covers topics that are harder to localize. For example, form validations may require an application to compare the values in many of its text fields. Printing, help, and about dialogs relate more to the external system than to the application itself. Chapter 2 completes the discussion of the ExpenseReporter by discussing these more global issues.


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.