views:

419

answers:

3

Hi community,

I have a form which includes a data sheet. I would like to make it possible for a user to select multiple rows, click on a button and have some sql query run and perform some work on those rows.

Looking through my VBA code, I see how I can access the last selected record using the CurrentRecord property. Yet I don't see how I can know which rows were selected in a multiple selection. (I hope I'm clear...)

What's the standard way of doing this? Access VBA documentation is somewhat obscure on the net...

Thanks!

+2  A: 

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.

Ben McCormack
+1 for your second option, though I'd rather not do that if possible... Hopefully Microsoft came up with an obscure way to access those selected rows...?
m_oLogin
Which version of Access are you using? I use Access 2002 at work. Interestingly, I just flipped through "Access 2002 Developer's Handbook" (Considered the Access Developer's Bible) and they have a class they created called MultiPik. This class *does* allow for selecting multiple items at a time, but they say it's slow when you have tons of items (they say 300 items is slow). If you're interested, I can look into the legality of posting the code for this class on Stack Overflow. Let me know.
Ben McCormack
well i'm using 2007 and my table has way more than 10k entries so that probably wouldn't work well..
m_oLogin
Seeing how Catch 2 from JohnFx's answer may prevent your solution from working, do you want me to show you how do to option 2 from my solution above? It would take me a little while to type out the code, so I'll gladly do it if you'd like to see it.
Ben McCormack
Given the inability of selections in a subform control to be non-contiguous, I think a multiselect listbox or a boolean checkbox in a temp table is preferable. I always use the latter, myself. It's really easy to implement and the users understand how it works (they aren't so comfortable with multiselect listboxes, even the "simple" version, which is a pain for selecting multiple items (i.e., the SHIFT key doesn't do anything).
David-W-Fenton
+3  A: 

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 i

End 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 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
amazing... that's why I kept getting selheight at 0... Thanks a lot I will try that right away and accept your answer if it worked out!
m_oLogin
hmm can't get it to work... With Access 2007, selheight stays at 0...
m_oLogin
The trick is grabbing the value before it loses focus. In what event are you grabbing the selheight value? Did you try it in a timer in the subform?Try checking the value in the immediate window (ctrl-g) and see if the issue is the selection resetting after losing focus.
JohnFx
Is an event registered each time the user clicks an item to select? If so, you could use that event to capture everything the user has selected each time they click. It's repetitive, sure, but it would seem to guarantee that you always had the most recent list of selected items when the focus changes to the button control.
Ben McCormack
I added some code to my answer that shows how to do this with the TimerInterval event. There might be a better event that is more closely linked to changing the selection, but I'm not sure offhand what it is. I tried SelectionChange (which seemed most appropriate, but it didn't work for me either). You might want to just experiment with moving the code to the events that look promising until you get a good result. Worst case, the timerinterval approach DOES work.
JohnFx
I couldn't get this to work either until I added this code. It just copies the value from the property where it's saved back to the forms .selheight property. F.SelHeight = save_selheightThat way when F.SelHeight is checked it will equal the value the timer grabbed.
Bob
A: 

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 With

End Sub
DJ