User Form textbox not in focus on form reset - SOLVED

Airfix9

Well-known Member
Joined
Sep 23, 2005
Messages
886
Hi all,

I am new to user forms and everything is going swimmingly.... BUT!

I have a form with a text box, a combo box and three command buttons on it.

When the form is initialised, the text box (txtInitial) is enabled and blank, the combo box (cboName) is disabled and blank, button 1 (cmdBackName) is enabled, button 2 (cmdName) is disabled and button 3 (cmdExitName) is enabled.

What happens is that the user enters their surname initial and the combo box is filled with all users whose surname initial that is. The user then selects him/herself from the list. Once done, cmdName is enabled allowing the user to import data to the file.

What I want to do is to reset the form when txtInitial is re-entered. My code for this is as follows:

Code:
Private Sub txtInitial_Enter()
    
    
    txtInitial.Text = ""
    cmdName.Enabled = False
    txtInitial.SetFocus
   
    
End Sub

The problem is that the focus is then set to cmdExitName, instead of txtInitial.

For information, the tab indices for each item are as follows (if that makes any difference):

txtInitial 1
cboName 3
cmdBackName 6
cmdName 4
cmdExitName 5

(The reason that 0 and 2 are missing is that they are labels and - I think - not pertinent to this issue).

Where am I going wrong, please?
 
I don't know why but the TextBox's Exit event is firing on:

cmdName.Enabled = False

This worked for me:

Code:
Dim NoExit As Boolean

Private Sub txtInitial_Enter()
    NoExit = True
    cmdName.Enabled = False
    With txtInitial
        .Text = ""
        .SetFocus
    End With
End Sub

Private Sub txtInitial_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If NoExit = True Then
        Cancel = True
        NoExit = False
        Exit Sub
    End If
    cmdName.Enabled = True
End Sub
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
EXCELLENT, thanks!!!

I had to make two minor adjustments, but it has worked just like I wanted.

The adjustments were moving the "cmdName.Enabled=False" instruction to after the "End With" in the Enter event and changing the "cmdName.Enabled=True" to "cmdName.Enabled=False" in the Exit event as I have enabled cmdName in the cboName change event (I only want them to have the option to hit the import button once they have chosen their name.

Thanks one and all for your help. Off for a beer now!
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top