Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Subform focus problem (XP)

    I've got a form with a subform. On the subform the are a number of buttons which can be clicked to open other popup forms.

    My problem is this:

    If I click a button on the subform, then close the popup, the button retains the focus.

    I've got some code in the On Current event for the parent form which changes the state of the form and disables some buttons.

    I get the error - cannot disable the button when it has the focus. I've tried setting the focus back to the main form (and then again on a control on the main form) but I still get the error. I've also noticed that even if I manually move around the main form, the moment I try to move to another record via the navigation buttons I still get the error. How can the button on the subform still have focus?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Subform focus problem (XP)

    Each form that is loaded into memory, whether it be as a main form or as a subform, has an active control. Even if the "real" focus is somewhere on the main form, the subform will still have an active control. You cannot disable or hide a control while is it the active control of the form that contains it. So you should insert code in the On Current event of the subform to ensure that another control is the active control when you disable or hide a command button. If there is no suitable control available, you could create an unbound text box with height and width equal to 0 for this. Before disabling/hiding controls on the subform, set focus to this text box.

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Subform focus problem (XP)

    Thanks Hans, I was leaning towards something like that but thought there might have been another way. Anyway, it works now, which is what counts.

  4. #4
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Subform focus problem (XP)

    For what it's worth, I've found it often makes sense to include Screen.PreviousControl.SetFocus in the Click event for a command button, which means the focus will be back where it was before the user clicked the button when the button's work is finished (unless whatever the button does also involves a change of focus on the same form).

    In cases where you only want the focus to end up in one of a limited group of eligible controls, you could use a procedure like the following SetFocusAmong() procedure. Its first argument is the "default" control where you want to move the focus if it's in an ineligible place, and the second argument is an array of the other eligible controls where it would be OK to leave the focus.

    Sub SetFocusAmong(ctlDefault As Control, ParamArray ctlCandidates())

    Dim ctl As Variant
    Dim strActiveControlName As String

    strActiveControlName = Screen.ActiveControl.Name

    If strActiveControlName = ctlDefault.Name Then
    Exit Sub
    End If

    For Each ctl In ctlCandidates
    If strActiveControlName = ctl.Name Then
    Exit Sub
    End If
    Next ctl

    ctlDefault.SetFocus

Posting Permissions

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