setfocus on user form

I

inquirer

In a user form I want to ensure that textbox7 contains either YES or NO when
the form is completed. If there is anything else in textbox7, I want the
form shown again with the focus on textbox7 and its contents highlighted.
The following does not set the focus or highlight the contents but doesn't
let me exit the form until I have yes or no in the textbox. Can someone tell
me what is missing please?


Private Sub CommandButton1_Click()
Dim test As Boolean

again1:
test = UCase(TextBox7.Value) = "YES" Or UCase(TextBox7.Value) = "NO"
If Not test Then
normLog10.Hide
MsgBox (TextBox7.Value & " is not acceptable, must be yes or no" &
vbCrLf & vbCrLf & _
" Try again")
normLog10.Show
TextBox7.SetFocus
TextBox7.SelStart = 0
TextBox7.SelLength = 100
GoTo again1:
End If
end sub

Thanks
Chris
 
N

Neil

Chris,

Try this, I haven't tested it.

normLog10.Show

With TextBox7
.SelStart = 0
.SelLength = Len(.Text)
End With

GoTo again1:



regards
Neil
 
G

Guest

Hi,
Moved .show line.

Private Sub CommandButton1_Click()
Dim test As Boolean

again1:
test = UCase(TextBox7.Value) = "YES" Or UCase(TextBox7.Value) = "NO"
If Not test Then
normLog10.Hide
MsgBox (TextBox7.Value & " is not acceptable, must be yes or no" &
vbCrLf & vbCrLf & _
" Try again")

TextBox7.SetFocus
TextBox7.SelStart = 0
TextBox7.SelLength = 100
normLog10.Show <=========== Moved!
GoTo again1:
End If
end sub
 
B

Bob Phillips

This works for me

Private Sub CommandButton1_Click()
Dim test As Boolean

With TextBox7
test = UCase(.Value) = "YES" Or UCase(.Value) = "NO"
If Not test Then
MsgBox (.Value & " is not acceptable, must be yes or no" & _
vbCrLf & vbCrLf & " Try again")
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Sub
 
H

Harald Staff

Hi Chris

Allow me to say that a textbox is perhaps the worst choice of control for a
forced yes-no-response. You should use either a combobox set to List style,
or two optionbuttons (aka radio buttons).

HTH. Best wishes Harald
 
D

Dave Peterson

Or even a single checkbox.

Harald said:
Hi Chris

Allow me to say that a textbox is perhaps the worst choice of control for a
forced yes-no-response. You should use either a combobox set to List style,
or two optionbuttons (aka radio buttons).

HTH. Best wishes Harald
 
I

inquirer

Mnay thanks for your help. Moving the .show line fixed my problem. I will
look into replacing the textbox with an option button or check box as well.
Thanks again
Chris
 

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