Here is the code to do this, but there is a catch.
Private Sub Command1_Click() Dim i As Long Dim RS As Recordset Dim F As Form Set F = Me.sf.Form Set RS = F.RecordsetClone If F.SelHeight = 0 Then Exit Sub ' Move to the first selected record. RS.Move F.SelTop - 1 For i = 1 To F.SelHeight MsgBox RS![myfield] RS.MoveNext Next i End Sub
Here's the catch: If the code is added to the button as soon as the user clicks on that button, the selection is lost in the grid (selheight will be zero). Thus, you need to fix this information and save it at the module level using a timer or other events in the form.
Here is an article describing how to work with the catch in detail.
http://www.mvps.org/access/forms/frm0033.htm
Catch 2: This only works with related choices. They cannot select multiple disjoint lines in the grid.
Update:
Perhaps the best event for this, but here is a working implementation using the form.timerinterval property that I tested (at least in Access 2k3, but 2k7 should work fine)
This code goes to SUBFORM, use the property to get the selheight value in the main form.
Public m_save_selheight As Integer Public Property Get save_selheight() As Integer save_selheight = m_save_selheight End Property Private Sub Form_Open(Cancel As Integer) Me.TimerInterval = 500 End Sub Private Sub Form_Timer() m_save_selheight = Me.selheight End Sub
Johnfx
source share