Color scheme: Light
You are here: start » en » Documentation » Microsoft Access development tools » WKL Forms Object Library » WKL Forms Object Library how-to guides » How to: Run the grid's commands from your own buttons

How to: Run the grid's commands from your own buttons

Offer New, Delete, Edit, and Export as command buttons on the form (or as ribbon commands) next to a WklGrid or WklTreeList, enabled exactly when the grid's own shortcut menu would offer them.

Requires WKL Forms Object Library 1.28 or later.

Solution overview

  • Commands. NewRecord, DeleteSelected, EditSelected, CopySelected, ShowExportDialog, and ShowContextMenu do what the shortcut menu and the keyboard shortcuts do, on the record that has the focus and with the same events and prompts (BeforeDelete asks before deleting, and BeforeCellEdit can cancel).
  • States. CanAdd, CanDelete, CanEdit, and CanExport follow the same rules as the menu commands; a command whose state is False raises wkeNotAvailable, just as Access raises error 2046 for its own commands.
  • Notification. CommandStateChange occurs when one of the four states changes: after the user selects another record, and also after code changes a setting or requeries the records.

Implementation

A form with the grid Grid1 and the buttons cmdNew, cmdDelete, cmdEdit and cmdExport:

Private Sub Form_Load()
    ShowCommandStates
End Sub
 
Private Sub Grid1_CommandStateChange()
    ShowCommandStates
End Sub
 
Private Sub ShowCommandStates()
    With Me!Grid1.Object
        Me!cmdNew.Enabled = .CanAdd
        Me!cmdDelete.Enabled = .CanDelete
        Me!cmdEdit.Enabled = .CanEdit
        Me!cmdExport.Enabled = .CanExport
    End With
End Sub
 
Private Sub cmdNew_Click()
    Me!Grid1.SetFocus
    Me!Grid1.Object.NewRecord
End Sub
 
Private Sub cmdDelete_Click()
    Me!Grid1.Object.DeleteSelected
End Sub
 
Private Sub cmdEdit_Click()
    Me!Grid1.SetFocus
    Me!Grid1.Object.EditSelected
End Sub
 
Private Sub cmdExport_Click()
    Me!Grid1.Object.ShowExportDialog
End Sub

Remarks

  • Set the focus before editing. NewRecord and EditSelected open a cell editor, and the user can type in it only while the grid has the focus, so call SetFocus on the control first.
  • A button's state can be out of date for a moment. CommandStateChange occurs once a change is complete and the grid has displayed it. A click in between runs into wkeNotAvailable; handle it with On Error if the button can be clicked very soon after a change.
  • Not when the form opens. The event reports changes, not the initial state, so read the states in Form_Load, as shown above.
  • Form view only. In Design view, the commands raise wkeNotAvailable, and the states are False.
  • Keyboard shortcuts. The same commands use the keyboard shortcuts of an Access datasheet: Ctrl++ for a new record, Ctrl+- to delete, Shift+F9 to requery (F5 works too), Ctrl+C to copy, and the Menu key or Shift+F10 for the shortcut menu. On a WklGrid record with detail views, Ctrl++ and Ctrl+- open and close the detail views instead.
  • Detail views. DetailGrid returns the grid of a detail view; its commands and states work the same way on that tab's records. Its events don't reach the form module.
en/wiki/msaccess/comctl/how-to/run-grid-commands-from-buttons.txt · Last modified: by Wolfgang van der Stille