Show a note with Undo
In this tutorial, you build a list of rules in which deleting a rule shows the note “Rule deleted” with Undo, and choosing Undo brings the rule back.
Prerequisites
- Microsoft 365 Access or the Access Runtime, 32-bit or 64-bit, with the WKL Forms Object Library 1.34 or later installed and registered for the current Windows user (How to: Install the released package).
- A new or existing database in a trusted location.
Steps
1. Create the sample table
Paste the procedure into a standard module and run it once from the Immediate window
(CreateNoteSample):
Public Sub CreateNoteSample()
With CurrentDb
.Execute "CREATE TABLE tblRules (ID LONG CONSTRAINT PK_Rules PRIMARY KEY, Name TEXT(50), IsDeleted YESNO)"
.Execute "INSERT INTO tblRules VALUES (1, 'Reminder after 30 days', False)"
.Execute "INSERT INTO tblRules VALUES (2, 'Discount from $1,000', False)"
End With
End Sub
2. Set the reference
In the VBA editor, choose Tools › References and select WKL Forms x.y Object Library. WklToast
isn't a control, so Access doesn't set the reference on its own.
3. Create the form
Create a continuous form frmRules with the record source
SELECT * FROM tblRules WHERE Not IsDeleted, a text box Name bound to Name, and a command
button cmdDelete (caption Delete) in the detail section.
4. Write the module
Option Compare Database
Option Explicit
Private WithEvents mToast As WklToast
Private Sub Form_Load()
Set mToast = New WklToast
End Sub
Private Sub cmdDelete_Click()
Dim id As Long, ruleName As String
If IsNull(Me!ID) Then Exit Sub
id = Me!ID
ruleName = Me!Name
CurrentDb.Execute "UPDATE tblRules SET IsDeleted = True WHERE ID = " & id, dbFailOnError
Me.Requery
mToast.Show "Rule """ & ruleName & """ deleted.", wtkSuccess, , "Undo", id
End Sub
Private Sub mToast_Action(ByVal Id As Long, ByVal Tag As Variant)
CurrentDb.Execute "UPDATE tblRules SET IsDeleted = False WHERE ID = " & Tag, dbFailOnError
Me.Requery
End Sub
5. Try it
Open the form and click Delete on the first rule: the rule disappears from the list, and a green note appears at the lower right of the Access window. Click Undo within 8 seconds, or press Ctrl+Shift+Z: the rule is back. The focus stays in the form the whole time.
Expected result
- Each deletion shows one note, which closes on its own after 8 seconds.
- Undo through the link, Ctrl+Shift+Z, or F6 followed by Enter brings the rule back.
- A note stays visible when the form closes, but its Undo then does nothing, because the module of a closed form no longer listens.
ActionFunctioncovers that case (see the how-to).
Next steps
- How to: Show a note after an action, with Undo:
ActionFunction, notes in the corner of a form.