Closing form without saving if field empty

Nyanko

Registered User.
Local time
Today, 13:24
Joined
Apr 21, 2005
Messages
57
Hi,

I have a main/sub input form and have a close button that should check if a specific field has been entered on the sub form - if it is empty then the record is not saved.
I have the following existing code :

Control for the Close button
Code:
Private Sub btn_CloseForm_Click()
'------------------------------------------------------------
' CONTROL FOR CLOSE BUTTON
'------------------------------------------------------------
On Error GoTo btn_CloseForm_Click_Err

    'Call the macro to check for empty records
    Forms!frm_MainInput.SetFocus
    EmptyRecordCheck

btn_CloseForm_Click_Exit:
    Exit Sub

btn_CloseForm_Click_Err:
    MsgBox Error$
    Resume btn_CloseForm_Click_Exit
End Sub

Empty Record Check Module
Code:
Function EmptyRecordCheck()
'------------------------------------------------------------
' Don't Save Empty Records
' If there is no Project Number and the form is closed then record will not save
'------------------------------------------------------------
On Error GoTo EmptyRecordCheck_Err

    'Check if the subform field "ProjectCode" is empty when the MainInput form is closed
    If (IsNull(Forms!frm_MainInput!frm_subRowDetail.Form!FormProjectCode)) Then
        'Then do not save a record (undo)
        DoCmd.RunCommand acCmdUndo
        Beep
        MsgBox "No Project Code entered - this record will not be saved", vbOKOnly, "Empty Record"
    End If

    'Close the form
    DoCmd.Close , ""

EmptyRecordCheck_Exit:
    Exit Function

EmptyRecordCheck_Err:
    If Err.Number <> 2501 Then
    MsgBox Error$
    End If
    Resume EmptyRecordCheck_Exit

End Function

I've started getting the message that "the command or action'Undo' isn't available now

I can't see why this won't work - any help ?
 
Ahhh I think this is because it's not a new record in theview, but an existing one that seems to have snuck in without the check.
How can I amend the above to either undo or delete the viewed record if the ProjectCode field is empty ?
 
Something like the below, remember to replace the red marked with your name. Remark code not tested

Code:
Private Sub btn_CloseForm_Click()
'------------------------------------------------------------
' CONTROL FOR CLOSE BUTTON
'------------------------------------------------------------
On Error GoTo btn_CloseForm_Click_Err

    'Call the macro to check for empty records
    If IsNull(Me.frm_subRowDetail.Form!FormProjectCode) Then
      If Me.Dirty Then
        Me.Undo
      Else
        CurrentDb.Execute ("Delete * from [B][COLOR=Red]YourTableName[/COLOR][/B] Where [[B][COLOR=Red]AFieldName[/COLOR][/B]]=" & Me.[B][COLOR=Red]TheRecordIDControlName[/COLOR][/B])
      End If
    End If
btn_CloseForm_Click_Exit:
    Exit Sub

btn_CloseForm_Click_Err:
    MsgBox Error$
    Resume btn_CloseForm_Click_Exit
End Sub
 

Users who are viewing this thread

Back
Top Bottom