Microsoft KB Archive/147143: Difference between revisions

From BetaArchive Wiki
m (Text replacement - ">" to ">")
m (Text replacement - """ to """)
Line 71: Line 71:
<li>Open the sample database Northwind.mdb.</li>
<li>Open the sample database Northwind.mdb.</li>
<li>Start the Report Wizard and create a report based on the Customers table.</li>
<li>Start the Report Wizard and create a report based on the Customers table.</li>
<li><p>In the &quot;Which fields do you want on your report&quot; box, select the following fields, and then click Next:<br />
<li><p>In the "Which fields do you want on your report" box, select the following fields, and then click Next:<br />
</p>
</p>
<pre class="fixed_text">      CompanyName
<pre class="fixed_text">      CompanyName
Line 79: Line 79:
       Country
       Country
                         </pre></li>
                         </pre></li>
<li>In the &quot;Do you want to add any grouping levels?&quot; box, click Next.</li>
<li>In the "Do you want to add any grouping levels?" box, click Next.</li>
<li>In the &quot;What sort order do you want for your records?&quot; box, click Next.</li>
<li>In the "What sort order do you want for your records?" box, click Next.</li>
<li>In the &quot;How would you like to layout your report?&quot; box, click Tabular, and then click Next.</li>
<li>In the "How would you like to layout your report?" box, click Tabular, and then click Next.</li>
<li>In the &quot;What style would you like?&quot; box, click Next.</li>
<li>In the "What style would you like?" box, click Next.</li>
<li>In the &quot;What title do you want?&quot; box, enter <span class="kbd userinput"> rptCustomers</span>. Click &quot;Modify the report's design,&quot; and then click Finish.</li>
<li>In the "What title do you want?" box, enter <span class="kbd userinput"> rptCustomers</span>. Click "Modify the report's design," and then click Finish.</li>
<li>In Design view, display the report's property sheet and set the FilterOn property to Yes.</li>
<li>In Design view, display the report's property sheet and set the FilterOn property to Yes.</li>
<li>Save and close the report.</li></ol>
<li>Save and close the report.</li></ol>
Line 102: Line 102:
       BorderStyle: Thin
       BorderStyle: Thin
       MinMaxButtons: None
       MinMaxButtons: None
       Width: 2.5&quot;
       Width: 2.5"
                         </pre></li>
                         </pre></li>
<li><p>Set the form's OnOpen property to the following event procedure:<br />
<li><p>Set the form's OnOpen property to the following event procedure:<br />
</p>
</p>
<pre class="codesample">      Private Sub Form_Open(Cancel As Integer)
<pre class="codesample">      Private Sub Form_Open(Cancel As Integer)
         DoCmd.OpenReport &quot;rptCustomers&quot;, A_PREVIEW 'Open Customers report.
         DoCmd.OpenReport "rptCustomers", A_PREVIEW 'Open Customers report.
         DoCmd.Maximize  'Maximize the report window.
         DoCmd.Maximize  'Maximize the report window.
       End Sub
       End Sub
Line 114: Line 114:
</p>
</p>
<pre class="codesample">      Private Sub Form_Close()
<pre class="codesample">      Private Sub Form_Close()
         DoCmd.Close acReport, &quot;rptCustomers&quot; 'Close the Customers report.
         DoCmd.Close acReport, "rptCustomers" 'Close the Customers report.
         DoCmd.Restore  'Restore the window size
         DoCmd.Restore  'Restore the window size
       End Sub
       End Sub
Line 127: Line 127:
       RowSource: Select Distinct [CompanyName] from Customers Order _
       RowSource: Select Distinct [CompanyName] from Customers Order _
                 By [CompanyName];
                 By [CompanyName];
       Width: 1.5&quot;
       Width: 1.5"


       Combo Box:
       Combo Box:
Line 134: Line 134:
       RowSource: Select Distinct [ContactName] from Customers Order _
       RowSource: Select Distinct [ContactName] from Customers Order _
                 By [ContactName];
                 By [ContactName];
       Width: 1.5&quot;
       Width: 1.5"


       Combo Box:
       Combo Box:
Line 140: Line 140:
       Tag: City
       Tag: City
       RowSource: Select Distinct [City] from Customers Order By [City];
       RowSource: Select Distinct [City] from Customers Order By [City];
       Width: 1.5&quot;
       Width: 1.5"


       Combo Box:
       Combo Box:
Line 148: Line 148:
                     WHERE(((Customers.Region) Is Not Null)) ORDER BY _
                     WHERE(((Customers.Region) Is Not Null)) ORDER BY _
                     Customers.Region;
                     Customers.Region;
       Width: 1.5&quot;
       Width: 1.5"


       Combo Box:
       Combo Box:
