Form Level Validation - Closing (1 Viewer)

Dairy Farmer

Registered User
Joined
Sep 23, 2010
Messages
244
I am using Form Level Validation and all works well except:

When adding a new record the user is prompted if they would like to add a new record or not. Cancel works fine.

If OK then a new record is created and the user can enter details. The problem comes in if they then decide not to add the record. The user is prompted to fill in the missing data, but how can I give them the option to discard the record completely? Dont want the user to have to use the Esc key.
 

Attachments

vbaInet

AWF VIP
Joined
Jan 22, 2010
Messages
26,374
Haven't had a look at your db but you can check if it's a new record using Me.NewRecord and act accordingly and if necessary plus Docmd.RunCommand accmdundo.
 

DCrake

Remembered
Joined
Jun 8, 2005
Messages
8,634
You may also have to go don the route of using unbound forms, you have more control over what goes on. The main difference is that you use the AfterUpdate event instead of the BeforeUpdate event.
 

Dairy Farmer

Registered User
Joined
Sep 23, 2010
Messages
244
Re: Form Level Validation - Closing (Solved)

Thank I got it now.
Code:
Private Sub Button_Close_Click()
    Button_Save_Click
    DoCmd.Close
End Sub

Private Sub Button_Save_Click()
Dim Cancel As String
'Code added in red
[COLOR=red]Dim Msg, Style, Title, Response, MyString
[/COLOR][COLOR=red]If Me.NewRecord Then[/COLOR]
[COLOR=red]    Msg = ("You have not entered an Animal Number." & vbNewLine & _
    "Do you want to discard this entry?")
    Style = vbYesNo + vbExclamation + vbDefaultButton2
    Title = "Missing Data"[/COLOR]
[COLOR=red]Response = MsgBox(Msg, Style, Title)[/COLOR]
[COLOR=red]If Response = vbYes Then
    MyString = "Yes"
    DoCmd.RunCommand acCmdRecordsGoToPrevious[/COLOR]
[COLOR=red][/COLOR] 
[COLOR=red]Else[/COLOR]
[COLOR=red]    MyString = "No"
    Cancel = True
[/COLOR]    
    If Len(Me.AnimalNumber & vbNullString) = 0 Then
        MsgBox "An Animal Number is required", vbExclamation, "Missing Data"
        Cancel = True
        Me.AnimalNumber.SetFocus
        
    ElseIf Len(Me.AnimalSex & vbNullString) = 0 Then
        MsgBox "Sex is a required field", vbExclamation, "Missing Data"
        Cancel = True
        Me.AnimalSex.SetFocus
'More code below, but not shown
    ElseIf Len........
 
Else
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Requery "AnimalList"
    End If
[COLOR=red]    End If
[/COLOR][COLOR=red]    End If[/COLOR]
End Sub
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom