subforms and forms

ryetee

Registered User.
Local time
Today, 13:11
Joined
Jul 30, 2013
Messages
967
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!?
 
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
 
I think what MarkK is showing you is that if you try working with the form as an object, you will run into problems if the form does not exist.. (no parent) pass it to a string instead of referencing the object. If the string is empty, that's fine the code will not crash!! But if you try and access an object that doesn't exist, ( if there's no parent form) then all hell is set loose...
 
This function is part of a very useful class module I created. It's the only class module I've ever created which is of any use!

Code:
Private Function fGetParentForm(ctrlActiveControl As Control) As Form
Dim strSubName As String
Dim strModuleName As String

strSubName = "fGetParentForm"
strModuleName = "Module - clsCallCalled"

On Error GoTo Error_Handler

        Dim ctlToTest As Control
        Set ctlToTest = ctrlActiveControl        'Will Always be a "Control? No Not always... "
'/////////// CHECK THIS ...No Not always...
        Dim X As Integer

            For X = 1 To 20 'This will check to 20 levels
                If Not fParentIsaForm(ctlToTest.Parent.Name) Then   'Is Parent a Form?
                'It's not a form, so it must still be a control so carry on checking a "Control"
                    Set ctlToTest = ctlToTest.Parent                'No --- Then check the next Parent
                Else
                'It is a form, so you have found the form that the control is on.
                    Set fGetParentForm = ctlToTest.Parent  'Yes -- Then Set the Property to The Parent Form
                    Exit For
                End If
            Next X

    'I cannot foresee a situation with more than 20 levels, but I thought a check on it would be wise
    If X >= 20 Then MsgBox "Message from clsCallCalled ---" & "ERROR 20 Levels EXCEEDED, change 20 to a higher Figure ---", , conAppName

'////////// For Testing -
    If prpFlgDevMode Then
        MsgBox " >>> WARNING !!! --- DEVELOPER MODE IS ACTIVE!!!", , conAppName
        MsgBox "Level Checked to is :- " & X & " Level(s)", , conAppName
    End If

Exit_ErrorHandler:

    Exit Function

Error_Handler:
    Dim strErrFrom As String
    Dim strErrInfo As String

        strErrFrom = "Error From:-" & vbCrLf & strModuleName & vbCrLf & "Subroutine >>>>>>> " & strSubName
        strErrInfo = "" & vbCrLf & "Error Number >>>>> " & Err.Number & vbCrLf & "Error Descscription:-" & vbCrLf & Err.Description

            Select Case Err.Number
                Case 1 'When Required, Replace Place Holder (1) with an Error Number
                    MsgBox "Error produced by Place Holder please check your code!" & vbCrLf & vbCrLf & strErrFrom & strErrInfo, , conAppName
                Case Else
                    MsgBox "Case Else Error" & vbCrLf & vbCrLf & strErrFrom & strErrInfo, , conAppName
            End Select
        Resume Exit_ErrorHandler

End Function      'fGetParentForm

The whole class module "Call Called" is on my website here:-


I'm indebted to MajP for helping to perfect it!
 
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.
 

Users who are viewing this thread

Back
Top Bottom