Introduction.
Double-Action doesn't mean that with one click you can execute two different procedures. For that matter, you can do several things with one click. Here, the emphasis is on programming the same Command Button for doing two different Actions i.e., when the Button is clicked a second time it will execute a different action than it did for the first one.
To make the point more clear, let us look into a real-world example. When the user opens a Data Editing Screen we want the screen to be kept locked for the safety of the data and to prevent users from changing the field values accidentally.
But, when it is time to make some changes in one or more fields in the current record the user will click the Command Button to unlock the Form for editing. After editing, the User will click the same button again to lock the screen.
Let us try an example. But, before starting with the design task, let us note down the above points with more specifics so that you will have a general idea of what we are trying to do when we proceed with the implementation steps.
Consider the Requirements
(a) When the Form is open for the first time, or when moved from one record to the other, the Form must be locked for editing. Why, when moving from one record to the other? Well, the User clicked the Button to unlock the Form for editing, but he forgot or didn't bother to click a second time to lock the Form. So we will apply an automatic Lock when she moves into another record.
(b) When the user adds a New Record the Form is fully open for keying in values and our Double-Action Command Button will be in a disabled state.
(c) When the user Clicks the Double-Action Command Button (as we call it now) for the first time, unlock the Form for editing.
(d) When she Clicks the same button a second time, lock the form again, to prevent the change of data by mistake.
The only question at this point is that how do we determine which click what action and if the user repeats the clicks several times, then what happens? It is very simple, read on.
Design Task
Open one of your existing Projects with a Data Editing Screen already available in it.
Create a Command Button at the Footer of the Form.
Click on the Command Button to select it, if it is not already in a selected state, display the Property Sheet (View - - > Properties).
Change the Name Property to cmdEdit.
Change the Caption Property (very important) Value to Edit.
To take care of the points that we have noted at (a), and (b) above, display the VBA Module of the Form (View - - > Code) copy and paste the following 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
The above code will take care of the New Record entry event as well as apply the automatic Lock when the User didn't Click the Command Button to Lock the Form directly.
Now we can concentrate on the Button Click part i.e. (c), and (d) points given above. Here, we have to check for the state of the Button when Clicked by the User. The trick is checking the Caption Value of the Button, and decide what is the user's intentions, and setting the Form Locked or Unlocked state. At every click on the Button, we will change the Caption Value to Edit, or Lock interchangeably.
If the Caption Value was Edit when the user clicked the button, then he/she 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 Animated Command Button with VBA
- Command Button Color Change on Mouse Move