Hiding/showing subform with a checkbox

AirCounsel

New member
Local time
Today, 16:46
Joined
May 4, 2012
Messages
8
Hi, I'm new here and somewhat inexperienced with Access VBA, so any instructions will need to be as detailed as possible. I want to be able to hide or show a subform on a main form with a checkbox bound to a table field. I've used the following code in the form current event and the checkbox afterupdate event:

'On current event of main form
'Check if Active checkbox is selected
'then show or hide subform
If Me.ChkWorker = True Then
'Me.Workers_Subform.Visible = True
Else
Me.Workers_Subform.Visible = False
End If

I get mixed results. When I check and uncheck the checkbox, it works fine. However, when I closed the form and re-open, things don't work. The checkbox appears checked, but the subform is not visible until I uncheck and check it again. Same issue when I first open the database. So, If this code is correct, under which event should I place it.

Thanks!

AirCounsel
 
Hi AirCounsel,

You code is triggered on an event. In this case you probably have this code executed each time the checkbox is clicked. It then checks to see if the checkbox was checked or unchecked and opens or closes the subform accordingly.

When your Main form first loads that even hasn't triggered yet (nothing has been clicked yet). So all you need to do is copy that same exact code to the Main Forms OnLoad() event. that way, when the form first loads it will check to see if ChkWorker is checked or not. If it is, it will display the Subform.

Hope that makes sense.
 
Me.Workers_Subform.Visible = Me.ChkWorker

in both the Form_Current AND ChkWorker_AfterUpdate events.

If there's a chance the field might be null then the code should be:

Me.Workers_Subform.Visible = Nz(Me.ChkWorker,False)
 
Thanks FuzeK3! I added the code to the form Load event. Still didn't work. Right now, the only time that it works is when I'm checking or unchecking the checkbox. Otherwise, when the form loads it shows the ckeckbox with a check mark, but the subform is not visible. Same thing if I navigate to different records. What's going on?
 
Thank you Vila Restal, it workerd like a charm! For my education, would you mind explaining what was going on and what your code actually did?
 
The Current event happens each time a record is shown in the form. So you need to do it then so it shows/hides correctly for the current record. And of course you need to do it when that chkbox value is changed.

And finally, whenever you find yourself writing code:

If Condition Then
x = True
Else
x = False
End If

you can replace it with

x = Condition

(and that works no matter what x is (variable or property) or what the Condition is)

unless, of course, if you want other lines of code inside the If and Else branches.

Oh and Nz is very handy function that works like this:

Nz(x, y) ---- is equivalent to -----> IIf(IsNull(x), y, x)

returns x unless x is null in which case returns y
 

Users who are viewing this thread

Back
Top Bottom