Delete/insert confusion

  • Thread starter Thread starter Bingo
  • Start date Start date
B

Bingo

As an old-time database programmer, maybe I'm making some
presumptions that I shouldn't, but...

In my application, I have 'new' button to add a record. Underneath,
I'm doing "addnew." I want to be able to cancel the new record if the
use changes their mind. If I do a "me.recordset.delete" I get a "No
current record" message. But it still looks like it exists to me, at
least in the form. The mouse wheel is also causing an insert if I
scroll past the bottom, which is annoying.

This is also triggering a "beforeInsert" event. It shouldn't.

The main question is: How do I abort an insert and know that it is
(or would have been) a new record?

Currently, I'm using Jet, which seems fairly restrictive, but it's
what I'm stuck with.

TIA!
 
Hi Bingo

The new record is not actually saved until after the Form_BeforeUpdate event
has successfully occurred, so you can't delete something that hasn't yet
been created. If you want to trap updates to new (and existing) records
then Form_BeforeUpdate is the place to do it.

Me.Undo will undo changes to the current record, and setting Cancel=True in
Form_BeforeUpdate will cancel the update.

One method you might consider is using a module level boolean variable
fOkToSave. If you have a Save button on your form, then have it run this
code:
fOkToSave = True
Me.Dirty = False ' saves the record

If Form_BeforeUpdate, you can say:
If fOkToSave then
fOkToSave = False ' reset it
Else
Cancel = True
Me.Undo
End If

To modify the mouse wheel behaviour, check out Stephen Lebans' web site:
http://www.lebans.com/mousewheelonoff.htm

--
Good Luck!
Graham Mandeno [Access MVP]
Auckland, New Zealand

Return mail address is invalid in a vain attempt to reduce spam.
Feedback is welcome at: (e-mail address removed)
Please post new questions or followups to newsgroup.
 
Hi Bingo

The new record is not actually saved until after the
Form_BeforeUpdate event has successfully occurred, so you can't
delete something that hasn't yet been created. If you want to
trap updates to new (and existing) records then
Form_BeforeUpdate is the place to do it.

Me.Undo will undo changes to the current record, and setting
Cancel=True in Form_BeforeUpdate will cancel the update.

One method you might consider is using a module level boolean
variable fOkToSave. If you have a Save button on your form,
then have it run this code:
fOkToSave = True
Me.Dirty = False ' saves the record

If Form_BeforeUpdate, you can say:
If fOkToSave then
fOkToSave = False ' reset it
Else
Cancel = True
Me.Undo
End If

To modify the mouse wheel behaviour, check out Stephen Lebans'
web site: http://www.lebans.com/mousewheelonoff.htm

Okay, thanks!

I'm doing some of that, but I din't realize I could reset the
"Dirty" flag.
 
Hi Bingo

The new record is not actually saved until after the
Form_BeforeUpdate event has successfully occurred, so you can't
delete something that hasn't yet been created. If you want to
trap updates to new (and existing) records then
Form_BeforeUpdate is the place to do it.

Me.Undo will undo changes to the current record, and setting
Cancel=True in Form_BeforeUpdate will cancel the update.

One method you might consider is using a module level boolean
variable fOkToSave. If you have a Save button on your form,
then have it run this code:
fOkToSave = True
Me.Dirty = False ' saves the record

If Form_BeforeUpdate, you can say:
If fOkToSave then
fOkToSave = False ' reset it
Else
Cancel = True
Me.Undo
End If

To modify the mouse wheel behaviour, check out Stephen Lebans'
web site: http://www.lebans.com/mousewheelonoff.htm

Actually, I was right about Me.Dirty. I got "Setting isn't valid
for property" when I tried to set it.
 
Are you using a bound form or an unbound one?

If it is a bound form, you should definitely not be doing any .AddNew's or
..Delete's. Access will do all those for you.

HTH,
TC
 
Hi Bingo
Okay, thanks!

I'm doing some of that, but I din't realize I could reset the
"Dirty" flag.

It doesn't reset the Dirty property - it makes the current record non-dirty
by saving it.

It's actually shorthand for:
DoCmd.RunCommand acCmdSaveRecord

Now, if as you say:
Actually, I was right about Me.Dirty. I got "Setting isn't valid
for property" when I tried to set it.
.... it sounds like you're using an unbound form (a form without a
RecordSource), or perhaps a form that's based on a read-only query.

Which recordset are you actually working on - the form's underlying
recordsource, or another recordset that you have opened in code?

--
Graham Mandeno [Access MVP]
Auckland, New Zealand

Return mail address is invalid in a vain attempt to reduce spam.
Feedback is welcome at: (e-mail address removed)
Please post new questions or followups to newsgroup.
 
TC said:
Are you using a bound form or an unbound one?

If it is a bound form, you should definitely not be doing any
.AddNew's or .Delete's. Access will do all those for you.

HTH,
TC

Sorry for the delay!

This is a bound form. How do you add a record if not through
addnew? Move last? I.e. a user (a very dumb one) wants to add a
record and presses "New" and my code should do what?
 
Hi Bingo


It doesn't reset the Dirty property - it makes the current
record non-dirty by saving it.

It's actually shorthand for:
DoCmd.RunCommand acCmdSaveRecord

Now, if as you say:
... it sounds like you're using an unbound form (a form without
a RecordSource), or perhaps a form that's based on a read-only
query.

Which recordset are you actually working on - the form's
underlying recordsource, or another recordset that you have
opened in code?

I'm using a bound form with full write privledges.

I think Access is just different enough from Powerbuilder to drive
me insane (and it's a short trip).
 
Back
Top