Hiding multiple controls based upon field

Johnny Drama

In need of beer...
Local time
Today, 01:11
Joined
Dec 12, 2008
Messages
211
Hi All,

I have a form that, when it opens, has a field "beer" that is populated with one of two possible options. Then I have a number of buttons that need to be either visible or hidden based upon what is populated in the beer field.

The buttons are all tagged; one group of buttons is tagged with "domestic" and the other with "imported".

I've spent a good 6 hours trying to figure this out to no avail. Any help would be greatly appreciated...
 
Along the lines of

Code:
  Dim ctl As Control

  For Each ctl In Me.Controls 
    If ctl.Tag = "Whatver" Then
      'Do something
    End If
  Next ctl
 
I tried that and a number of variations. The problem is when the form opens I need the controls to be set depending on what appears in the beer field. If the beer field is populated with "domestic beer" then I need all of the controls that have been tagged domestic to be visible and all the controls that have been tagged imported to be hidden. Vise versa if the beer field is populated with "imported beer".
 
Code:
Private Sub txtBeer_AfterUpdate()
Dim ctl As Control

For Each ctl In Me.Controls
  
If Nz(Me.txtBeer, "") = "" Then
   
   If (ctl.Tag = "domestic") Or (ctl.Tag = "imported") Then
     ctl.Visible = True
   End If
   
Else
    
  If Me.txtBeer = "domestic beer" Then
    If ctl.Tag = "domestic" Then
     ctl.Visible = True
    End If
    If ctl.Tag = "imported" Then
     ctl.Visible = False
    End If
  End If
  
  If Me.txtBeer = "imported beer" Then
    If ctl.Tag = "domestic" Then
     ctl.Visible = False
    End If
    If ctl.Tag = "imported" Then
     ctl.Visible = True
    End If
 End If

End If

Next

End Sub
Code:
Private Sub Form_Current()

Dim ctl As Control

For Each ctl In Me.Controls
  
If Me.NewRecord Then
   
   If (ctl.Tag = "domestic") Or (ctl.Tag = "imported") Then
     ctl.Visible = True
   End If
   
Else
    
  If Me.txtBeer = "domestic beer" Then
    If ctl.Tag = "domestic" Then
     ctl.Visible = True
    End If
    If ctl.Tag = "imported" Then
     ctl.Visible = False
    End If
  End If
  
  If Me.txtBeer = "imported beer" Then
    If ctl.Tag = "domestic" Then
     ctl.Visible = False
    End If
    If ctl.Tag = "imported" Then
     ctl.Visible = True
    End If
 End If

End If

Next

End Sub
I think I've got this right, but beware; I'm in the throes of converting from Access 2003 to 2007 and I find it hard to concentrate properly! :rolleyes: I haven't time to set up a db to test this. If you run into any problems post back and I'll check to see if the trouble is on my end.

You'll need to replace txtBeer with the actual name of your Control where the type of beer is entered.

Linq ;0)>
 
PBaldy - I've tried your method, but I can't get it to work. I've got controls that I'm grouping together using the tag property, but I'm not sure how to translate that into the smaple code you provided.
If Me.Beer = "domestic" Then
Tag.domestic.Visible = True
Else
Tag.imported.Visible = False
End If

I get an invalid qualifier error.

MissingLinq - I tried your method and it works halfway. It only works for the domestic beer. When I try to open the form with imported beer it still shows the controls tagged "Domestic" and hides the controls tagged "imported"
 
I just went to the trouble of setting up a file and the code I gave works just as expected.

A couple of things:

Your attempt at Paul's code

If Me.Beer = "domestic" Then
Tag.domestic.Visible = True
Else
Tag.imported.Visible = False
End If

makes no sense at all! Tag is a Property of a Control, not the name of a Control, which is what your attempt insinuates, sort of.

Where do you have the Tag Property set? It should be in the Properties Pane for the Controls to be hidden, under the Other tab. It needs to be entered there as domestic or imported, just like that, without any quotation marks. You only use quotes around the Tag Property when referring to it in code.

Also, in the Control holding the beer type, it has to be entered as imported beer or domestic beer. Entering imported, or import beer, or domestic won't work. It has to be exactly the same. You might think about changing this Control to a Bound Combobox, where imported beer and domestic beer are the only choices; this would eliminate the possibility of errors here.

If the names and/or values you're entering are different, you need to, as I said before, modify the code I gave you to reflect this. And be sure replace txtBeer with the actual name of the Control on your Form.

Linq ;0)>
 
I appreciate you going through the trouble of setting up a file and testing it for me. I'm going to take you suggestion and convert the beer control to a bound combo box and try and muddle through it.

As far as my attempt at Paul's code...I'm by no means a coder so I'm just trying to figure it out as I go along. :( The tag property is set in the correct place. :):)
 

Users who are viewing this thread

Back
Top Bottom