Closing a form without data

Novice1

Registered User.
Local time
Yesterday, 20:37
Joined
Mar 9, 2004
Messages
385
I have a button with code. I want to close the record if there isn't any data. If there is data I want to delete the record. Is there a more efficient way than what I have below? I don't like the first If statement.


On Error GoTo Error_Handler

If IsNull([Grade]) And IsNull([FName]) And IsNull([LName]) Then

DoCmd.Close
DoCmd.OpenForm "frmMainPageCustomers", acNormal, "", "", , acNormal
DoCmd.SetWarnings (True)

Else

DoCmd.SetWarnings (False)
DoCmd.RunCommand acCmdDeleteRecord

DoCmd.Close
DoCmd.OpenForm "frmMainPageCustomers", acNormal, "", "", , acNormal
DoCmd.SetWarnings (True)

End If

Error_Handler_Exit:
On Error Resume Next
Exit Sub

Error_Handler:
MsgBox Err.Description
Resume Error_Handler_Exit
 
You can't close the form 1st. When the form vanishes, so does the code.
Close the form last.

If the record has a key,run the delete, then close.
 
You can't close the form 1st. When the form vanishes, so does the code.

Not really. You would want to have any code that does something that requires the form to be open but opening another form doesn't and works fine.
 
In both cases you will close the form, right, so no need to repeat that block, so you could do...
Code:
    If Len(Me.Grade & Me.FName & Me.LName) > 0 Then
[COLOR="Green"]        'delete the record if there is data in fields, above[/COLOR]
        DoCmd.SetWarnings False
        DoCmd.RunCommand acCmdDeleteRecord
        DoCmd.SetWarnings True
    End If
    
    DoCmd.Close acForm, Me.Name
    DoCmd.OpenForm "frmMainPageCustomers"
 

Users who are viewing this thread

Back
Top Bottom