prevent deletion of record

S

Sue

I have a form that displays previous year's info at top w/ space to enter new
info current year info at bottom. I would like to prevent the deletion of
the record if there is a value greater than '0' in the previous yr value.
There is a deletion button on the form.

Suggestions...
 
M

Marshall Barton

Sue said:
I have a form that displays previous year's info at top w/ space to enter new
info current year info at bottom. I would like to prevent the deletion of
the record if there is a value greater than '0' in the previous yr value.
There is a deletion button on the form.

I think I would disable the delete button if the prev yr
field >0 using the form's Current event:

Me.btnDelete.Enabled = (Me.[previous yr] > 0)
 
K

Klatuu

In the Current event of the form, manipulate the enabled property of the
delete command button:

Me.cmdDeleteButton.Enabled = Nz(Me.PreviousYrValue,0) > 0
 
S

Sue

I already have an Event Procedure controling the change of another field in
the On Current property. Can i add another line to that On Current Event
Procedure?
 
S

Sue

I already have an Event Procedure controling the change of another field in
the On Current property. Can i add another line to that On Current Event
Procedure?

Marshall Barton said:
Sue said:
I have a form that displays previous year's info at top w/ space to enter new
info current year info at bottom. I would like to prevent the deletion of
the record if there is a value greater than '0' in the previous yr value.
There is a deletion button on the form.

I think I would disable the delete button if the prev yr
field >0 using the form's Current event:

Me.btnDelete.Enabled = (Me.[previous yr] > 0)
 
G

George Nicholson

Forms have a Delete event, which can be cancelled. Here is a modification of
the example in VBA Help that might work for you:

Private Sub Form_Delete(Cancel As Integer)
If Me.PreviousYear <> 0 Then
Cancel = True
MsgBox "This record can't be deleted."
End If
End Sub

Forms also have BeforeDelConfirm & AfterDelConfirm events, but those only
fire if you have Tools>Options>Edit>ConfirmRecordChanges turned On, which is
frequently not the case.

HTH,
 
K

Klatuu

George's method will work, but to disable the command button, my original
suggestion would be the way to go.
Yes, you can have as many lines as you need in any event.
 
M

Marshall Barton

Yes, you can add lots of stuff to a procedure. This one
line is quite independent of most things so it should not
intefere with any thing else. The one exception would be if
your delete button has the focus, in which case you would
need to set the focus to some other control before disabling
the button.
--
Marsh
MVP [MS Access]

I already have an Event Procedure controling the change of another field in
the On Current property. Can i add another line to that On Current Event
Procedure?

Marshall Barton said:
I think I would disable the delete button if the prev yr
field >0 using the form's Current event:

Me.btnDelete.Enabled = (Me.[previous yr] > 0)
 
G

George Nicholson

There are *several* ways to delete a record (Edit>DeleteRecord, Ctrl +
KeypadMinusKey, etc.). Disabling the command button only covers one of them.
Granted, coding the Delete event only works while in Form view, but it
covers all the possibilites while in Form view.

Having the command button disabled might be less confusing for the user, but
that's only cosmetic unless you've turned off menus and are trapping
keystrokes. To protect the data while in Form view you need to code the
Delete event (or set the Form to not allow deletions).

HTH,
 
Top