SetFocus not working

J

Junior

Hi - what am i doing wrong with the following code - it works except focus
does not return to [txtDteRet]?

If Not (dtReturnd >= dtIssued) Then
MsgBox "The date returned cannot be earlier than date issued"
Me.txtDteRet.SetFocus
Me!txtDteRet = Null
Exit Sub
End If
 
P

Peter Hoyle

The code looks fine but maybe you have some other code that is running when
Me.txtDteRet is reset to Null.
Try setting the focus after resetting it to Null

Me.txtDteRet = Null
Me.txtDetRet.SetFocus

Cheers,
Peter
 
M

Marshall Barton

Junior said:
Hi - what am i doing wrong with the following code - it works except focus
does not return to [txtDteRet]?

If Not (dtReturnd >= dtIssued) Then
MsgBox "The date returned cannot be earlier than date issued"
Me.txtDteRet.SetFocus
Me!txtDteRet = Null
Exit Sub
End If

Where is that code? You can not reset the focus in some
events. That kind of check is usually done in the control's
BeforeUpdate event and instead of setting the focus, set the
procedure's Cancel argument to True.

Sub dtReturnd_BeforeUpdate(Cancel As Integer)
If Not (dtReturnd >= dtIssued) Then
MsgBox "The date returned cannot be earlier than date
issued"
Me!txtDteRet.Undo
Cancel = True
Exit Sub
End If
 
J

Junior

Marshall - the code is called in the after update event
is that the problem??
Marshall Barton said:
Junior said:
Hi - what am i doing wrong with the following code - it works except focus
does not return to [txtDteRet]?

If Not (dtReturnd >= dtIssued) Then
MsgBox "The date returned cannot be earlier than date issued"
Me.txtDteRet.SetFocus
Me!txtDteRet = Null
Exit Sub
End If

Where is that code? You can not reset the focus in some
events. That kind of check is usually done in the control's
BeforeUpdate event and instead of setting the focus, set the
procedure's Cancel argument to True.

Sub dtReturnd_BeforeUpdate(Cancel As Integer)
If Not (dtReturnd >= dtIssued) Then
MsgBox "The date returned cannot be earlier than date
issued"
Me!txtDteRet.Undo
Cancel = True
Exit Sub
End If
 
M

Marshall Barton

Junior said:
Marshall - the code is called in the after update event
is that the problem??

Most actions that cause the edited text in a control to be
updated have already started the process of moving the
focus, so it's too late for your code to change its mind.

Try the sample code I provided earlier.
--
Marsh
MVP [MS Access]


Junior said:
Hi - what am i doing wrong with the following code - it works except focus
does not return to [txtDteRet]?

If Not (dtReturnd >= dtIssued) Then
MsgBox "The date returned cannot be earlier than date issued"
Me.txtDteRet.SetFocus
Me!txtDteRet = Null
Exit Sub
End If
"Marshall Barton" wrote
Where is that code? You can not reset the focus in some
events. That kind of check is usually done in the control's
BeforeUpdate event and instead of setting the focus, set the
procedure's Cancel argument to True.

Sub dtReturnd_BeforeUpdate(Cancel As Integer)
If Not (dtReturnd >= dtIssued) Then
MsgBox "The date returned cannot be earlier than date
issued"
Me!txtDteRet.Undo
Cancel = True
Exit Sub
End If
 

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