setfocus

G

Guest

I have a text box which receives a 10 digit number.
A fucntion is then called and the validity of the number is checked.
If the number is invalid the bacground colour of the text box is changed to
RED and the focus and Selection for the text box is re-set

Sub ChangeTextBoxColour()
'change text box colour to show alert
frmHotlist.txtCardNumber.BackColor = RGB(255, 0, 0)
frmHotlist.txtCardNumber.SetFocus
frmHotlist.txtCardNumber.SelStart = 0
frmHotlist.txtCardNumber.SelLength = 10

End Sub

This initially worked fine until I inserted another text box for the
insertion of a date the validity of the text is checked using IsDate()
This then calls a subroutine to change the text box colour and reset the
focus on the text box (all incidentally to speed up entering data into the
form)

HOWEVER.
The second setfocus refused to work! as did the selection process.
when I went back to my first text box and rem out the code of the setfocus
to see if the second set focus would work then nothing happened THEN AFTER
putting the code back to the way it was the FIRST TEXT BOX will not work
either!
Is anyone aware it there is some kind of bug in the software or should I be
doing something else?
 
R

RB Smissaert

Not sure what exactly is going on, but I find that sometimes you have to
focus another control first
before moving the focus to the control you really
want to get the focus.

So you want control A and do:

ControlB.SetFocus
ControlA.SetFocus

RBS
 
B

Bob Phillips

Dean,

I have just run up a simple test and it works okay for me.

Private Sub CommandButton1_Click()
If Len(txtCardNumber.Text) < 1 Then ChangeTextBoxColour
If Not IsDate(txtDate.Text) Then txtDateColour
End Sub

Sub ChangeTextBoxColour()
'change text box colour to show alert
With frmHotlist.txtCardNumber
.BackColor = RGB(255, 0, 0)
.SetFocus
.SelStart = 0
.SelLength = 10
End With

End Sub

Sub txtDateColour()
'change text box colour to show alert
With frmHotlist.txtDate
.BackColor = RGB(255, 0, 0)
.SetFocus
.SelStart = 0
.SelLength = 10
End With

End Sub

What does the rest of your code look like?

--

HTH

RP
(remove nothere from the email address if mailing direct)
 

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