By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Problem with SetFocus

P: n/a
In my application vehicles arrive and depart from a workshop. The
ArrivalDate and the HandoverDate are each entered. Sometimes the
person who should enter the arrival date forgets to do so.

When the vehicle is handed over to the customer and the HandoverDate is
entered I wish to warn about the possibly missing ArrivalDate. In the
BeforeUpdate event of HandoverDate I have:

Private Sub HandoverDate_BeforeUpdate(Cancel As Integer)
If Not IsNull(Me.HandoverDate) And IsNull(Me.ArrivalDate) Then
MsgBox "Please enter the Arrival date and then re-enter the
Handover date", ,
"This vehicle appears not to have arrived"
Cancel = True
Me.HandoverDate.Undo
Me.ArrivalDate.SetFocus
End If
End Sub

Everything works fine except for the final SetFocus. This gives rise
the error:
Run-time error 2108: You must save the field before you execute the
SetFocus method.
That is clear enough but I am undoing the entry and cancelling the
update. I guess that the Cancel does not have an effect until the
event routine has exited.

Where do I sensibly put the SetFocus?

I could put it in the AfterUpdate event but I would then have to put
some boolean variable in the BeforeUpdate and test it in AfterUpdate.
Seems a bit too messy.

Aug 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 7 Aug 2006 03:26:27 -0700, Jim Devenish wrote:
In my application vehicles arrive and depart from a workshop. The
ArrivalDate and the HandoverDate are each entered. Sometimes the
person who should enter the arrival date forgets to do so.

When the vehicle is handed over to the customer and the HandoverDate is
entered I wish to warn about the possibly missing ArrivalDate. In the
BeforeUpdate event of HandoverDate I have:

Private Sub HandoverDate_BeforeUpdate(Cancel As Integer)
If Not IsNull(Me.HandoverDate) And IsNull(Me.ArrivalDate) Then
MsgBox "Please enter the Arrival date and then re-enter the
Handover date", ,
"This vehicle appears not to have arrived"
Cancel = True
Me.HandoverDate.Undo
Me.ArrivalDate.SetFocus
End If
End Sub

Everything works fine except for the final SetFocus. This gives rise
the error:
Run-time error 2108: You must save the field before you execute the
SetFocus method.
That is clear enough but I am undoing the entry and cancelling the
update. I guess that the Cancel does not have an effect until the
event routine has exited.

Where do I sensibly put the SetFocus?

I could put it in the AfterUpdate event but I would then have to put
some boolean variable in the BeforeUpdate and test it in AfterUpdate.
Seems a bit too messy.
What Boolean test?

Why cancel the valid Handover entry and then re-enter it just because
a different entry has not been yet made?
Save the Handover entry THEN set focus to the missing Arrival field.

In the Handover AfterUpdate event:

If Not IsNull(Me.HandoverDate) And IsNull(Me.ArrivalDate) Then
MsgBox "This vehicle appears not to have arrived." _
& vbNewLine & "Please enter the Arrival date."
Me.ArrivalDate.SetFocus
End If
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
Aug 7 '06 #2

P: n/a
The possible problem with this is - if the user then forgets or ignores
to enter the required arrival date before closing the form, the record
is saved without an arrival date. To overcome this I would have to
make the test when closing the form. Maybe, on balance, that could be a
better place to do the test, preventing the closure of the form if no
arrival date has been entered.

I am wanting to ensure that it is not possible to have a handover date
but no arrival date.
fredg wrote:
>
What Boolean test?

Why cancel the valid Handover entry and then re-enter it just because
a different entry has not been yet made?
Save the Handover entry THEN set focus to the missing Arrival field.

In the Handover AfterUpdate event:

If Not IsNull(Me.HandoverDate) And IsNull(Me.ArrivalDate) Then
MsgBox "This vehicle appears not to have arrived." _
& vbNewLine & "Please enter the Arrival date."
Me.ArrivalDate.SetFocus
End If
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
Aug 7 '06 #3

P: n/a
On 7 Aug 2006 07:13:36 -0700, Jim Devenish wrote:
The possible problem with this is - if the user then forgets or ignores
to enter the required arrival date before closing the form, the record
is saved without an arrival date. To overcome this I would have to
make the test when closing the form. Maybe, on balance, that could be a
better place to do the test, preventing the closure of the form if no
arrival date has been entered.

I am wanting to ensure that it is not possible to have a handover date
but no arrival date.

fredg wrote:
>>
What Boolean test?

Why cancel the valid Handover entry and then re-enter it just because
a different entry has not been yet made?
Save the Handover entry THEN set focus to the missing Arrival field.

In the Handover AfterUpdate event:

If Not IsNull(Me.HandoverDate) And IsNull(Me.ArrivalDate) Then
MsgBox "This vehicle appears not to have arrived." _
& vbNewLine & "Please enter the Arrival date."
Me.ArrivalDate.SetFocus
End If
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
Re: >preventing the closure of the form if no arrival date has been
entered. <
Forms can display many records.
Don't you mean 'prevent saving the record you are on', rather than
just closing the form.

Place the code in the Form's BeforeUpdate event to prevent leaving the
record without entering the Arrival date if the Handover date has been
entered.

If Not IsNull(Me.HandoverDate) And IsNull(Me.ArrivalDate) Then
MsgBox "This vehicle appears not to have arrived." _
& vbNewLine & "Please enter the Arrival date."
Me.ArrivalDate.SetFocus
End If

Then also place the following code in the Form's BeforeUnload event:

If Not IsNull(Me.HandoverDate) And IsNull(Me.ArrivalDate) Then
MsgBox "This vehicle appears not to have arrived." _
& vbNewLine & "Please enter the Arrival date."
Cancel = True
Me.ArrivalDate.SetFocus
End If

Now you won't be able to close the form without entering the Arrival
date if the Handover date has been entered in the record currently
displayed.
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
Aug 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.