Making a field invisible after checkbox (1 Viewer)

Prakashpdl

New member
Local time
Today, 06:04
Joined
Apr 25, 2020
Messages
27
I have a form with check box (Chk1) and a subsequent field (txtbox1). Field should be visible only if check box is checked. I put a code at afterupdate event of chkbox. Now on form load the checkbox is unchecked but the field is still visible. It becomes invisible only when I check it and again uncheck it. I want the field to be invisible when form opens and visible only after checkbox checked. Is this possible?
 

bob fitz

AWF VIP
Local time
Today, 01:19
Joined
May 23, 2011
Messages
4,719
Put your code in the form's On Current event as well
 

Gasman

Enthusiastic Amateur
Local time
Today, 01:19
Joined
Sep 21, 2011
Messages
14,260
So make it's Visible property false to start with?
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:19
Joined
Mar 14, 2017
Messages
8,777
Like the others have said, you can either put the code in the form's Load event, or if this is a bound form its Current event, and/or set the default visibility to False to start with. I would put it in Load and Current - but that may be overkill, just me. :)
 

Micron

AWF VIP
Local time
Yesterday, 20:19
Joined
Oct 20, 2018
Messages
3,478
Ja, Load not necessary if using Current, which fires after Load when form opens. Current covers both form opening and record navigation. With your approach, changes required in one will necessitate changes in the other, which is just unnecessary work and increases the likelihood that you will forget to change one or the other.
 

Solo712

Registered User.
Local time
Yesterday, 20:19
Joined
Oct 19, 2012
Messages
828
If the two controls are on an unbound form (as it looks they are) you could simply add the statement txtbox1.visible = False to the OnLoad event.

Best,
Jiri
 

Micron

AWF VIP
Local time
Yesterday, 20:19
Joined
Oct 20, 2018
Messages
3,478
you could simply add the statement txtbox1.visible = False to the OnLoad event
Again, I don't get why anyone would use the On Load event. That won't help if user is moving from record to record because it may need to be checked on every record activation. Regardless of whether or not navigation is occurring or even if it doesn't need to be validated when navigating, the Current event fires when navigating records and when the form opens, thus covers both situations.
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:19
Joined
Mar 14, 2017
Messages
8,777
Again, I don't get why anyone would use the On Load event
For an unbound form, plus even for bound forms many times I have loaded a form with no recordsource and then set it dynamically, so I didn't want to assume that Current will fire immediately when the form loads.
 
Last edited:

Micron

AWF VIP
Local time
Yesterday, 20:19
Joined
Oct 20, 2018
Messages
3,478
You are saying the Current event does not fire when an unbound form is opened? That would be news to me and I doubt it is true, but hey...
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:19
Joined
Mar 14, 2017
Messages
8,777
Yes, maybe it does, but it just seems more sense in the load event in case of a form without a current record.
Maybe just me in keeping with the way I think of bound vs. unbound forms. Nothing earthshaking. :)

EDIT: I just tested it and found that the Current event does indeed fire even for an unbound form. Never really tried or wanted to use Current for an unbound form, but it's cool to learn something new, I guess..;)
 
Last edited:

Micron

AWF VIP
Local time
Yesterday, 20:19
Joined
Oct 20, 2018
Messages
3,478
Well we agree to disagree.
Let's forget about the possibility of control focus etc. events firing upon form open and just deal with the basics. Order of events is
Open > Load > Resize > Activate > Current

This does not matter if the form has controls or not, let alone if it is bound so your reply to my question doesn't make sense to me, I'm afraid. It should be obvious that code that validates anything will run twice if it's in the Load (or Open) event as well as the Current. But if the validation needs to be made on a record by record basis, now you need to have it in 2 places. If it needs altering in one place, it needs to be altered in the other as well. If you didn't know that and have been coding in this fashion, that is one thing. If you do know it and defend it as good practice, then I don't know what to say, so I'll say no more about it in this thread. I've either made my point and it makes sense, or I haven't and it doesn't.
 

Gasman

Enthusiastic Amateur
Local time
Today, 01:19
Joined
Sep 21, 2011
Messages
14,260
FWIW I have seen code from experts here in the forum, where code is run twice and I have just accepted it.?

A bit like people who test for BOF & EOF for an empty recordset.?

AFAIK one or the other is enough?, no need to check for both, but some do to be on the safe side?
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:19
Joined
Mar 14, 2017
Messages
8,777
I'm not advocating specifically putting it in both events, once knowing that both events will fire, no. I see your point on that and stand corrected on whether or not Current fires on unbound forms.

I just don't normally use the form's Current event on unbound forms, that's all. Doesn't make a whole lot of sense to me, but if you like using that instead of the Load event, given that I now understand both will fire more or less when the form loads, that's cool.
 

Micron

AWF VIP
Local time
Yesterday, 20:19
Joined
Oct 20, 2018
Messages
3,478
A bit like people who test for BOF & EOF for an empty recordset.?
No, it's not the same as that. You can be at either the end of a recordset or at the beginning of it and there are still records. If both BOF and EOF are true, there are no records.
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:19
Joined
Mar 14, 2017
Messages
8,777
@Gasman I think for recordsets there are much more specific reasons, trickily specific to DAO recordsets, where that is warranted.
Or who knows, maybe I am actually thinking of moving last to get an accurate recordcount. My brain is mush today.
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:19
Joined
Mar 14, 2017
Messages
8,777
If both BOF and EOF are true, there are no records.
Yes, but if EOF is true upon opening it, there are no records ... I think.
And, (I think), that is what he is talking about. People testing for EOF and BOF at the same time immediately upon opening the recordset, which wouldn't make much sense. Who knows. Perhaps God himself will step down and clarify this extremely trivial tit for tat we are having.
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:19
Joined
Mar 14, 2017
Messages
8,777
@Micron
Now that I know that the Current event fires on unbound forms more or less immediately, I have stood corrected on that point specifically.
What I still think is funny is just the logical thought process of using Current on unbound forms instead of using the load event on unbound forms - but like you said, we can agree to disagree on that.
 

Gasman

Enthusiastic Amateur
Local time
Today, 01:19
Joined
Sep 21, 2011
Messages
14,260
No, it's not the same as that. You can be at either the end of a recordset or at the beginning of it and there are still records. If both BOF and EOF are true, there are no records.
OK, I should have clarified that.
I apologise, I should have explained it better,
You open a recordset., at that point you test. There are no records. Only one test is needed at that point.?
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:19
Joined
Mar 14, 2017
Messages
8,777
You open a recordset., at that point you test. Only one test is needed at that point.?
I figured that's what you were saying. And why I agreed.
 

Users who are viewing this thread

Top Bottom