Test for open as Parent or Subform

caferacer

Registered User.
Local time
Today, 19:14
Joined
Oct 11, 2012
Messages
96
Hi All,

I have a form which is used on its own standalone and is also used as a subform. I want to make it read only when opened as a standalone.

It’s opened from a command button using

DoCmd.OpenForm "frm Vendors Secondary", acNormal, , "[EquipmentIDFK]=" & EquipmentID, , acFormReadOnly, Me![VendorEquipmentJCTID]

When it’s used as a subform there is other code in the form OnLoad event which looks at user permissions and allows edits if the user has sufficient rights, so appears to be overriding the acFormReadOnly.

I would like to insert a test for the form opened as a parent or subform into the code below, but have searched and not found anything?

Private Sub Form_Load()
On Error GoTo Err_Form_Load
Dim iEdit As Integer
'set edit permissions
iEdit = Forms!frmmenu!txtLevel.Value
Select Case iEdit
Case Is < 2
Me.AllowEdits = False
Case Else
Me.AllowEdits = True
End Select

Any suggestions welcome.

Thanks

Mark.
 
Last edited:
Hi All,

I have a form which is used on its own standalone and is also used as a subform. I want to make it read only when opened as a standalone.

It’s opened from a command button using

DoCmd.OpenForm "frm Vendors Secondary", acNormal, , "[EquipmentIDFK]=" & EquipmentID, , acFormReadOnly, Me![VendorEquipmentJCTID]

When it’s used as a subform there is other code in the form OnLoad event which looks at user permissions and allows edits if the user has sufficient rights, so appears to be overriding the acFormReadOnly.

I would like to insert a test for the form opened as a parent or subform into the code below, but have searched and not found anything?

Private Sub Form_Load()
On Error GoTo Err_Form_Load
Dim iEdit As Integer
'set edit permissions
iEdit = Forms!frmmenu!txtLevel.Value
Select Case iEdit
Case Is < 2
Me.AllowEdits = False
Case Else
Me.AllowEdits = True
End Select

Any suggestions welcome.

Thanks

Mark.

I've not had to check for the presence of a parent form before, if there is no direct switch or setting to do so (I'm sure others will chime in if there is) it is just a simple matter of referring to a property, let's say the name of the parent form. If the form in question is opened as 'standalone' then an error will be generated. The error will be 2452. Simply capture this error code and continue on your merry way. If you need more info just ask.
 
you can modify this code to suit your needs


Code:
 Private Sub Form_Load()
     
     On Error Resume Next
    Debug.Print Parent.Name
    If Err.Number = 2452 Then
        MsgBox "no parent"
    Else
        MsgBox "parent is " & Parent.Name
    End If
    
End Sub
 
cj's idea is neat.


I do this in the form open event

Code:
 dim p as form
 on error goto noparent
  
 set p = me.parent
 'form is a subform
 exit sub
  
 noparent:
 'form is not a subform
 

Users who are viewing this thread

Back
Top Bottom