Canceling adding a new record on a form

  • Thread starter Thread starter David C. Holley
  • Start date Start date
D

David C. Holley

I have a subform that lists guests attached to a reservation using
CONTINUOUS view. Guests are selected using a comboBox. Since a guests
SHOULD NOT be listed twice on a reservation, I want to put something
together where if the guest is selected, the user is advised that the
guest is already attached to the reservation. I can check for wether or
not the guest is already attached easily enough, however I need to
figure out to which event I should attach the code - Form_BeforeInsert,
Form_AfterInsert, comboBox_?.
 
David said:
I have a subform that lists guests attached to a reservation using
CONTINUOUS view. Guests are selected using a comboBox. Since a guests
SHOULD NOT be listed twice on a reservation, I want to put something
together where if the guest is selected, the user is advised that the
guest is already attached to the reservation. I can check for wether
or not the guest is already attached easily enough, however I need to
figure out to which event I should attach the code -
Form_BeforeInsert, Form_AfterInsert, comboBox_?.

BeforeInsert is to soon. That is when the record is first dirtied. It "might"
work if the control that is dirtying the form is the ComboBox.

AfterInsert is too late. No point in waiting until after the record is saved to
tell them they need to change it.

The AfterUpdate of the ComboBox or the BeforeUpdate of the form are both good
choices. You could of course make a unique index at table design level so that
it is impossible to enter such a duplicate. Then you don't need any code at
all.
 
Bas said:
You say "if the guest is selected", so I'd suggest you use the Click
or AfterUpdate event of the combobox. (I usually use Click because
then I don't have to distinguish between bound and unbound controls)

Are you suggesting that AfterUpdate doesn't work with an unbound ComboBox? Or
is there some other difference?
 
You say "if the guest is selected", so I'd suggest you use the Click or
AfterUpdate event of the combobox. (I usually use Click because then I
don't have to distinguish between bound and unbound controls)
 
That is what I am suggesting indeed... be noted I decided this quite
some time ago, and never tried AfterUpdate since. Let's test. [fumbles]
No, no difference.

Which leaves me with other questions, more Why-typed. What could be the
use of a Click event for a combobox? It fires along with AfterUpdate.
Could there be some setting where AfterUpdate indeed doesn't fire? As I
suspect I must have seen at least once. I admit I am a little hasty in
generalizing sometimes, but never from zero samples :-)
 
Click event allows you to separate the situation where a user types a value
into a combobox instead of selecting it from the dropdown list. You could
use the Click event to identify when a selection is made versus a manual
entry.

--

Ken Snell
<MS ACCESS MVP>

Bas Cost Budde said:
That is what I am suggesting indeed... be noted I decided this quite some
time ago, and never tried AfterUpdate since. Let's test. [fumbles] No, no
difference.

Which leaves me with other questions, more Why-typed. What could be the
use of a Click event for a combobox? It fires along with AfterUpdate.
Could there be some setting where AfterUpdate indeed doesn't fire? As I
suspect I must have seen at least once. I admit I am a little hasty in
generalizing sometimes, but never from zero samples :-)

Rick said:
Are you suggesting that AfterUpdate doesn't work with an unbound
ComboBox? Or is there some other difference?
 
FYI The final code. I was quite surprised that it was as simple as it was.

Private Sub cboClient_AfterUpdate()

If DCount("lngRecordId", "tblTransferGuests", "lngClientId = " & _
Me.cboClient & " AND lngTransportId = " & Me.lngTransportID) = 1 Then

MsgBox$ ("'" & Me.cboClient.Column(1) & "' has already been attached to
this reservation.")
Cancel = True
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
End If

End
 
I'm afraid that doesn't work. I type in a value and the Click event
fires nevertheless.
 
Cancel=true? There is no Cancel parameter for AfterUpdate. Instead of
DoMenuItem you could to cboClient.Undo (which I'd prefer, if only out of
legibility)
 
My apologies.... you're correct, of course... AfterUpdate event occurs, and
then the Click event.... well, back to school to relearn what I've been
thinking...
 
Back
Top