Delete/Undo code if no records (1 Viewer)

Dairy Farmer

Registered User.
Local time
Today, 09:30
Joined
Sep 23, 2010
Messages
244
Need help finishing this code please. Access 2k7.

Existing record - Deletes (working)
New record, dirty - Undo (working)
New record, no data - Undo (working)

No records - goes to "Existing record"

Code:
Private Sub Button_Delete_Click()
Dim Msg, Style, Title, Response, MyString
Dim Cancel As String
'Existing record
    If (Not Me.NewRecord) Then
 
        Msg = "Are you sure you want to delete this entry?" & vbNewLine & "This CANNOT be undone!"
        Style = vbOKCancel + vbQuestion + vbDefaultButton2
        Title = "Delete An Entry"
        Response = MsgBox(Msg, Style, Title)
            If Response = vbCancel Then
                MyString = "Cancel"
                Cancel = True
                DoCmd.GoToControl "ProblemDate"
            Else
                MyString = "OK"
                DoCmd.RunCommand acCmdDeleteRecord
                Me.AllowAdditions = False
            End If
    End If
'New record dirty
    If (Me.NewRecord And Me.Dirty) Then
                Msg = "Are you sure you want to discard this entry?"
                Style = vbOKCancel + vbQuestion + vbDefaultButton2
                Title = "Discard An Entry"
                Response = MsgBox(Msg, Style, Title)
            If Response = vbCancel Then
                MyString = "Cancel"
                Cancel = True
                DoCmd.GoToControl "ProblemDate"
                 Me.AllowAdditions = False
            
            Else
                MyString = "OK"
                DoCmd.RunCommand acCmdUndo
                Me.AllowAdditions = False
            End If
    End If
'New record no data
    If (Me.NewRecord And Not Me.Dirty) Then
            Me.AllowAdditions = False
    End If
End Sub
 

vbaInet

AWF VIP
Local time
Today, 07:30
Joined
Jan 22, 2010
Messages
26,374
I've not really scrutinised your code but wouldn't you put an Exit Sub for each delete type? Or maybe you want the code to continue to the next.

Re your main question, I don't understand what this means, "No records - goes to Existing record". Can you explain what it should do?
 

Dairy Farmer

Registered User.
Local time
Today, 09:30
Joined
Sep 23, 2010
Messages
244
Re your main question, I don't understand what this means, "No records - goes to Existing record". Can you explain what it should do?

Sorry, I had posted some more detail, but got disconnected and had to retype.

The sub forms properties are AllowAdditions = No. The user has to click on New Record button to start a record.

When selecting an item in the List Box in the main form, there may be no records in the sub form. Hence, if the user uses the delete button they get the "If (Not Me.NewRecord) Then" message.

No record <> New Record
No record <> New Record and Dirty
No record <> New Record and no data

I would like it to ignore if there are no records.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 07:30
Joined
Sep 12, 2006
Messages
15,658
if there are no records then there is no newrecord

hence the first test (not me.newrecord) is true, and thats where it goes)


i would trap no records case - something like

if nz(me.id,0)=0 then exit sub
 

Dairy Farmer

Registered User.
Local time
Today, 09:30
Joined
Sep 23, 2010
Messages
244
Re: Delete/Undo code if no records - Solved

Thanks. It works fine now.

Code:
Private Sub Button_Delete_Click()
Dim Msg, Style, Title, Response, MyString
Dim Cancel As String
    If (Not Me.NewRecord [B][COLOR=red]And Nz(Me.IDTreatmentProblem, 0) <> 0[/COLOR][/B]) Then
.......

Goto learn how to use NZ and remember to use it.
 

Users who are viewing this thread

Top Bottom