|
Question : Copying multiple user selected records to a recordset
|
|
In a MS Access form, a user can select more than one record by clicking on first record's record selector and then, while holding the shift key down, clicking on nearby record's selector, which selects all the records between and including the two records.
Is there a way, using VB code, to copy these selected records to a recordset that can then be manipulated using code? I know how to use the CurrentRecord property of a RecordSetClone to copy a single selected record, but can't work out how to copy a block of selected records.
|
|
Answer : Copying multiple user selected records to a recordset
|
|
Here's an adapted function. Call it from your form - it will return a DAO recordset with the required rows. You'd call it as Set rst = fLoadRstWithSelected(Me, "YourPKID")
Code follows:
Function fLoadRstWithSelected(pForm As Access.Form, strPKName As String) As DAO.Recordset Dim intBottom As Integer Dim intTop As Integer Dim intI As Integer Dim strFilter As String Dim rst As DAO.Recordset With pForm intTop = .SelTop intBottom = .SelHeight + .SelTop - 1 End With Set rst = pForm.RecordsetClone With rst If intTop > intBottom Then strFilter = "1=0" Else For intI = intTop To intBottom .AbsolutePosition = intI - 1 strFilter = strFilter & "," & .Fields(strPKName).Value Next strFilter = strPKName & " In (" & Mid(strFilter, 2) & ")" End If .Filter = strFilter Set rst = .OpenRecordset End With Set fLoadRstWithSelected = rst Set rst = Nothing End Function
Bear in mind - that a selection is only maintained for as long as the form has the focus. So if this is a subform - as soon as you attempt to click a button on the parent form - your selection will be lost. Using a menu command or the exit event of the subform control will be options though.
|
|
|
|