Question Prompt on close and don't save the null record

hasanrazaj1

Registered User.
Local time
Tomorrow, 01:48
Joined
Jun 3, 2009
Messages
20
Slaam to all.
hey everyone.

i have form with some txt fields and close button.

i want that when user click on close button then access generate a message with yes and no button.

which is such as" Are you sure to close Application" vb yes or no
when use press yes then access check only two fields for null and if both or one null then record not saved in table all things work good but only saving issued not solved. for this purpose i used cancel = true if msgbox = vbyes but it save the record even both fields are empty. what problem with code I cann't understand.

the code behind close button is :

Code:
Private Sub cmdClose_Click()
Dim strmsg As String
On Error GoTo Err_cmdClose_Click

strmsg = "Are you sure to close Application"

If MsgBox(strmsg, vbQuestion + vbYesNo, "Close Application") = vbYes Then
    If IsNull(Me.WithDrawAmount) Or IsNull(Me.Description) Then
        MsgBox ("Record will be not saved")
        End If
Cancel = True
DoCmd.Close acForm, "WithDrawSpecial"
Else
    If IsNull(Me.WithDrawAmount) Or IsNull(Me.Description) Then
    Me.WithDrawAmount.BorderColor = vbRed
    Me.Description.BorderColor = vbRed
    End If
End If

Exit_cmdClose_Click:
    Exit Sub

Err_cmdClose_Click:
    MsgBox Err.Description
    Resume Exit_cmdClose_Click
    
End Sub

any ideas, suggestion, will be appreciated.
 
Code:
Private Sub cmdClose_Click()
Dim Msg, Style, Title, Response, MyString
Dim Cancel As String
    If ((Nz(Me.WithDrawAmount, 0) = 0) Or (Nz(Me.Description, 0) = 0)) Then
 
        Msg = "Are you sure you want to discard this entry?"
        Style = vbOKCancel + vbQuestion + vbDefaultButton2
        Title = "Quit Application"
        Response = MsgBox(Msg, Style, Title)
            If Response = vbCancel Then
                MyString = "Cancel"
                Cancel = True
                Me.WithDrawAmount.BorderColor = vbRed
                Me.Description.BorderColor = vbRed
                DoCmd.GoToControl "WithDrawAmount"
 
            Else
                MyString = "OK"
                If (Me.NewRecord And Me.Dirty) Then
                    DoCmd.RunCommand acCmdUndo
                 DoCmd.Close acForm, "WithDrawSpecial"
                
                ElseIf (Me.NewRecord And Not Me.Dirty) Then
                    DoCmd.Close acForm, "WithDrawSpecial"
                End If
            End If
    Else
            DoCmd.Close acForm, "WithDrawSpecial"
    End If
End Sub

I take it you will have some kind of proceedure should an existing record have Field1 or Field2 deleted.
 
Last edited:
this code works good. i have a main form "Account" where i click on cmdWithDrawAmount button to open withdrawspecial form for amount entry in concern account record. withdrawspecial form open using openArgs properity for opening in same record as in "Account".

Form withdrawspecial also have some other fields like accountid which filledup automically by Account OpenArgs Properity. TranscationID (autonumber) field, WithDrawAmount field and Description fields.

i want that when user open withdrawspecial form for amount entry but he click on close button to close the form without filling WithDrawAmount field and Description field then access generate message box with yes or no button. if user click on yes then form close without saving the records and if he/she click on no then both fields border get red.

is it impossible to prevent saving the record if only two above said fields empty.
 
If you put this code in the Close Button on form WithDrawSpecial that is what it does.
Code:
Private Sub cmdClose_Click()
Dim Msg, Style, Title, Response, MyString
Dim Cancel As String
[COLOR=red]'Check and see if either fields are blank.[/COLOR]
    If ((Nz(Me.WithDrawAmount, 0) = 0) Or (Nz(Me.Description, 0) = 0)) Then
[COLOR=red]'Either or both fields are empty so prompt the user.[/COLOR]
        Msg = "Are you sure you want to discard this entry?"
        Style = vbOKCancel + vbQuestion + vbDefaultButton2
        Title = "Quit Application"
        Response = MsgBox(Msg, Style, Title)
[COLOR=red]'User clicks the Cancel button and is returned to the form.[/COLOR]
            If Response = vbCancel Then
                MyString = "Cancel"
                Cancel = True
[COLOR=red]'Fields are highlighted.[/COLOR]
                Me.WithDrawAmount.BorderColor = vbRed
                Me.Description.BorderColor = vbRed
[COLOR=red]'Goes to field WithdrawAmount.[/COLOR]
                DoCmd.GoToControl "WithDrawAmount"
 
            Else
[COLOR=red]'User click on OK to discard changes.[/COLOR]
                MyString = "OK"
[COLOR=red]'The record is new.[/COLOR]
[COLOR=red]'Record is dirty (has data entered but not yet saved).[/COLOR]
                If (Me.NewRecord And Me.Dirty) Then
[COLOR=red]'Undo any changes and close the form.[/COLOR]
[COLOR=red]'Record not saved.[/COLOR]
                 DoCmd.RunCommand acCmdUndo
                 DoCmd.Close acForm, "WithDrawSpecial"
[COLOR=red]'Record is new but not data has been entered (not dirty).[/COLOR]                
                ElseIf (Me.NewRecord And Not Me.Dirty) Then
[COLOR=red]'Just close the record. Yhere is nothing to save.[/COLOR]
                    DoCmd.Close acForm, "WithDrawSpecial"
                End If
            End If
    Else
[COLOR=red]'Both fields have something in them.[/COLOR]
[COLOR=red]'Close the form.[/COLOR]
[COLOR=red]'Changes are saved automatically.[/COLOR]
            DoCmd.Close acForm, "WithDrawSpecial"
    End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom