Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to enumerate selected form records in Access 2000


View products that this article applies to.

This article was previously published under Q208502
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

For a Microsoft Access 2002 version of this article, see 294202 (http://support.microsoft.com/kb/294202/EN-US/ ) .

↑ Back to the top


Summary

You can select multiple records in a form using the record selector at the left side of the form. You can also use the SelTop and SelHeight form properties to specify or determine the number of selected records in a continuous or Datasheet view form.

This article shows you how to use the SelTop and SelHeight form properties to enumerate through the list of selected records in a form.

NOTE: SelLeft and SelWidth are form properties that you can use to determine the current columns selected in Datasheet view; however, this article does not discuss these form properties.

↑ Back to the top


More information

The SelTop and SelHeight properties enable you to determine which records are selected in a continuous or Datasheet view form. You can use the SelTop property to determine which row is the first in the selection. You can use the SelHeight property to determine the number of rows in the current selection.

The following information describes how to use these properties to enumerate the list of selected records from one of the following:
  • A macro run from a toolbar button or an AutoKeys macro.
  • Code run from a command button in the header or footer section of the form.
NOTE: The second method presents some challenges and is a bit more difficult to accomplish. Suppose that you want to place a command button in the header or footer section of a continuous form to enable your users to copy, move, or print a report against the selected set of records. When you press this button, the current selection of records disappears when the command button receives focus. Information later in this article shows you how to circumvent this behavior.

Using a Toolbar Button or an AutoKeys Macro to Enumerate Selected Records

The advantage to using a toolbar button or keyboard key to run your code is that the current selection of records will not be lost; this simplifies the procedure:
  1. CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  2. Open the sample database Northwind.mdb and create a new module.
  3. Type or paste the following procedure in the new module:
    Function DisplaySelectedCompanyNames()
       Dim i As Long
       Dim F As Form
       Dim RS As Recordset
    
       ' Get the form and its recordset.
       Set F = Forms![Customers1]
       Set RS = F.RecordsetClone
    
       ' Move to the first record in the recordset.
       RS.MoveFirst
    
       ' Move to the first selected record.
       RS.Move F.SelTop - 1
    
       ' Enumerate the list of selected records presenting
       ' the CompanyName field in a message box.
       For i = 1 To F.SelHeight
         MsgBox RS![CompanyName]
         RS.MoveNext
       Next i
    
    End Function
    
    					
  4. Save the module with a unique name and close the module.
  5. Create a new macro named AutoKeys as follows:
      Macro Name     Action
      ----------------------
      {F5}           RunCode
    
      AutoKeys Actions
      ------------------------------------------------
      RunCode
         Function Name: =DisplaySelectedCompanyNames()
    					
  6. On the Insert menu, click Form to open the New Form dialog box.
  7. Click AutoForm: Tabular in the list of available wizards, select the Customers table in the Choose the table or query where the object's data comes from box, and then click OK.
  8. On the File menu, click Save and save the form with the default name, Customers1.
  9. Right-click the toolbar and click Customize.
  10. In the Customize dialog box, click the Commands tab and under Categories, click All Macros. Under Commands, drag "AutoKeys.{F5}" from the Commands list to an empty space on the toolbar, and then click Close.
  11. Select a record or a set of records on the form by using the record selector buttons on the left side of the continuous form. Press the F5 key or click the new toolbar button.

    Note that a message box appears for each selected record displaying the contents of the selected record's Company Name field.

Using a Command Button to Enumerate Selected Records

  1. Follow steps 1 through 3 in the "Using a Toolbar Button or an AutoKeys Macro to Enumerate Selected Records" section to create the module with the DisplaySelectedCompanyNames() procedure.
  2. Open the module in Design view and add the following to the Declaration section:
    Dim MySelTop As Long
    Dim MySelHeight As Long
    
    Dim MySelForm As Form
    Dim fMouseDown As Integer
    					
  3. Type or paste the following two procedures:
    Function SelRecord(F As Form, MouseEvent As String)
       Select Case MouseEvent
          Case "Move"
             ' Store the form and the form's Sel property settings
             ' in the MySel variables ONLY if mouse down has not
             ' occurred.
             If fMouseDown = True Then Exit Function
             Set MySelForm = F
             MySelTop = F.SelTop
             MySelHeight = F.SelHeight
    
          Case "Down"
             ' Set flag indicating the mouse button has been pushed.
             fMouseDown = True
          Case "Up"
             ' Reset the flag for the next time around.
             fMouseDown = False
       End Select
    End Function
    
    Public Sub SelRestore()
    Debug.Print "got into Restore"
       ' Restore the form's Sel property settings with the values
       ' stored in the MySel variables.
       MySelForm.SelTop = MySelTop
       MySelForm.SelHeight = MySelHeight
    
    End Sub
    
    					
  4. Save and close the module.
  5. On the Insert menu, click Form to open the New Form dialog box.
  6. Click AutoForm: Tabular in the list of available wizards, select the Customers table in the Choose the table or query where the object's data comes from box, and then click OK.
  7. On the File menu, click Save and save the form with the default name, Customers1.
  8. Open the Customers1 form in Design view. Double-click the form footer section bar to open the properties sheet (if it isn't already open), and change the Height property to .5 inches.
  9. Add a command button to the form footer section and set the following properties as indicated:
    Name: cmdSelectedCompanyNames
    Caption: Display Selected Company Names
    Width: 2"
    OnClick: [Event Procedure]
    OnMouseDown: =SelRecord([Form],"Down")
    OnMouseMove: =SelRecord([Form],"Move")
    OnMouseUp: =SelRecord([Form],"Up")
  10. Add the following code to the Click event of the new cmdSelectedCompanyNames command button:
    Private Sub cmdSelectedCompanyNames_Click()
       Dim X
       ' Restore the lost selection.
       SelRestore
    
       ' Enumerate the list of selected company names.
       X = DisplaySelectedCompanyNames()
    End Sub
    
    					
  11. View the form in Form view and select a record or a set of records by using the record selector buttons on the left side of the continuous form. Click the new "Display Selected Company Names" command button.

    Note that a message box appears for each selected record displaying the contents of the selected record's Company Name field.
NOTE: If you want to use the command button to enumerate the list of selected records in a subform, pass the subform as the argument to the SelRecord() function from the command button's Mouse events. For example, suppose your subform is called Orders Subform; the command button mouse-move properties would be set to the following:
   OnMouseDown: =SelRecord([Orders Subform].[Form],"Down")
   OnMouseMove: =SelRecord([Orders Subform].[Form],"Move")
   OnMouseUp:   =SelRecord([Orders Subform].[Form],"Up")
				

↑ Back to the top


References

For more information about selected records in forms, click Microsoft Access Help on the Help menu, type selecting fields and records in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB208502, kbhowto

↑ Back to the top

Article Info
Article ID : 208502
Revision : 3
Created on : 11/7/2007
Published on : 11/7/2007
Exists online : False
Views : 322