Line 155: Line 155:
       RowSource: Select Distinct [Country] from Customers Order _
       RowSource: Select Distinct [Country] from Customers Order _
                     By [Country];
                     By [Country];
       Width: 1.5&quot;
       Width: 1.5"
                         </pre></li>
                         </pre></li>
<li><p>Add the following command button to your form, which will enable you to reset the values in the form's combo boxes and check boxes:<br />
<li><p>Add the following command button to your form, which will enable you to reset the values in the form's combo boxes and check boxes:<br />
Line 171: Line 171:
         Dim  intCounter as Integer
         Dim  intCounter as Integer
         For intCounter = 1 To 5
         For intCounter = 1 To 5
         Me(&quot;Filter&quot; &amp; intCounter) = &quot;&quot;
         Me("Filter" &amp; intCounter) = ""
         Next
         Next
       End Sub
       End Sub
Line 190: Line 190:
   ' Build SQL String.
   ' Build SQL String.
   For intCounter = 1 To 5
   For intCounter = 1 To 5
       If Me(&quot;Filter&quot; &amp; intCounter) <> &quot;&quot; Then
       If Me("Filter" &amp; intCounter) <> "" Then


         strSQL = strSQL &amp; &quot;[&quot; &amp; Me(&quot;Filter&quot; &amp; intCounter).Tag &amp; &quot;] &quot; _
         strSQL = strSQL &amp; "[" &amp; Me("Filter" &amp; intCounter).Tag &amp; "] " _
           &amp; &quot; = &quot; &amp; Chr(34) &amp; Me(&quot;Filter&quot; &amp; intCounter) &amp; Chr(34) &amp;  _
           &amp; " = " &amp; Chr(34) &amp; Me("Filter" &amp; intCounter) &amp; Chr(34) &amp;  _
           &quot; And  &quot;
           " And  "
           End If
           End If
         Next
         Next


         If strSQL <> &quot;&quot; Then
         If strSQL <> "" Then
         ' Strip Last &quot; And &quot;.
         ' Strip Last " And ".
           strSQL = Left(strSQL, (Len(strSQL) - 5))
           strSQL = Left(strSQL, (Len(strSQL) - 5))
           ' Set the Filter property.
           ' Set the Filter property.
Line 229: Line 229:


# Open the frmFilter pop-up form in Form view. Note that the rptCustomers report opens in Print Preview behind the form and displays all records in the Customers table.
# Open the frmFilter pop-up form in Form view. Note that the rptCustomers report opens in Print Preview behind the form and displays all records in the Customers table.
# In the Region combo box, select &quot;BC,&quot; and then click the Set Filter button. You should see only the records that contain &quot;BC&quot; in the Region field.
# In the Region combo box, select "BC," and then click the Set Filter button. You should see only the records that contain "BC" in the Region field.
# Click Clear to reset the report's Filter property.
# Click Clear to reset the report's Filter property.


Line 239: Line 239:
== REFERENCES ==
== REFERENCES ==


For more information about the Filter property, search the Help Index for &quot;Filter Property,&quot; or ask the Microsoft Access 97 Office Assistant.<br />
For more information about the Filter property, search the Help Index for "Filter Property," or ask the Microsoft Access 97 Office Assistant.<br />
<br />
<br />
For more information about filter by form or filter by selection, search the Help Index for &quot;Filter By Form&quot; or &quot;Filter By Selection,&quot; or ask the Microsoft Access 97 Office Assistant.
For more information about filter by form or filter by selection, search the Help Index for "Filter By Form" or "Filter By Selection," or ask the Microsoft Access 97 Office Assistant.


</div>
</div>

Revision as of 10:08, 21 July 2020

Article ID: 147143

Article Last Modified on 1/19/2007



APPLIES TO

  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition



This article was previously published under Q147143

Moderate: Requires basic macro, coding, and interoperability skills.

SUMMARY

This article describes how to filter a report dynamically in Print Preview by selecting filter criteria from a pop-up form.

NOTE: This article explains a technique demonstrated in the sample files, RptSampl.exe (for Microsoft Access for Windows 95 version 7.0) and RptSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

145777 ACC95: Microsoft Access Sample Reports Available in Download Center

175072 ACC97: Microsoft Access 97 Sample Reports Available in Download Center

MORE INFORMATION

The technique involves creating a pop-up form and a report in the sample database Northwind. The form enables you to choose which fields and values to use for filtering a report in Print Preview.

Creating the Report


  1. Open the sample database Northwind.mdb.
  2. Start the Report Wizard and create a report based on the Customers table.
  3. In the "Which fields do you want on your report" box, select the following fields, and then click Next:

           CompanyName
           ContactName
           City
           Region
           Country
                            
  4. In the "Do you want to add any grouping levels?" box, click Next.
  5. In the "What sort order do you want for your records?" box, click Next.
  6. In the "How would you like to layout your report?" box, click Tabular, and then click Next.
  7. In the "What style would you like?" box, click Next.
  8. In the "What title do you want?" box, enter rptCustomers. Click "Modify the report's design," and then click Finish.
  9. In Design view, display the report's property sheet and set the FilterOn property to Yes.
  10. Save and close the report.

