A better way to close main form?

G

Greg

On my main form (Customers) - Table (Customers) -- Primar Key (CustomerID) I
have a few command buttons. One of them is Create new customer
("DoCmd.GoToRecord , , acNewRec") and another one is exit command:
Dim smsg As String
smsg = "Do you want to exit?"
If MsgBox(smsg, vbQuestion + vbYesNo, "Exit Remedy?") = vbYes Then
DoCmd.Quit

Everything is working ok however if I select Create New Customer button and
change my mind I am not able to close this form because error message: Index
or primary key cannot contain a Null value

I can add Validation text and instruct user to close main window, but it
does not seems to me like a right way to do it. Is there is a better way to
close main form in case I change my mind and don't want to add new customer?
Thanks
 
D

Dirk Goldgar

Greg said:
On my main form (Customers) - Table (Customers) -- Primar Key
(CustomerID) I
have a few command buttons. One of them is Create new customer
("DoCmd.GoToRecord , , acNewRec") and another one is exit command:
Dim smsg As String
smsg = "Do you want to exit?"
If MsgBox(smsg, vbQuestion + vbYesNo, "Exit Remedy?") = vbYes Then
DoCmd.Quit

Everything is working ok however if I select Create New Customer button
and
change my mind I am not able to close this form because error message:
Index
or primary key cannot contain a Null value

I can add Validation text and instruct user to close main window, but it
does not seems to me like a right way to do it. Is there is a better way
to
close main form in case I change my mind and don't want to add new
customer?


This should only happen if you've "dirtied" the new customer record. Maybe
you have code that does that when you click your New Customer button, or
maybe you've left that out of your description of what happens. Anyway,
your Exit button chould check for the condition:

'----- start of example code -----
Dim smsg As String

smsg = "Do you want to exit?"

If MsgBox(smsg, vbQuestion + vbYesNo, "Exit Remedy?") = vbYes Then

If Me.Dirty Then
Select Case MsgBox( _
"The current record has been modified, but not " & _
"saved. Do you want to discard those changes?", _
vbQuestion+vbYesNoCancel,
"Record Has Been Changed!")
Case vbYes
Me.Undo
Case vbNo
Me.Dirty = False ' save changes
Case vbCancel
Exit Sub
End Select
End If

DoCmd.Quit

End If
'----- end of example code -----
 
G

Greg

Thanks

Dirk Goldgar said:
This should only happen if you've "dirtied" the new customer record. Maybe
you have code that does that when you click your New Customer button, or
maybe you've left that out of your description of what happens. Anyway,
your Exit button chould check for the condition:

'----- start of example code -----
Dim smsg As String

smsg = "Do you want to exit?"

If MsgBox(smsg, vbQuestion + vbYesNo, "Exit Remedy?") = vbYes Then

If Me.Dirty Then
Select Case MsgBox( _
"The current record has been modified, but not " & _
"saved. Do you want to discard those changes?", _
vbQuestion+vbYesNoCancel,
"Record Has Been Changed!")
Case vbYes
Me.Undo
Case vbNo
Me.Dirty = False ' save changes
Case vbCancel
Exit Sub
End Select
End If

DoCmd.Quit

End If
'----- end of example code -----

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
P

Pat Hartman

Let me add that this dirty test in a close button is especially important
because there is a bug in Access that causes it to silently loose updates if
there was an error writing to the table. You get no error message but the
data is not saved. Strangely, the "x" doesn't have this issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top