Access continuous form: Add a control without modifying the underlying table?

前端 未结 3 876
闹比i
闹比i 2021-01-14 01:30

I\'m making a simple Access form (continuous view). This has a checkbox in the Details section and a Command button in the footer.

This way, the user can use the ch

相关标签:
3条回答
  • 2021-01-14 02:01

    No, there is not. A continuous form is not a lot of active records, it is one active record with views of lots of other records. Any updates to unbound controls apply only to the current record. You can use the record selectors to select a set of records and work with them: http://wiki.lessthandot.com/index.php/Allow_the_User_to_Select_Multiple_Records_for_Processing

    0 讨论(0)
  • 2021-01-14 02:13

    You could include record selection check boxes in a form based on a disconnected recordset. That's an ADO recordset you create in memory, not bound to any data source. With the primary key in the recordset, your command button's click procedure can walk the recordset to retrieve a list of primary keys of the "selected" records. If that approach sounds useful, see this article by Danny Lesandrini at Database Journal: Create In-Memory ADO Recordsets

    I created this form based on code from that article. The main form includes a subform based on a disconnected recordset which is loaded during the subform's Form_Open.

    Form based on disconnected recordset

    Note you don't actually need to display the primary key (ID) in the form; as long as it's included in the recordset, you can retrieve it when walking the recordset.

    Private Sub Form_Open(Cancel As Integer)
        Dim dbs As DAO.Database
        Dim fld As ADODB.Field
        Dim rstAdo As ADODB.Recordset
        Dim rstDao As DAO.Recordset
        Dim strSql As String
    
        Set rstADO = New ADODB.Recordset
        With rstAdo
            .Fields.Append "EmployeeID", adInteger, , adFldKeyColumn
            .Fields.Append "FirstName", adVarChar, 10, adFldMayBeNull
            .Fields.Append "LastName", adVarChar, 20, adFldMayBeNull
            .Fields.Append "Selected", adBoolean
            .CursorType = adOpenKeyset
            .CursorLocation = adUseClient
            .LockType = adLockPessimistic
            .Open
        End With
    
        Set dbs = CurrentDb
        strSql = "SELECT EmployeeID, FirstName, LastName " & _
                 "FROM Employees ORDER BY LastName, FirstName"
        Set rstDao = dbs.OpenRecordset(strSql, dbOpenSnapshot)
    
        Do Until rstDao.EOF
            rstAdo.AddNew
            rstAdo!EmployeeID = rstDao!EmployeeID
            rstAdo!FirstName = rstDao!FirstName
            rstAdo!LastName = rstDao!LastName
            rstAdo!Selected = False
            rstAdo.Update
            rstDao.MoveNext
        Loop
    
        Set Me.Recordset = rstAdo
        rstDao.Close    
        Set rstDao = Nothing
        Set dbs = Nothing
    End Sub
    

    That code sample uses early binding for ADO which requires setting a reference for a version of Microsoft ActiveX Data Objects. However, it can work fine with the appropriate modifications for late binding.

    This approach is not exactly light-weight. However it allows you to have selection check boxes without binding them to a Yes/No field in the actual data table. That would be a challenge in a multi-user application when users might overwrite each others selections in the shared table. The disconnected recordset neatly avoids such conflicts.

    0 讨论(0)
  • 2021-01-14 02:20

    You can most certainly do this. You simply bind the checkbox to a VBA function as it data source.

    That function can return true/false based on say the PK of the row and you store the values in a collection.

    I have a working sample here:

    http://www.kallal.ca/msaccess/msaccess.html

    Grab the multi-select example.

    So the posts here claiming that you cannot do this or you need a column or one needs to use some disconnected recordsets ARE ALL 100% WRONG

    0 讨论(0)
提交回复
热议问题