Introduction.
Double-Action does not simply mean executing two different procedures with a single click—after all, you could trigger several actions at once if you wanted. The emphasis here is on programming the same command button to perform two distinct actions depending on whether it is being clicked for the first time or for the second time.
In other words, when the button is clicked once, it performs one specific action; when clicked again, it performs a different action.
For clarity, let’s consider a practical example. Suppose a user opens a data editing screen. To protect the data and prevent accidental changes, the screen should initially be in locked mode, making the fields read-only.
When it becomes necessary to edit one or more fields in the current record, the user clicks the command button to unlock the form for editing. After making the required changes, the user clicks the same button again to lock the form back.
Double-Action Command Button – Functional Roadmap
-
Initial Form State
-
When Form opens or when moving to a different record, all fields are locked for editing.
-
Purpose: Prevents accidental edits, even if the user forgot to relock the previous record.
-
-
New Record Handling
-
If the user starts adding a new record, the form opens fully unlocked for data entry.
-
The Double-Action button remains disabled in this state (no locking needed for new entry).
-
-
First Click Action
-
Unlocks the form for editing.
-
Allows changes to the current record.
-
-
Second Click Action
-
Locks the form again to protect data from accidental changes.
-
-
Click State Tracking
-
The system uses a simple toggle:
-
Odd-numbered clicks = Unlock form.
-
Even-numbered clicks = Lock form.
-
-
Repeated clicking just alternates between locked and unlocked states.
-
Implementation Steps for the Double-Action Command Button
-
Open an Existing Project
Open one of your existing MS Access projects that already contains a data editing form. -
Add the Command Button
-
Switch the form to Design View.
-
Insert a Command Button into the Form Footer section.
-
-
Set Button Properties
-
Click once on the button to select it.
-
Open the Property Sheet (
View → Properties). -
Change the Name property to: cmdEdit
Change the Caption property value (very important) to: Edit
Open the Form’s VBA Module
-
With the form still selected, go to View → Code to open the form’s VBA code module.
-
-
Insert the Code
Copy and paste the following VBA code into the module:
-
Private Sub Form_Current()
If Me.NewRecord Then
With Me
.cmdEdit.Caption = "Edit"
.cmdEdit.ForeColor = 0
.cmdEdit.FontBold = False
.AllowEdits = True
.cmdEdit.Enabled = False
End With
Else
With Me
.AllowEdits = False
.cmdEdit.Caption = "Edit"
.cmdEdit.ForeColor = 0
.cmdEdit.FontBold = False
.cmdEdit.Enabled = True
End With
End If
End Sub This VBA code fully implements your functional roadmap:
-
Form_Current locks or unlocks fields depending on whether the record is new.
-
cmdEdit_Click toggles between Edit and Lock modes.
-
Separate LockForm and UnlockForm procedures make it easy to extend for other control types if needed.
If the Caption Value was 'Edit' when the user clicked the button, then the user wants to edit values on the Form.
If the Caption was 'Lock', then the user wants to lock the Form after editing the data.
The following code will do the trick. Copy and paste the VBA Code into the Form's Module and save the Form.
The VBA Code
Private Sub cmdEdit_Click()
Dim cap As String
cap = Me.cmdEdit.Caption
Select Case cap
Case "Edit"
With Me
.AllowEdits = True
.cmdEdit.Caption = "Lock"
.cmdEdit.ForeColor = 128
.cmdEdit.FontBold = True
.Refresh
End With
Case "Lock"
With Me
.AllowEdits = False
.cmdEdit.Caption = "Edit"
.cmdEdit.ForeColor = 0
.cmdEdit.FontBold = False
.Refresh
End With
End Select
End Sub Experiment with your design and check whether it works every time correctly as expected.
If any suggestions for improvement or found that it didn't work the way you expected it, then please post them in the comments.
Earlier Post Link References:
- Command Button Animation
- Double Action Command Button
- Colorful Command Buttons
- Transparent Command Button
- Command Button Animation-2
- Creating an Animated Command Button with VBA
- Command Button Color Change on Mouse Move













