• Subform focus problem (XP)

    • This topic has 6 replies, 3 voices, and was last updated 21 years ago.
    Author
    Topic
    #405023

    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?

    Viewing 1 reply thread
    Author
    Replies
    • #828446

      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.

      • #828750

        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.

        • #829898

          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

        • #829899

          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

      • #828751

        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.

    • #828447

      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.

    Viewing 1 reply thread
    Reply To: Subform focus problem (XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: