Link to home
Start Free TrialLog in
Avatar of thao-nhi
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.
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dale gave you the answer you need so I don't need any points -- except, I'd be careful about arbitrarily setting values of bound controls in the current event of the form.  You don't want to modify existing records and the suggested code will do that. If you are going to set the value of the checkbox to false, you should probably determine if you are on a new record or an existing record.  But setting a default is the best option.  That way you don't need any code at all.

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.
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

Open in new window

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
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

Open in new window