• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 20. Conquering Big Problems that... > Testing Quotes and Pounds

Testing Quotes and Pounds

The moment of truth comes when you apply what you have learned. You can test variables and expressions in a standard module using the immediate window before you insert them into a class module. If you want to test variables in event procedures in class modules, you set breakpoints so that when you run the form or report, you have a chance to examine your code from where the breakpoints are set in the procedure. In contrast, in the standard module, you can test the variables by either using Print.Debug statements to view the expressions in the immediate window or by setting breakpoints and watches to view them. Either way, you can view the expressions without ever opening the form or report to which you eventually copy the expressions. The following steps walk you through the testing process:

1.
Open the Claims database from the AccessByExample folder.

2.
Open the Loss Report report in Design view.

3.
Right-click the title bar of the report, and then click Properties to open the Property sheet of the form.

4.
Click the Event tab; then click the drop-down box of the OnOpen event property, and choose [Event Procedure]. Click the Build button.

5.
Once in the Visual Basic Editor Module Window, click Insert, Module. If the immediate window is not open, press Ctrl+G.

6.
Type the following code in the Module window:

Sub Test() 
Dim strState As String, strFilter As String 

strState = "FL" 
strFilter = "[AccidentState]=""" & strState & """" 
Debug.Print strFilter 

End Sub 

7.
Click Debug, Compile Claims to check for typos.

8.
Click the Run Sub/User Form button. Figure 20.1 shows the following results that appear in the immediate window:

[AccidentState]="FL" 

Figure 20.1. Expressions can be tested in the immediate window before pasting them into another module.


9.
Change the line that begins with strFilter to the following:

strFilter = "[AccidentState]=" & Chr$(34) & strState & Chr$(34) 

10.
Click the Run Sub/User Form button again. The results should be the same.

11.
With your mouse, highlight everything between Sub Test() and Debug. Print, as shown in Figure 20.2. Copy the lines by pressing Ctrl+C.

Figure 20.2. Copy and paste the code from a standard module to a class module.


12.
In the Project window on the left, click the plus sign (+) next to Microsoft Access Class Objects. Double-click Report_Loss Report to open the event procedure tied to the OnOpen event begun in step 4.

13.
Paste the copied lines in the procedure on the line immediately following Private Sub Report_Open(Cancel As Integer) by pressing Ctrl+V.

14.
Type Me.Filter = strFilter on the line immediately following the pasted lines. Next, type Me.FilterOn = True on the next line to look like the following:

Private Sub Report_Open(Cancel As Integer) 
Dim strState As String, strFilter As String 

strState = "FL" 
strFilter = "[AccidentState]=" & Chr$(34) & strState & Chr$(34) 
Me.Filter = strFilter 
Me.FilterOn = True 
End Sub 

15.
Click Debug Compile Claims to check for errors; then press Alt+F11 to return to Design view.

16.
Click the Print Preview button to view the report. Only Florida records appear.

CAUTION

If you get a parameter box for the state field, it is misspelled in the procedure. For example, if you spelled the AccidentState field AcidentState, a parameter box with the incorrect spelling would appear.

17.
Close Print Preview. Press Alt+F11 to return to the procedure attached to the OnOpen event property in the Module window.

18.
Delete the code between the Sub and End Sub lines, and insert the following code:

Dim strBDate As String, strEDate As String 
Dim strFilter As String 

strBDate = "1/1/96" 
strEDate = "1/1/98" 
    strFilter = BuildCriteria("[AccidentDate]", _ 
    adBSTR, "between " & strBDate & " and " & strEDate) 
Me.Filter = strFilter 
Me.FilterOn = True 

19.
Press Alt+F11 to return to Design view. Click Print Preview to view the records between 1/1/96 and 1/1/98.

20.
While in Print Preview, click the drop-down box of the Zoom button, and choose 75% to view the 75% Preview window, as shown in Figure 20.3.

Figure 20.3. You can view the filtered records in Print Preview to confirm that the filter worked.


21.
Close Print Preview; then close and save the report, but don’t save the Standard module, which was just for testing and practice.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint