[AccessD] How do I refer to a range of selected records in a subform datasheet?

David McAfee davidmcafee at gmail.com
Thu Mar 5 15:07:16 CST 2009


I have a read only subform in datasheet mode.

I've switched from using a list box, because the user wanted sorting, column
width, filtering and a couple of other options.

With the list box, if the user wanted to update a bunch of records, I would
do something like this:

The user select the listbox row(s) then presses a command button.

Code in the onClick event would perform a task similar to:


'Inserting records from a listbox (Looping through the listbox)
If Me.lstTitles.ItemsSelected.Count > 0 Then
    Dim varIndex As Variant
    Dim lRecordsAffected As Long
    Dim Cnn As ADODB.Connection
    Dim Cmd As ADODB.Command
    Set Cnn = New ADODB.Connection
    Set Cnn = CurrentProject.Connection
    For Each varIndex In Me.lstAddrLocOrContact.ItemsSelected 'For every
item selected in list box
    Set Cmd = New ADODB.Command
         With Cmd
            Set .ActiveConnection = Cnn
                .CommandType = adCmdStoredProc
                .CommandText = "stp_001B3_AddContactTitles"
                .Parameters.Append .CreateParameter("@ctcID", adInteger,
adParamInput, , Me.strCtcID)
                '.Parameters.Append .CreateParameter("@addrLocID",
adInteger, adParamInput, , Me.lstAddrLoc.Column(3, varIndex))
                .Parameters.Append .CreateParameter("@addrID", adInteger,
adParamInput, , Me.strAddrID)
                Set .ActiveConnection = Cnn
                .Execute lRecordsAffected
            End With
        Set Cmd = Nothing
    Next 'Loop to the next selected item
    Cnn.Close
    Set Cnn = Nothing
End If



I know I can reference the form!sbform!PKID but do I have to loop the the
whole recordset and check if record is selected?

I'm getting rusty with access. :)

David



More information about the AccessD mailing list