Problem with close button

T

tom

I have designed a form to be used by data entry person. I want to
eliminate the standard menu navigation buttons. I have created a
Close button that will close the form and thus save the record. The
date field is indexed with no duplicates so that a duplicate date
can't be entered. Here is the code in the Unload Event of the form:

If DCount("[IndexedFieldName]", "TableName") > 1 then MsgBox "Indexed
Field name is a duplicate. Change the value." Cancel = True
Me![IndexedFieldname].SetFocus End If

When a duplicate date is entered and the Close button is clicked the
Msg Box does pop up. However when the "ok" button inside the message
box is clicked it generates a run-time error 2169.

Is there another way to accomplish this? What am I doing wrong? Any
help is appreciated.
 
T

TC

If the data field has an index that does not allow duplicates, Access will
automatically throw an error (end prevent the entry of the new record) if
the user tries to enter a duplicate. You don't need any code for that, at
all. And if you >did< code that check yourself, you would not put it in the
Close button - that's too late. You'd put it in the form's BeforeUpdate
event. That's the event for checking things at the last possible moment
before the new record is saved to the table.

HTH,
TC
 
J

JasonS

Hi!
If your form is bound to the table, simply use Button Wizard from Toolbox.
Choose Rercord Operations --> Save Record.
Then click Button's properties, and open code designed by Wizard for onClick
event.
It probably has got Sub.... End Sub
Put this line one line above "End Sub":
DoCmd.Close
It will save your current record, and then will close the form.
 

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