Question on making a group of fields

superfly5203

Registered User.
Local time
Today, 14:54
Joined
Apr 5, 2016
Messages
43
Hello,

I'm making a new database and I have some fields that I'm hiding when a checkbox isn't selected. I have about 20 or so fields that I want to make a group so I don't have to type them all in, along with the labels for each of them. Is that possible?

Also I have noticed that in my form when I go to a previous entry even if the box is checked to show a field, the field isn't there. I'll have to uncheck the box and then recheck it for the field to show up. What code do I need to have to make it so when i cycle through entries the fields will show up based on the boxes that are checked for that specific entry?

here is the code that I have been using:

Private Sub UCA_AfterUpdate()
If Me.UCA = True Then
Me.CCO_Mod_Number.Visible = True
Me.CCO_NTE_Value.Visible = True
Me.Date_CCO_Mod_Signed.Visible = True
Me.Label5.Visible = True
Me.Label6.Visible = True
Me.Label7.Visible = True
Else
Me.CCO_Mod_Number.Visible = False
Me.CCO_NTE_Value.Visible = False
Me.Date_CCO_Mod_Signed.Visible = False
Me.Label5.Visible = False
Me.Label6.Visible = False
Me.Label7.Visible = False
End If
End Sub

Thanks for any help!
 
you could put them in a sub form.
then when user check a box, show the subform

Code:
 sub chkBox1_afterupdate()
    subForm1.visible = chkBox1.value
 end sub
 
I was looking into how to do that, but the guides I found made it seem like you need two tables. Right now I only have one table and one form. Can I make a sub form with only one table?
 
Each control has a tag property. If you set the tag to a known value lets say HideCtrl, you can loop through them and hide / un-hide them.
Code:
Dim ctl As Control

   For Each ctl In me.Controls
        If ctl.Tag = "HideCtrl" Then
            ctl.Visible = Not(Me.Yourcheckbox)
            
        End If
   Next
If you put this code in both the after update of the check box , and in the on current event of the form it will do what you want.
 
You can automate this, using the Tag Property of the Controls whose Visibility needs to be turned on or off.

In Form Design View
  • Holding down <Shift> and Left-Click on each Control to be made Visible or Invisible, in turn
  • Go to Properties - Other
  • In the Tag Property box, enter Marked, just like that, no Quotes
  • Click on any other Property in the pane

Now, for the code:

Code:
Private Sub UCA_AfterUpdate()

Dim ctrl As Control

 If Me.UCA = True Then

  For Each ctrl In Me.Controls
    If ctrl.Tag = "marked" Then
      ctrl.Visible = True
    End If
  Next

 Else

  For Each ctrl In Me.Controls
    If ctrl.Tag = "marked" Then
      ctrl.Visible = False
    End If
  Next

End If

End Sub
To keep the Formatting appropriate, as you move from Record-to-Record, you'll need the same code in the Form_Current event, as well.

Linq ;0)>
 
Thanks both for the replies, setting a tag for fields is exactly what I was looking for. I decided to delete all the old code and use the simpler code. however, now it looks like after I un-check my UCA checkbox, all fields that are not apart of any tag, i have 3 tags set up right now, become invisible. Do I need to make everything I don't want to hide a separate tag?

Here is my new code
Code:
Private Sub Form_Current()
Dim ctrl As Control

 If Me.UCA = True Then

  For Each ctrl In Me.Controls
    If ctrl.Tag = markedUCA Then
      ctrl.Visible = True
    End If
  Next

 Else

  For Each ctrl In Me.Controls
    If ctrl.Tag = markedUCA Then
      ctrl.Visible = False
    End If
  Next

End If
End Sub

Private Sub UCA_AfterUpdate()

Dim ctrl As Control

 If Me.UCA = True Then

  For Each ctrl In Me.Controls
    If ctrl.Tag = markedUCA Then
      ctrl.Visible = True
    End If
  Next

 Else

  For Each ctrl In Me.Controls
    If ctrl.Tag = markedUCA Then
      ctrl.Visible = False
    End If
  Next

End If

End Sub
 
I deleted the quotes on the tag name and I guess that was causing my issues. I went back and put them back in and its working perfectly! I didn't know if i should delete my previous post in case someone else makes the same mistake I did.

Thanks a bunch!
 
Don't forget that as your check box returns True or False you can us it as the value to use that saves on the use of the Else clause.

ctrl.Visible = Me.UCA
 

Users who are viewing this thread

Back
Top Bottom