Bound many controls to a "frame" in a form

R

rahmad

Hi all,
I use and option group to shown or hide many controls
in my form ( text boxes,labels,and a combo box).Is there
a way to bound these controls to a ( called frame ) so that
when we write the code, we just need to type this " frame "
name not alot of these controls name.Is it possible

Thank's
 
D

Douglas J. Steele

Not that I'm aware of.

A common technique is to set the Tag property of the controls so that the
ones you want to control all have the same value there (say "xyz"), and then
write a routine along the lines of:

Sub ToggleVisibility(VisibilityStatus As Boolean)
Dim ctlCurr As Control

' Since you can't hide a control that has focus,
' you need to ensure that focus is set to a control
' that will always be visible before running the loop.

Me.SomeControl.SetFocus

For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "xyz" Then
ctlCurr.Visible = VisibilityStatus
End If
Next ctlCurr

End Sub
 
D

Douglas J. Steele

The Tag property is free-form text: you can put whatever you want in it. All
I'm saying is put some unique expression into the property (I used "xyz" as
my example), and then for each control, check whether its Tag property is
set to that value.

You may also be interested in reading my June, 2004 "Access Answers" column
in Pinnacle Publication's "Smart Access". You can download the column (and
sample database) for free at
http://www.accessmvp.com/djsteele/smartaccess.html
 
D

Douglas J. Steele

Select all of the relevant controls at once (you can "lasso" them, you can
click on each one while holding down the Shift key, or there are other means
of selecting multiple controls at once: just make sure you've only selected
the ones of interest).

Once you've got them all selected, look at the Properties window. Whatever
you type there will apply to all of the selected controls. (That's why the
list of properties is so much shorter). Find the Tag property, and type your
magic word there.

Alternatively, you can select each control one at a time and type the word
into the Properties window for each one.
 
N

New_Access

Sorry Doug,
In what even I have to write the below routine you had
mentioned in your previous post.

Sub ToggleVisibility(VisibilityStatus As Boolean)
Dim ctlCurr As Control

' Since you can't hide a control that has focus,
' you need to ensure that focus is set to a control
' that will always be visible before running the loop.

Me.SomeControl.SetFocus

For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "xyz" Then
ctlCurr.Visible = VisibilityStatus
End If
Next ctlCurr

End Sub

Thank's
 
D

Douglas J. Steele

That routine can be called from wherever you want. Your original question
was how to avoid having to write a number of statements to make a group of
controls visible or not. What event were you planning on using?
 
N

New_Access

I want the controls visible when otion group value is 2.
Is it good idea if I put it in 'on click' event of the option group
or 'after update',cause the form just for adding new records only,
so when the from open the option group value perhaps always 1.
Thank's
 
N

New_Access

I'm sorry Doug,
I can't make it work with this.


Private Sub SpeedMode_opt_AfterUpdate()

'This code for hide or shown
'fields of speed step 2 specification

On Error GoTo SpeedMode_opt_AfterUpdate

Dim ctlCurr As Control
Dim booVisibility As Boolean
Dim strControlToToggle As String
Dim strCurrControlName As String

'If speed mode option is 1,speed step 2 specification fields
'is invisible.If option is, all spec fields of speed step 2
'is visible

booVisibility = Not Me.SpeedMode_opt
strControlsToToggle = ListFormControls(Me.Name, "xyz")

If Len(strControlsToToggle) > 1 Then
For Each ctlCurr In Me.Controls
strCurrControlName = ";" & ctlCurr.Name & ";"
If InStr(1, strControlsToToggle, strCurrControlName,
vbTextCompare) > 1 Then
ctlCurr.Visible = booVisibility
End If

End_SpeedMode_opt_AfterUpdate:
Exit Sub

Err_SpeedMode_opt_AfterUpdate:
MsgBox Err.Description & "(" & Err.Number & ") in " & _
Me.Name & ".SpeedMode_opt_AfterUpdate", _
vbOKOnly + vbCritical, "Error Occured"
Resume End_SpeedMode_opt_AfterUpdate

End Sub
 
D

Douglas J. Steele

That bears little resemblance to the code I suggested.

What's the function ListFormControls(Me.Name, "xyz")? What does it return?
 
N

New_Access

Compile Error:
Sub or function not defined

Douglas J. Steele said:
That bears little resemblance to the code I suggested.

What's the function ListFormControls(Me.Name, "xyz")? What does it return?
 
D

Douglas J. Steele

Afraid I can't help you, since I never suggested a function by that name in
my solution so I have no idea what it's supposed to be,

Just to reiterate, my suggestion was:

Sub ToggleVisibility(VisibilityStatus As Boolean)
Dim ctlCurr As Control

' Since you can't hide a control that has focus,
' you need to ensure that focus is set to a control
' that will always be visible before running the loop.

Me.SomeControl.SetFocus

For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "xyz" Then
ctlCurr.Visible = VisibilityStatus
End If
Next ctlCurr

End Sub

To have your AfterUpdate event change the visibility, you'd use

Private Sub SpeedMode_opt_AfterUpdate()
'This code for hide or shown
'fields of speed step 2 specification

On Error GoTo SpeedMode_opt_AfterUpdate

Dim booVisibility As Boolean

booVisibility = Not Me.SpeedMode_opt
Call ToggleVisibility(booVisibility)

End_SpeedMode_opt_AfterUpdate:
Exit Sub

Err_SpeedMode_opt_AfterUpdate:
MsgBox Err.Description & "(" & Err.Number & ") in " & _
Me.Name & ".SpeedMode_opt_AfterUpdate", _
vbOKOnly + vbCritical, "Error Occured"
Resume End_SpeedMode_opt_AfterUpdate

End Sub
 

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