UserForm1.Textbox1.SetFocus Question

Z

Zane Greer

I have created a program in Excel 2000, which is used on a Windows 98,
Windows 2000 and Windows XP (Home Addition) platform. My problem is
the same on all platforms. The program contains 10 UserForms that
contain numerous Textboxes, comboboxes, listboxes, etc. Information is
entered into the textboxes and eventually transferred to forms for
printing, and a spreadsheet is used to maintain historical records.
Information fed into some of the textboxes is validated prior to the
user being able to continue to the next stage. When the user clicks
CommandButton1 the UserForm is hidden and the data entered validated.
If the data entered is invalid a MsgBox appears with a message
advising the user, who must then click the OK button to continue.
Upon clicking the OK button the unacceptable data that has been
entered in the textbox is erased and the UserForm re-appears so that
the user can enter the correct data. My problem is that I cannot get
the SETFOCUS command to activate the textbox when the UserForm
re-appears.

For example the Userform10 Code contains:

Private Sub commandbutton2_Click()
UserForm10.Hide
UserForm10.TextBox1.SetFocus
UserForm1.Show
End Sub

The UserForm1 Code contains:

Private Sub Commandbutton4_Click()
UserForm1.Hide
UserForm10.Show
UserForm10.TextBox1.SetFocus

End Sub
 
B

Bob Phillips

Zane,

I think I get the general idea but the specific examples you show are not
clear to me as you seem to be bringing up different forms. Normally in a
single form you would do something like test the textbox value on exit and
if in error just highlight, like so

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) > 5 Then
MsgBox "Input must be less than 5 chars"
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
TextBox1.SetFocus
Cancel = True
End If
End Sub

If you do wish to bringt up a difefrent form, I suggest having variables in
that form to say which and use these in that form's activate event. For
instance in form1

Private Sub Commandbutton4_Click()
UserForm1.Hide
With UserForm10
.iTextBoxFocus = 2
.Show
End With
End Sub

Note the dor (.) before the iTextBoxFocus variable as this is referring to a
variable in form10

Then in form10, have something like

Public iTextBoxFocus As Long

Private Sub UserForm_Activate()
Select Case iTextBoxFocus
Case 2: TextBox2.SetFocus
Case 3: TextBox3.SetFocus
Case Else: TextBox1.SetFocus
End Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top