Check if checkbox is checked when clicking ???

ksor

Registered User.
Local time
Today, 07:20
Joined
Feb 8, 2018
Messages
70
I have a checkbox to make some of the data on my records private.

When that checkbox is checked I hide the private data and shows a button to access these data IF a password is correct.

BUT ...

I too need to be able to UN-privatize the data again by UN-checking the checkbox BUT ONLY if the password is right.

I then think I need a method to check the state of the checkbox when I click it.

If it's

1) UN-checked I have to check it, hide the data and show the "Show"-butten - and it works nicely
2) ALREADY checked I have to ask for the password ect, ect

But HOW do I check the state of the checkbox in the click-event of that same checkbox ... I seems like the state is changing the moment I click on the checkbox !

Any ideas ?
 
Then do the reverse logic:

Private sun chk_click()
If me.chk=false
'Ask for password
'If password not correct, undo
'Me.chk.undo
Else
'Hide controls
End iF
End sub
 
ArnelGP’s code will work (it always does!), but why go through the headache of having passwords which anyone worth their salt can figure out, when you can us the ENVIRON(“USERNAME”) to verify its you and then unhide the controls as necessary?
 
I don't think it's answers my question on how to check the state of the checkbox in the mothods for that checkbox.


Even in the BeforeUpdate the state is CHECKED if it was UN-checked when I clicked it . .. and visa versa. :banghead:Shouldn't the state be UN-checked ?
 
I have 2 different sets of code for this purpose:

Firstly, using If..Else..End If in after update event

Code:
Private Sub  chkDisplayMode_AfterUpdate()

If chkDisplayMode = True Then
   'do something
Else
   'do the opposite thing
End If

End Sub

The On Click event may be more appropriate

Elsewhere, I use a boolean value AnonFlag to hide confidential data in reports

Code:
Private Sub chkAnonymous_Click() 

    AnonFlag = chkAnonymous

End Sub

i.e. if the checkbox is ticked AnonFlag is true & various items are hidden in the report (or vice versa)

The boolean value is defined globally in a module so it can be used in multiple places

Code:
Public AnonFlag As Boolean

HTH
 
Last edited:
ArnelGP’s code will work (it always does!), but why go through the headache of having passwords which anyone worth their salt can figure out, when you can us the ENVIRON(“USERNAME”) to verify its you and then unhide the controls as necessary?


I use it in a genealogic system where the private data should by hidden if someone is looking over my shoulders :D
 
The solution is to use the OldValue property :p


Then you can check in the clickevent if the checkbox was checked or not BEFORE you clicked it !


I wasn't aware of this property - soeey ! :banghead:
 
Last edited:
I would use the BeforeUpdate event of the check box, and cancel the change if the correct password was not supplied. However, whatever rings your bell...
 
I would use the BeforeUpdate event of the check box, and cancel the change if the correct password was not supplied. However, whatever rings your bell...


I've tried that, but it's the same problem ... the Value has changed the moment you click the checkbox ... again you have to look at Oldvalue to see what the state was BEFORE you clicked the checkbox.
 
Did you also try either of the methods I suggested in post #5, both of which I use regularly and I know they work.
 
Did you also try either of the methods I suggested in post #5, both of which I use regularly and I know they work.


I didn't get you on that idea and I now have a test solution that's "by the book" - at least, I think :)

You can change the "Password idea" in the Sub getAdgangskode()


See the attached file ... let me hear your comments :o
 

Attachments

Last edited:
Not sure what language its in but, being a typical English speaker, I can't understand the message boxes etc.

Of course you can use the .oldvalue but you don't need to do so.
The first code I posted previously does exactly the same thing as yours but based on the current value. So you have one step less to code.

As for the chkAnonymous idea, that checkbox is on the form from which the reports at called. The reports then have two sets of controls; always visible and hidden if anonymous. These are tagged as A and H respectively and both visible by default

Then in the report open event, I have one line of code that sets all controls tagged H as hidden if chkAnonymous is true.

Does that make sense
 
Not sure what language its in but, being a typical English speaker, I can't understand the message boxes etc.


It's DANISH and just a warning to the user IF he makes it private he HAS TO KNOW the password if he later wants to see the data OR make them UN-private again.


I think my solution is "straight for everyone to figure out" in the future when my grand-grand-son maybe wants to change something in the code :D
 
It's DANISH and just a warning to the user IF he makes it private he HAS TO KNOW the password if he later wants to see the data OR make them UN-private again.


I think my solution is "straight for everyone to figure out" in the future when my grand-grand-son maybe wants to change something in the code :D

Ah that's what I guessed based on my intensive learning watching all 4 seasons of the Bridge/Bron/Brune. Pity it's now finished.

Also good to hear Access will still be in use in 50 years time. Makes all that coding seem worth while...:D
 
There is a very useful technique which can be used for checking/modifying multiple controls. One method is to individually code for a particular control. This is ok for one or two controls, but if you get up to 10 - 20+ controls, then this technique is very useful and very powerful.... Basically your form can be thought of as a place which contains the controls. The correct term is a "collection of controls". This means you can treat every control in the collection with the same code.

Basically you say I want to examine every control in this form in turn, I want to look at controls with these particular attributes, if they have the necessary attributes which identify them, then I can modify/update them to suit.

I blogged about it on my website here:-

Loop Through a Set of Controls
http://www.niftyaccess.com/loop-through-a-set-of-controls/

where I demonstrate how to test a group of text boxes to see if they have anything entered in them. I also demonstrate how to force one checkbox in a group of checkboxes to always remained checked. It can be any one of the checkboxes. Basically the question you are asking of the operator is to make at least one selection in a group of checkboxes. The code enforces this rule, making sure that at least one checkbox is checked...
 
There is a very useful technique which can be used for checking/modifying multiple controls. ...


Yeah, I've have often used that too, but in this case it's more complicated because it's in a continues form and there you can't just hide/show controls :banghead: the changes will affect ALL records in the form !


I think my demo is a good generel solution but NOT for continues forms - in such case you have to combine what I show in the demo with some conditional formatting ... and in the end THAT's more simple !
;)
 

Users who are viewing this thread

Back
Top Bottom