Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Form cannot be found" when trying to call subform on a tabbed form

Status
Not open for further replies.

lopexo

Programmer
Joined
Oct 3, 2003
Messages
5
Location
US
I have a module that has a function to change the properties of all controls in a form, (i.e. font, text, color etc.) it is called when a form is opened and it works perfectly. However, I've tried to use the same code on a form that has tabs and subforms in the tabs. The control changes are applied thoughout the form on all tabs, except on the subform's controls. I've tried to call the same function on the subform form_open() and I get a message saying that the form is not open and thereofore cannot be found. i've also tried calling the function on the on_click even of the tab that has the subform, but it still says that form is not open even tho it is. not sure how to correct this, any help would be greately appreciated.

Lopexo
 
Are you looping through the controls collection? You'll need to check the control types to reference the subform:
Code:
  Dim sfrm As Form
  Dim ctl As Control
  Dim sctl As Control

  For Each ctl In Me.Controls
    [blue]If TypeOf ctl Is SubForm Then[/blue]
      Set sfrm = ctl.Form
      For Each sctl in sfrm.Controls
        [green]'format subform controls[/green]
      Next sctl
    Else
      [green]'format main form controls[/green]
    End If
  Next ctl

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
thanks. I am looping through all the controls in the form. the code you provided does indeed work to check whether the form is a subform or not( actually I have not gotten to the point where it actually tests for a subform) since my code does not even get there when I call the function from the subform.

in case this helps, here is the code i'm using.

Code:
        If Not fIsLoaded(strFormName) Then
        '    MsgBox "Form " & strFormName & " is probably closed!! " & _
        '        vbCrLf & "Please open it & try again.", vbCritical
            Exit Function
        End If
    
        Set frm = Forms(strFormName)
        'Count the number of controls
        intCnt = frm.Count

    For Each Ctrl In frm.Controls
        If TypeOf Ctrl Is SubForm Then
        Set frm = Ctrl.Form
        For Each sctl In frm.Controls
        'format subform subcontrols
..
..
        Next sctl
        Else
        'format main form controls
..
..
    End Select
    End If
Next Ctrl


--code to check whether form is opened or not
Function fIsLoaded(ByVal strFormName As String) As Integer
'Returns a 0 if form is not open or a -1 if Open
    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> 0 Then
        If Forms(strFormName).CurrentView <> 0 Then
            fIsLoaded = True
        End If
    End If
End Function

for some reason, the fIsLoaded form return error "Microsoft Office cannot find the form "form name"....
 
A subform isn't considered an open form, but a control on the main form on which it resides, so yes, the IsLoaded will not report it as open, and the "Set frm = Forms(strFormName)" will bomb.

I'm not entirely sure which direction you wish to persue, but I think you'd need to amend your code a bit. If you need to check whether a form is open, you could pass for instance me.parent.form.name (or just me.parent.name) to the function, to check whether the subforms main form is open. And in such case, you'd need to do something about the set statement too (for instance like demonstrated by VBSlammer).

But have a look at faq702-5010, section two, which deals with something similar - calling the function recursively for each subform, performing some property toggling.

It should normally be safe to call such recursive function from the main form, as the sequence of events usually have the main form open/load after the subform open/load.

Roy-Vidar
 
Thanks RoyVidar and VBSlammer, both suggestions helped a lot, but while testing the example on FAQ702-5010, I get a type missmatch error when calling the function recursively from the subform case. any ideas...I just copied and pasted the example for testing.

thanks

Code:
call rvsToggleProperties(Me)

Public Sub rvsToggleProperties(frm As Form)
    Dim ctl As Control
    Frm("txtTst").SetFocus
    For Each ctl In frm.Controls
        Select Case ctl.ControlType
            Case acComboBox, acListBox, acTextBox
                If (ctl.Name <> "txtTst") Then
                    ctl.Visible = Not ctl.Visible
                    ' toggling the visible property
                End If
            Case acSubform
                Call rvsToggleProperties(ctl.Form)
        End Select
    Next ctl
End Sub
 
Don't know, it has not occured on previous testing, and I can't replicate it now.

Which line, what type of control, did you try this from a button on the form (i e after both subform and main form is fully open/loaded), or in the load/open of any of the forms (risking that the referenced form isn't yet fully opened)

Roy-Vidar
 
mmmm....i tried it on the main form's form load sub.

However, I've made some changes to my code using VBslammer suggestion and your suggestion of passing "me.parent.form.name " on the sub form, and that worked perfectly.

thank you, my problem has been resolved.

lopexo
 
Forms![frmmainformname]![subformname].SetFocus
Forms![frmmainformname]![subformname].Form![flieldname].SetFocus

dont worry about the tab control. They aren't even aware that they are there, I am finding!

misscrf

It is never too late to become what you could have been ~ George Eliot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top