Creating the Pop-up Form


  1. Create a new form not based on any table or query in Design view with the following form properties:

          Form: frmFilter
          ---------------------
          ScrollBars: Neither
          RecordSelectors: No
          NavigationButtons: No
          PopUp: Yes
          BorderStyle: Thin
          MinMaxButtons: None
          Width: 2.5"
                            
  2. Set the form's OnOpen property to the following event procedure:

          Private Sub Form_Open(Cancel As Integer)
             DoCmd.OpenReport "rptCustomers", A_PREVIEW 'Open Customers report.
             DoCmd.Maximize  'Maximize the report window.
          End Sub
                            
  3. Set the form's OnClose property to the following event procedure:

          Private Sub Form_Close()
             DoCmd.Close acReport, "rptCustomers" 'Close the Customers report.
             DoCmd.Restore  'Restore the window size
          End Sub
                            
  4. Add the following five combo boxes in the detail section. Place them on the form vertically, one below each other:

    NOTE: In the SQL expressions below, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this expression.

          Combo box:
          Name: Filter1
          Tag: CompanyName
          RowSource: Select Distinct [CompanyName] from Customers Order _
                     By [CompanyName];
          Width: 1.5"
    
          Combo Box:
          Name: Filter2
          Tag: ContactName
          RowSource: Select Distinct [ContactName] from Customers Order _
                     By [ContactName];
          Width: 1.5"
    
          Combo Box:
          Name: Filter3
          Tag: City
          RowSource: Select Distinct [City] from Customers Order By [City];
          Width: 1.5"
    
          Combo Box:
          Name: Filter4
          Tag: Region
          RowSource: SELECT DISTINCT Customers.Region FROM Customers _
                         WHERE(((Customers.Region) Is Not Null)) ORDER BY _
                         Customers.Region;
          Width: 1.5"
    
          Combo Box:
          Name: Filter5
          Tag: Country
          RowSource: Select Distinct [Country] from Customers Order _
                         By [Country];
          Width: 1.5"
                            
  5. Add the following command button to your form, which will enable you to reset the values in the form's combo boxes and check boxes:

          Command button:
          Name: Clear
          Caption: Clear
          OnClick: [Event procedure]
    
          Set the OnClick [Event procedure] as follows:
                            


          Private Sub Clear_Click()
             Dim  intCounter as Integer
             For intCounter = 1 To 5
             Me("Filter" & intCounter) = ""
             Next
          End Sub
                            
  6. Add a second command button to your form:

          Command Button:
          Name: Set Filter
          Caption: Set Filter
          OnClick: [Event procedure]
    
          Set the OnClick [Event procedure] as follows:
                            


    Private Sub Set_Filter_Click()
       Dim strSQL as String, intCounter as Integer
       ' Build SQL String.
       For intCounter = 1 To 5
          If Me("Filter" & intCounter) <> "" Then
    
             strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] " _
               & " = " & Chr(34) & Me("Filter" & intCounter) & Chr(34) &  _
               " And  "
              End If
            Next
    
            If strSQL <> "" Then
            ' Strip Last " And ".
              strSQL = Left(strSQL, (Len(strSQL) - 5))
              ' Set the Filter property.
              Reports![rptCustomers].Filter = strSQL
              Reports![rptCustomers].FilterOn = True
            End If
    End Sub
                            
  7. Add a third command button to your form:

          Command Button:
          Name: Close
          Caption: Close
          OnClick: [Event procedure]
    
          Set the OnClick [Event procedure] as follows:
                            


          Private Sub Close_Click()
             DoCmd.Close acForm, Me.Form.Name
          End Sub
                            
  8. Close and save the form as frmFilter.

Filtering the Report


  1. Open the frmFilter pop-up form in Form view. Note that the rptCustomers report opens in Print Preview behind the form and displays all records in the Customers table.
  2. In the Region combo box, select "BC," and then click the Set Filter button. You should see only the records that contain "BC" in the Region field.
  3. Click Clear to reset the report's Filter property.

NOTE: To experiment with various combinations of data, you can select an item from one or more combo boxes and click the Set Filter button. When you are ready to select different items, first click the Clear button to reset the report's Filter property before making new choices in the combo boxes.

REFERENCES

For more information about the Filter property, search the Help Index for "Filter Property," or ask the Microsoft Access 97 Office Assistant.

For more information about filter by form or filter by selection, search the Help Index for "Filter By Form" or "Filter By Selection," or ask the Microsoft Access 97 Office Assistant.


Additional query words: dynamic popup

Keywords: kbhowto KB147143