0

I have an issue that's been bugging me for a few hours now:

I have a Clear Button on my Excel userform runs a subroutine that clears all data in my XY Frame.

After running this, I want to return the focus to a textbox in the XY Frame. However, I get an error every time I try to use .SetFocus.

While messing with this, I figured out that the "ActiveControl" is still the Clear Button, and I'm wondering how to switch the ActiveControl to my XY Frame. XYFrame.Activate doesn't work.

Any ideas?

Private Sub ClearButton_Click()

    ClearForm ' Run subroutine to clear out data

    MsgBox (XYForm.ActiveControl) ' This shows that ActiveControl is still "ClearButton"
    XYFrame.Activate ' This gives an error
    XYFrame.TextBox1.SetFocus ' This gives an error

End Sub

The other maddening thing is that I can refer to the textbox value without any issues, but I just can't SetFocus on the textbox either in the subroutine or in the ClearButton sub.

 XYForm.TextBox1.Visible = True  'works fine                        
 XYForm.TextBox1.Enabled = True  'works fine
 XYForm.TextBox1.Locked = False  'works fine
 msgbox(XYForm.TextBox1.Value)   'works fine
 XYForm.TextBox.SetFocus         'doesn't work
shwan
  • 538
  • 6
  • 21
  • Are you sure `TextBox1` is a child of `XYFrame` and not of `XYForm`? Try `XYForm.TextBox1.SetFocus`. – Matteo NNZ Dec 18 '14 at 17:11
  • what exactly is the error message? could it be that the TextBox is disabled in the ClearForm Sub? – Stefan Woehrer Dec 18 '14 at 17:12
  • So TextBox1.Visible = True work no problem. I'm positive TextBox1 is a child of XYframe. I'm getting a Run-time error / Unspecified error – shwan Dec 18 '14 at 17:13
  • In the ClearForm Subroutine, I loop through all the controls in XYFrame and set them to null values. I do this using a for loop with Set thisBox = UserForm.Controls("TextBox" & i), thisBox.Value = vbNullString – shwan Dec 18 '14 at 17:16
  • 1
    `Me.TextBox1.SetFocus` , don't forget the `1` in the textbox's name (your example code wrote `Textbox.setfocus`, wich obviously errors as there is no such control name). I don't think i ever used the parent (even if multipage or others) of a control inside a Userform (contrary to a worksheet, where i use it like a mad man). – Patrick Lepelletier Aug 07 '17 at 12:23

0 Answers0