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, andShowContextMenudo what the shortcut menu and the keyboard shortcuts do, on the record that has the focus and with the same events and prompts (BeforeDeleteasks before deleting, andBeforeCellEditcan cancel). - States.
CanAdd,CanDelete,CanEdit, andCanExportfollow the same rules as the menu commands; a command whose state is False raiseswkeNotAvailable, just as Access raises error 2046 for its own commands. - Notification.
CommandStateChangeoccurs 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.
NewRecordandEditSelectedopen a cell editor, and the user can type in it only while the grid has the focus, so callSetFocuson the control first. - A button's state can be out of date for a moment.
CommandStateChangeoccurs once a change is complete and the grid has displayed it. A click in between runs intowkeNotAvailable; handle it withOn Errorif 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
WklGridrecord with detail views, Ctrl++ and Ctrl+- open and close the detail views instead. - Detail views.
DetailGridreturns 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.
Related topics
- API reference: WklGrid (shortcut menu and keys), WklTreeList (
NewRecord,DeleteSelected,EditSelected,CopySelected,ShowExportDialog,ShowContextMenu,CanAdd,CanDelete,CanEdit,CanExport,CommandStateChange)
en/wiki/msaccess/comctl/how-to/run-grid-commands-from-buttons.txt · Zuletzt geändert: von Wolfgang van der Stille