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?
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Have you tried setting the focus at the end of your cmdName_Click event procedure?
 

Airfix9

Well-known Member
Joined
Sep 23, 2005
Messages
886
Hi Andrew, thanks for replying

The principle is to re-disable everything that should be disabled should the user enter the incorrect initial (we've got some VERY fat-fingered users here!)

The cmdName button is only clicked when the user is happy that they have made the correct selections.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
I understand that. Once you have imported the data, what's the harm in returning to the TextBox automatically?
 

Airfix9

Well-known Member
Joined
Sep 23, 2005
Messages
886

ADVERTISEMENT

No harm. In fact, once the data is imported, the user form will disappear entirely (there is a re-launch button) as this is a display of sales figures and the users are salesmen.

The key is that I want to reset the form in case of error.

The whole process is that the user gets an initial form which asks how they want to see the data (sales view, marketing view, total company view, name view etc). Upon selection of the view type, a new user form is shown asking for more specifics. The cmdBackName button allows the user to return to the previous user form. The cmdExitName button allows the user to close the user form without importing the data.

What I want to do is to re-set the form in case the user makes a mistake. The cmdName click event wouldn't happen in this case.

I hope I'm making myself clear - I know what I'm thinking but not how to put it into words.........
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,813
Office Version
  1. 365
Platform
  1. Windows
Why not just unload the form and show it again?

Then everything should be reset.
 

Airfix9

Well-known Member
Joined
Sep 23, 2005
Messages
886

ADVERTISEMENT

Doesn't seem to work as it interferes with the initial show instruction.

This is set on the frmInitial click event for optName.

Code:
Private Sub optName_Click()
    
    Load FrmName
    
    Unload FrmInitial
    
    FrmName.Show
    
End Sub

I suspect it causes a loop, because I get the following error:

Run-time error '-2147417848 (80010108)':

Automation error
The object invoked has disconnected from its clients.

Which sounds painful to me.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,813
Office Version
  1. 365
Platform
  1. Windows
How many forms do you actually have?

Which form are you having trouble?
 

Airfix9

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

There are 6 forms. frmInitial is the opening form which gives five options. Each option calls a form and unloads frmInitial.

The problem is with frmName - I would like to reset it.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,813
Office Version
  1. 365
Platform
  1. Windows
So what's your exact process?

If the last code you posted is for frmInitial why not unload it before you show/load the next form?
 

Watch MrExcel Video

Forum statistics

Threads
1,108,630
Messages
5,523,989
Members
409,553
Latest member
alscno

This Week's Hot Topics

Top