We help IT Professionals succeed at work.

How Can I Disable a Delete Button if The Form is Blank

gdunn59
gdunn59 asked
on
I have code to delete the current record on a form.  I want to be able to disable this button if the form is blank.

I have included the code for the record deletion.

How can I do this?

Thanks,
gdunn59
Private Sub cmdDelRec_Click()
On Error GoTo Err_cmdDelRec_Click
Dim rtn As String

DoCmd.SetWarnings False
    rtn = MsgBox("Are you sure you want to Cancel this record?", vbYesNo)
    If rtn = vbYes Then
        RunCommand acCmdDeleteRecord
        'DoCmd.GoToRecord , , acPrevious
        DoCmd.GoToRecord , , acNewRec
        Me.InquiryNum.SetFocus
        Me.Requery
    Else
        Cancel = True
        Me.InquiryNum.SetFocus
    End If

DoCmd.SetWarnings True

Exit_cmdDelRec_Click:
    Exit Sub

Err_cmdDelRec_Click:
    Cancel = True
    Resume Exit_cmdDelRec_Click
    
End Sub

Open in new window

Comment
Watch Question

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007
Commented:
Something like this:

Private Sub Form_Current()

If Me.NewRecord = True Or Me.RecordSetClone.Recordcount = 0 Then
  Me.cmdDelRec.Enabled = False
Else

Me.cmdDelRec.Enabled = False

End If

End Sub

mx
Commented:
DatabaseMX:

Shouldn't the following else statement be:

Else
Me.cmdDelRec.Enabled = True

instead of:

Else
Me.cmdDelRec.Enabled = False

When it wasn't a new record and/or blank, It didn't work until I changed it to "True".

Thanks,
gdunn59



DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
opps ... for sure ... TRUE !

mx

Author

Commented:
Ok.  Thanks!

Author

Commented:
Worked like a charm once I changed the Else Statement to True instead of False.  Thanks again
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
You are welcome ...

mx