Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740

    Unanswered: with subform - screen.activeform.activecontrol

    i use
    screen.activeform.activecontrol
    all over the place.

    works a treat until the control i'm trying to talk to is on a subform.

    anyone got any ideas?

    izy
    currently using SS 2008R2

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    441
    I don't use those activethingies much, but my guess would be that since a subform is not recognized as an open form, but a control on the main form on which it resides, activeform would reference the main form.

    To get hold of the subform, you'd probably need to reference through the main form someway. I don't know how. Only thing I managed in a short test, was getting at the subform control

    screen.activeform.activecontrol.name

    which could be used in a reference

    forms(screen.activeform.name)(screen.activeform.ac tivecontrol.name)...

    but getting the activecontrol on the subform from the main form - I don't know, unless just dumping it to a form public in the one of the after/lost something events on the subform controls.

    Or if you could invoke the code from the subform, me.activecontrol.<someproperty> should work, shouldn't it?
    Roy-Vidar

  3. #3
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    thanks for the thoughts, Roy.

    i think your experiments were right:
    screen.activeform.activecontrol
    in the case of .activecontrol in a subform points at the subform control (the "hole" the subform lives in).

    what i'm looking for is something like
    if screen.activeforms.activecontrol isSubform then
    'go looking for screen.activeform.activecontrol.activecontrol
    'or whatever is the syntax to find Forms!mainForm!subForm!activeControl
    else
    'fine, it's easy, no subform in the way of progress
    endif

    ideas anyone ??

    izy
    currently using SS 2008R2

  4. #4
    Join Date
    Mar 2004
    Location
    Berlin, Germany
    Posts
    107
    maybe a starter

    you can check if the ActiveControl of your ActiveForm is a SubForm container by :

    Dim Ctr As Control
    Set Ctr = Screen.ActiveForm.ActiveControl
    If TypeName(Ctr) = "SubForm" Then ...

    further you can get a grip on the ActiveControl within the SubForm with
    Ctr.Form.ActiveControl. ... (whatever Prop U'r interested in)

  5. #5
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    izy,

    While looking through the help files for something else, I stumbled across this:

    The next two examples show how you might refer to a control named NewData on a subform ctlSubForm contained in the form called OrderForm:

    Forms!OrderForm.ctlSubForm.Form!Controls.NewData
    Forms!OrderForm.ctlSubForm!NewData

    Using this and your example, something like this would work:
    Code:
    ...
    strCtl = Screen.ActiveForm.ActiveControl.Name
    strParent = Screen.ActiveForm.ActiveControl.Parent.Name
    If forms(strParent)(strCtl).ControlType = acSubform Then
       'here's the route to the active control on the subform
       forms(strparent)(strctl).form.activecontrol.name
    End IF
    Inspiration Through Fermentation

  6. #6
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    thanks a lot bachatero - this works purrrrrrrfectly!

    Code:
    Dim myTarget as Textbox
    
    Private Sub Form_Load()
        Dim myControl As Control
        Set myControl = Screen.ActiveForm.ActiveControl
        If TypeName(myControl) = "SubForm" Then
            Set myTarget = myControl.Form.ActiveControl
        Else
            Set myTarget = myControl
        End If
        '    ...etc
    izy
    currently using SS 2008R2

  7. #7
    Join Date
    Mar 2004
    Location
    Berlin, Germany
    Posts
    107
    there you go

  8. #8
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    ...and thanks RNG too!

    .ControlType = acSubform
    looks good too.

    izy
    currently using SS 2008R2

  9. #9
    Join Date
    Mar 2004
    Location
    Berlin, Germany
    Posts
    107
    just as a joke

    you can put your code into a recursive function so it would still work, even if you have several subforms nested into each other (though nested subforms tend to have bad performance)

  10. #10
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    no joke (but also no recursion)
    i already coded it with nested Ifs to go to subSubForm

    just for fun: show me how with recursion


    izy
    currently using SS 2008R2

  11. #11
    Join Date
    Mar 2004
    Location
    Berlin, Germany
    Posts
    107
    put the function getActiveControl somewhere in a module and call it by


    Private Sub Form_Load()
    Dim myControl As Control
    Set myControl = getActiveControl
    ...



    public function getActiveControl(optional ParentControl as Control)
    dim ActControl as Control
    if isMissing(ParentControl) then
    ActControl = Screen.ActiveForm.ActiveControl
    else
    ActControl = ParentControl.Form.ActiveControl
    endif
    if ActControl.ControlType = acSubForm then
    set getActiveControl = getActiveControl(ActControl)
    else
    set getActiveControl = ActControl
    endif
    end function


    be careful .. i did not debug the code .. and recursion is nasssty ssstuff so be sure to save your stuff before running it!

  12. #12
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    almost!!
    needed Variant in place of Control to get IsMissing() flying:

    thanks for the help: final date-gathering demo is attached

    izy
    currently using SS 2008R2

  13. #13
    Join Date
    Mar 2004
    Location
    Berlin, Germany
    Posts
    107
    cool thingie!! i sure will find a use for it!

  14. #14
    Join Date
    Nov 2003
    Location
    LONDON
    Posts
    238
    I would'nt use the activeform/control property as you don't have sufficient control over the results.

    To reference a subform you use the following syntax:

    ' To get the properties of the subform (for example the record source)
    Me("Name of Subform").Form.Recordsource

    ' To get a control on the sub form
    Me("Name of Subform").Form.Controls("Name of subform control").Value

    If you are on a sub form and you want to reference the parent form you would use the following. There is no intellisense when you use the parent property so you have to make sure you spell the properties correctly

    ' To get the properties of the parent form
    Parent.Recordsource

    ' To get the controls of the parent form
    Parent.Controls("Name of control").Value

    To test if a form is a sub form or not use the following global function:

    Public Function IsSubForm(frm As Access.Form) As Boolean
    ' Is the form referenced in the
    ' parameter currently loaded as a subform?
    ' Check its Parent property to find out.
    ' In:
    ' frm: a reference to the form in question
    ' Out:
    ' Return value: True if the form is a subform
    ' False if it's a standalone form
    On Error Resume Next
    Dim strName As String
    strName = frm.Parent.Name
    IsSubForm = (Err.Number = 0)
    Err.Clear
    End Function

    You can run this test any time by running a test along the lines of:
    If IsSubForm(Me) Then
    Debug.Print "I am a subform. My parent name is:" & Parent.Name
    Else
    Debug.Print "I am a main form"
    End If

    Justin

  15. #15
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    yeay Justin

    but

    i have an arbitrary number of forms, subforms, and subsubforms open, each containing an arbitrary number of textboxes expecting date input.

    the _click() of any one of these date-expecting textboxes opens yet another form...

    ...i need _load() of this new form to discover which was clicked amongst the arbitrary number of clickable date-expecting textboxes on the screen.

    it is precisely what you describe as the lack of "sufficient control" in .activecontrol that lets me work out what got clicked: the most recently clicked control remains .activecontrol whilst my new popup modal is loading.

    an earlier version of this utilty had a complicated mechanism for passing the clicked textbox object to a startup routine in the popup - this was seriously undesireable since it needed several lines of code in every date-expecting textbox _Click() to pass the object. the current version using .activecontrol and recursion to zap through sub form levels works fine and needs only one line of code in the _Click() to open the popup.

    izy
    currently using SS 2008R2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •