SetFocus not working as I would expect

J

justinclose

I am having some trouble getting the SetFocus method to work the way
that I think it should (and the way that the help files seem to
indicate). Here is my code snippet:

-----------------------
Sub txtbxStartTime_AfterUpdate()
Dim numChkResult As Integer

If txtbxStartTime.Value = "" Then
Exit Sub
'Want to jump out of here if the value was changed, then
deleted. Don't need to operate on empty inputs.
End If

'Calling function and capturing return value
numChkResult = NumberCheck(0) '0 indicates it was Start Time
txtbox that made the call

If numChkResult = 1 Then '"1" return value
indicates a problem
MsgBox "There was a problem with data entered into the Start Time
box. Go back and try again."
txtbxStartTime.SetFocus
lblHoursWorkedCalc.Caption = "" 'clearing the box so there
aren't odd values in it if a box is empty or wrong
Exit Sub
End If

If Not IsEmpty(txtbxStartTime.Value) Then 'Don't want to
head into this if it is empty, gets me errors.
If optStartAM.Value = True Then
dtStartTime = CDate(txtbxStartTime.Value & " A")
End If

If optStartPM.Value = True Then
dtStartTime = Format(txtbxStartTime.Value & " P", "Hh:Nn AM/
PM") 'Building the actual TIME datatype
End If
End If

CalcHoursWorked 'Calling my little function

End Sub
-------------------------------

What happens is that the focus ends up on the next tab stop control
instead of back on the this control. Essentially, I am checking the
numbers entered into the box and I want to return focus back to that
text box if the numbers don't check out. (I am asking them to enter
in the time of day that they started work. It is a simple (well,
supposed to be simple) time card spreadsheet interface project. I am
requesting time started then time ended.)

I have tried the SetFocus in the called NUMBERCHECK function, but get
the same result. So I imagine that it is just a matter of having the
command in the correct event. This is the AFTER_UPDATE event. So it
is my guess that the action of tabbing out of this text box control,
or clicking on another control, gets the SetFocus property evaluated
after this event finishes running.

Can anyone offer advice on this?

Thanks!
 
M

Mole Hunter

Someone? Anything?

The short question, again: How can I return focus to a control?

I am checking a number value using the "AfterUpdate" event, but want
to return the focus back to that same control if the number entered is
bad (so the user can easily reenter a number).
 
N

Norman Jones

Hi Mole Hunter,

Try using the Exit event and set the value of the
Cancel argument to true if the value is not valid.
 

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

Similar Threads


Top