thao-nhi
asked on
Checkbox click event does not fire vba code until the second click. Access 2016 vba
On the form, I have a checkbox and the click event as shown below. On form load, I have the checkbox value set to false.
Private Sub Check2458_Click()
Check2458.Value = True
ConditionReceived.Value = "Received and working "
The checkbox does not get checked on the first click and the code did not get executed.
Any help resolving this issue is appreciated.
Private Sub Check2458_Click()
Check2458.Value = True
ConditionReceived.Value = "Received and working "
The checkbox does not get checked on the first click and the code did not get executed.
Any help resolving this issue is appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I guess you wish to make it a "one-time update" when updated to True, so - as the value defaults to False - just lock the checkbox when having been clicked using the AfterUpdate event:
Private Sub Check2458_AfterUpdate()
With Me!Check2458
.Locked = .Value
End With
ConditionReceived.Value = "Received and working "
End Sub
You don't have to set the value of the checkbox....this is done when you actually clicking it...think it as the switch for your office lights
so the code should be
so the code should be
Private Sub Check2458_Click()
If nz(Check2458,False) then 'This evaluates if you actually clicked the checkbox and before was Null or False to True
ConditionReceived.Value = "Received and working "
end if
End Sub
A couple of other things. You have an Access generated control name with a number = 2458. It is time to compact and repair your database. Also, it is good practice to give your controls meaningful names. It isn't likely that you'll remember what 2468 is next week let alone next month. Of course, changing the names after the fact requires a lot of clean up since you will orphan any event code - which you will need to move to the correct events using the new name and delete all the old references. You will also get compile errors for any code references to the poor names. But the sooner you clean up, the less work you have to do.
When you reference controls, use
Me.ConditionReceived = "Received and working "
That gives you intellisense and you'll get compile time errors. Other control references are not resolved until runtime and so they result in runtime errors or simply incorrect results.