Check box problem

Greyowlsl

Mlak Mlak
Local time
Today, 18:53
Joined
Oct 4, 2006
Messages
204
Hi,

Ive searched the forums and access help but i cant find why my code dosent work. The error i get is "you entered and expression that has no value".
Code:
Private Sub chk_breaker_GotFocus()

If Me.chk_breaker = True Then
Me.UPGRADE_CIRCUIT = "1"
Else: Me.UPGRADE_CIRCUIT = "0"
End If

End Sub
and this also doesn't work
Code:
Me.chk_breaker.value = -1

I just want it that when i check the check box a '1' appears in a text box straight away.

I use access 97

Thanks,
Leon
 
I don't know why, but it is generally better, in code, to use

Me.chk_breaker.value = -1

rather than

Me.chk_breaker.value = True

although both should work. This has been noted to be true by others as well as myself.

I suspect the problem here is your use of the GotFocus event. I think this is going to fire before you can tick the Checkbox. This kind of thing is normally done using the AfterUpdate event.

But what do you want happening if the user simply ignores the Checkbox, which most users would do if they do not want to tick it?

One way to address this would be to also check it in the Form_BeforeUpdate event and set UPGRADE_CIRCUIT to "0" if it is not ticked.

Another way would be to have the Default Value of UPGRADE_CIRCUIT set to "0".

UPGRADE_CIRCUIT is a Textbox, isn't it, not another Checkbox? Because the syntax you're using is only correct for a Text Field, not a Yes/No Field.

Linq ;0)>
 
Thanks Linq,

I now have:
Code:
Private Sub Form_AfterUpdate()

If Me.chk_breaker.Value = -1 Then
Me.UPGRADE_CIRCUIT = "1"
Else: Me.UPGRADE_CIRCUIT = "0"
End If

End Sub
and
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.chk_breaker.Value = 0
End Sub
which im not sure is correct.

What this code now does is when i open the form the check box is still shaded out, and when i click on the check box 3 other check boxes including the clicked one (all named differently, but were a copy and pasted object from the original) and the Text box 'UPGRADE_CIRCUIT' just remains blank.

Thanks,
Leon
 
To have the value assigned to UPGRADE_CIRCUIT immediately, when chk_breaker is ticked, your code needs to be in the chk_breaker_ AfterUpdate event, not the Form_AfterUpdate event:
Code:
Private Sub chk_breaker_AfterUpdate()
If Me.chk_breaker.Value = -1 Then
 Me.UPGRADE_CIRCUIT = "1"
Else
 Me.UPGRADE_CIRCUIT = "0"
End If
End Sub
And then, in Form_BeforeUpdate,
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 If IsNull(Me.chk_breaker) Then Me.UPGRADE_CIRCUIT = "0"
End Sub

You only want/need "0" to be assigned to UPGRADE_CIRCUIT if the Checkbox has been ignored by the user. If it has been ticked or ticked then unticked, the assignment will already have been made in the chk_breaker_AfterUpdate() event.

As to

"other check boxes including the clicked one (all named differently, but were a copy and pasted object from the original)...remains blank"

I have no idea, this being the first mention of them.

Linq ;0)>
 
Hi Linq,

I run access 97 so that may be why there is no afterupdate event listed in the event tab. I still however put your code in; unfortunately it still doesn't work. The check boxes all check when i click on any one of them, the txtbox is still empty, and i can't unclick any of the check boxes. I don't know what i should try next.

,Leon
 
Any particular reason you're still running Access 97?
 

Users who are viewing this thread

Back
Top Bottom