How do I access the selected rows in Access? How do I access the selected rows in Access? vba vba

How do I access the selected rows in Access?


Here is the code to do it, 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 iEnd Sub

Here's the catch: If the code is added to a button, as soon as the user clicks that button, the selection is lost in the grid (selheight will be zero). So you need to capture that info and save it to a module level variable either with a timer or other events on the form.

Here is an article describing how to work around the catch in some detail.
http://www.mvps.org/access/forms/frm0033.htm

Catch 2: This only works with contiguous selections. They can't select mutliple non-sequential rows in the grid.

Update:
There might be a better event to trap this, but here is a working implementation using the form.timerinterval property that i have tested (at least in Access 2k3, but 2k7 should work just fine)

This code goes in the SUBFORM, use the property to get the selheight value in the master form.

Public m_save_selheight As IntegerPublic Property Get save_selheight() As Integer    save_selheight = m_save_selheightEnd PropertyPrivate Sub Form_Open(Cancel As Integer)    Me.TimerInterval = 500End SubPrivate Sub Form_Timer()    m_save_selheight = Me.selheightEnd Sub


I used the technique similar to JohnFx

To trap the Selection height before it disappears I used the Exit event of the subform control in the Main form.

So in the Main form:

Private Sub MySubForm_Exit(Cancel As Integer)  With MySubForm.Form    m_SelNumRecs = .SelHeight    m_SelTopRec = .SelTop    m_CurrentRec = .CurrentRecord  End WithEnd Sub


I've tried doing something like that before, but I never had any success with using a method that required the user to select multiple rows in the same style as a Windows File Dialog box (pressing Ctrl, Shift, etc.).

One method I've used is to use two list boxes. The user can double click on an item in the left list box or click a button when an item is selected, and it will move to the right list box.

Another option is to use a local table that is populated with your source data plus boolean values represented as checkboxes in a subform. After the user selects which data they want by clicking on checkboxes, the user presses a button (or some other event), at which time you go directly to the underlying table of data and query only those rows that were checked. I think this option is the best, though it requires a little bit of code to work properly.

Even in Access, I find sometimes it's easier to work with the tables and queries directly rather than trying to use the built-in tools in Access forms. Sometimes the built-in tools don't do exactly what you want.