Looping through subforms ?

J

jsteeves

I need to open a form in "read only" - it contains many subforms placed
within tab controls:

/begin diagram
Forms![Master]
Forms![Master]![frmA]
Forms![Master]![frmB]
Forms![Master]![frmB].Form![frmB1]
Forms![Master]![frmB].Form![frmB2]
/end diagram

frmA and frmB are in a tabctl in Master
frmB1 and frmB2 are in a tabctl in frmB

i need to loop thru all the forms to set the read only properties, here is
what i have:

On Error Resume Next

Dim stLinkCriteria As String
Dim frMaster As form
Dim Ctrl As Control
Dim CCtrl As Control

Set frMaster = Form_Master
stLinkCriteria = "fieldA=""" & lstControl & "" 'setting a filter
frMaster.Filter = stLinkCriteria

With frMaster
.AllowAdditions = False
.AllowDeletions = False
.AllowEdits = False
.Visible = True
End With

For Each Ctrl In frMaster
If Ctrl.ControlType = acTabCtl Then
For Each CCtrl In Ctrl
If CCtrl.ControlType = acSubform Then
With CCtrl
.AllowAdditions = False
.AllowDeletions = False
.AllowEdits = False
End With
End If
Next CCtrl
End If
Next Ctrl

End Sub

I have tried without testing for tabcontrol and just testing for subform and
get pretty much the same result. It appears to be locating the subforms and
going through the with block, but not actually applying any of the properties.
 
D

Dirk Goldgar

jsteeves said:
I need to open a form in "read only" - it contains many subforms
placed within tab controls:

/begin diagram
Forms![Master]
Forms![Master]![frmA]
Forms![Master]![frmB]
Forms![Master]![frmB].Form![frmB1]
Forms![Master]![frmB].Form![frmB2]
/end diagram

frmA and frmB are in a tabctl in Master
frmB1 and frmB2 are in a tabctl in frmB

i need to loop thru all the forms to set the read only properties,
here is what i have:

On Error Resume Next

Dim stLinkCriteria As String
Dim frMaster As form
Dim Ctrl As Control
Dim CCtrl As Control

Set frMaster = Form_Master
stLinkCriteria = "fieldA=""" & lstControl & "" 'setting a filter
frMaster.Filter = stLinkCriteria

With frMaster
.AllowAdditions = False
.AllowDeletions = False
.AllowEdits = False
.Visible = True
End With

For Each Ctrl In frMaster
If Ctrl.ControlType = acTabCtl Then
For Each CCtrl In Ctrl
If CCtrl.ControlType = acSubform Then
With CCtrl
.AllowAdditions = False
.AllowDeletions = False
.AllowEdits = False
End With
End If
Next CCtrl
End If
Next Ctrl

End Sub

I have tried without testing for tabcontrol and just testing for
subform and get pretty much the same result. It appears to be
locating the subforms and going through the with block, but not
actually applying any of the properties.

A subform control doesn't have the properties you're trying to set --
the form object *displayed* in the subform control does. Your "On Error
Resume Next" statement is hiding the errors from you. You would need to
write:

With CCtrl.Form ' <----- see the change?
.AllowAdditions = False
.AllowDeletions = False
.AllowEdits = False
End With

You can probably dispense with your test for ...
If Ctrl.ControlType = acTabCtl Then

Every control that is in Controls collection of the tab control is also
in the Controls collection of the form.

You could make the routine recursive, like this:

'------ start of example "air code" ------
Sub MakeFormReadOnly(frm As Access.Form)

Dim ctl As Access.Control

With frm
.AllowAdditions = False
.AllowDeletions = False
.AllowEdits = False
End With

For Each ctl In frm
If ctl.ControlType = acSubform Then
MakeFormReadOnly(ctl.Form)
End If
Next ctl

End Sub
'------ end of example "air code" ------

Then you could simplify your original code to:

'------ start of revised code ------
Dim stLinkCriteria As String

stLinkCriteria = "fieldA=""" & lstControl & "" 'setting a filter
With Forms!Master
.Filter = stLinkCriteria
.FilterOn = True
MakeFormReadOnly(.Form)
.Visible = True
End With
'------ end of revised code ------

NOTE: The code above assumes that the form is already open; that's what
your code seemed to be assuming. Because you used the class-module
syntax, "Set frMaster = Form_Master", it's not clear to me what your
intentions really are.

HOWEVER, if you just want to open the form filtered and read-only, why
don't you just do this:

DoCmd.OpenForm "Master", DataMode:=acFormReadOnly

?
 
D

Dirk Goldgar

Dirk Goldgar said:
HOWEVER, if you just want to open the form filtered and read-only, why
don't you just do this:

DoCmd.OpenForm "Master", DataMode:=acFormReadOnly

I should have noted that you can apply the filter at the same time:

DoCmd.OpenForm "Master", _
WhereCondition:=stLinkCriteria, _
DataMode:=acFormReadOnly

If that's what you had in mind, of course.
 
J

jsteeves

I did originally use the docmd, but if a read/write instance of the form was
already open it would not correctly refresh the permissions.

I also must admit I will sometimes take the hard inefficient route because I
enjoy geeking out on the code.

I'll try checking for the open form first and then using the docmd.
 

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