subforms and forms

ryetee

Registered User.
Local time
Today, 22:35
Joined
Jul 30, 2013
Messages
1,005
I've inherited an access database.
There is at least one instance where a subform is used on 2 different forms. Let's call them form1 and form2.
The problem is that code in an event procedure (after update) assumes that the form is form1. The problem is that this event procedure is now required in form2.
Can you detect which is the main form in the subform event or can the code

If Forms![form1]![dataname] <> "Sale" Then

be changed to use something meaning mainform!?
 
Try:

If Parent.Name = "this name" Then
'do this
Else
'this
End If
 
Because Form.Parent will raise an error if there is no parent, I commonly write a custom property on a subform as follows...
Code:
Property Get ParentName() As String
On Error Resume Next
    ParentName = Me.Parent.Name
End Property

With the presence of such a property, you can now write code on the subform like...
Code:
Private Sub Form_Load()
    Me.cmdRunForm1Process.enabled = Me.ParentName = "Form1"
End Sub
... which only enables a Form1 function call if Form1 is the parent.
 
Try:

If Parent.Name = "this name" Then
'do this
Else
'this
End If
i've got something like that
I've got strParentForm= Me.Parent.Name and then i use
If Forms(strParentForm)![strtemplate] <> "Sale" Then
 
If I provided assistance, I do not remember.

However you can simplify the function a little.

Code:
Public Function getParentForm(callingCtl As Access.Control) As Access.Form
  On Error GoTo errlbl
  Dim ctl As Object
  Set ctl = callingCtl.Parent
  Do While Not TypeOf ctl Is Access.Form
    Set ctl = ctl.Parent
  Loop
  Set getParentForm = ctl
  Exit Function
errlbl:
  MsgBox Err.Number & " " & Err.Description
End Function

However, I agree that 20 levels will never be reached and I doubt you could get more than 3 levels. The only realistic time when the parent is not the form is when the control is on a tabcontrol and the tabcontrol is the parent. So a control on a tab on a tab.
 
Thanks to everyone for the help. Appreciate it
 

Users who are viewing this thread

Back
Top Bottom