Can I group controls together and apply code to the group

J

Julia B

Hi

I've got a form with lots of entry fields. When I open the form I want them
to be invisible but then, under certain circumstances want to make them
visible again. They may go back to being invisible later.

I was wondering if there was any way to change a property to a group of
controls in one go? Can I group them and do the following:

If scenario1 then
group.visible = true
else
group.visible = false
end if

I hope so, otherwise it's going to take ages to write the code!!

Thanks in advance
Julia
 
F

fredg

Hi

I've got a form with lots of entry fields. When I open the form I want them
to be invisible but then, under certain circumstances want to make them
visible again. They may go back to being invisible later.

I was wondering if there was any way to change a property to a group of
controls in one go? Can I group them and do the following:

If scenario1 then
group.visible = true
else
group.visible = false
end if

I hope so, otherwise it's going to take ages to write the code!!

Thanks in advance
Julia

First, manually enter some text in each control's Tag property to
identify which group you wish that control to be part of, something
like "GroupA" or "GroupB".

Then use code to make just that one group of controls visible or not:

Dim ctl as Control
If [SomeControl] = Scenario1 Then
For each ctl in Me.Controls
if ctl.tag = "GroupA" then
ctl.Visible = True
ElseIf ctl.Tag = "" Then
ctl.Visible = True
Else
ctl.Visible = False
End IF
Next
Else
For each ctl in Me.Controls
if ctl.tag = "GroupB" then
ctl.Visible = true
ElseIf ctl.Tag = "" Then
ctl.Visible = True
Else
ctl.Visible = False
End IF
Next
End If

Only controls whose Tag property is GroupA or GroupB will be effected.
 
A

Arvin Meyer [MVP]

There are no control arrays in Access, but you can loop through the
controls collection and do things. Also there is a Tag property in every
control, so you could loop through all the controls and check the tag
property, if, for instance you wanted to exempt some of your controls:

Add this to a standard module:

Public Sub HideIt(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls
With ctl
If ctl.Tag = 1 Then
ctl.Visible = False
End If
End With
Next ctl
Set ctl = Nothing
Set frm = Nothing
End Sub

Then in your form, you can do:

If scenario1 Then
HideIt
End If
 
J

Julia B

That's excellent, thanks very much!!
Julia

fredg said:
Hi

I've got a form with lots of entry fields. When I open the form I want them
to be invisible but then, under certain circumstances want to make them
visible again. They may go back to being invisible later.

I was wondering if there was any way to change a property to a group of
controls in one go? Can I group them and do the following:

If scenario1 then
group.visible = true
else
group.visible = false
end if

I hope so, otherwise it's going to take ages to write the code!!

Thanks in advance
Julia

First, manually enter some text in each control's Tag property to
identify which group you wish that control to be part of, something
like "GroupA" or "GroupB".

Then use code to make just that one group of controls visible or not:

Dim ctl as Control
If [SomeControl] = Scenario1 Then
For each ctl in Me.Controls
if ctl.tag = "GroupA" then
ctl.Visible = True
ElseIf ctl.Tag = "" Then
ctl.Visible = True
Else
ctl.Visible = False
End IF
Next
Else
For each ctl in Me.Controls
if ctl.tag = "GroupB" then
ctl.Visible = true
ElseIf ctl.Tag = "" Then
ctl.Visible = True
Else
ctl.Visible = False
End IF
Next
End If

Only controls whose Tag property is GroupA or GroupB will be effected.
 
J

Jan Baird

Jan Baird is out of the country until September 20. Every effort will be
made to respond to messages, but please be patient.
 
J

Jan Baird

Jan Baird is out of the country until September 20. Every effort will be
made to respond to messages, but please be patient.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top