I'm making some progress -- by adding the BeforeUpdate code you listed below,
I'm eliminating the possibility of adding blank records to the table. But
now it's getting really difficult to close my form if you instead decide you
don't want to add any records after all. Let me give you some more details
about my form -- it seems like it should be pretty simple, but I sure am
having a tough time getting it to do what I want.
I removed the validation properties from the field in the underlying table,
it seemed that the beforeUpdate event was more effective in preventing blank
records.
The form has a Store Field and a number of checkboxes, all bound to fields
in a table. There is an add button and a close button. Code for the add
button is simply
DoCmd.RunCommand acCmdRecordsGoToNew
The close button's code is:
DoCmd.Close acForm, "frmRsales"
There is also some fixing up of the store field that I need to do. It's a
five character field, but if the user enters less than 5 characters, I add
leading 0's in the textbox's
exit event:
Me.STORE.Value = Format(Me.STORE.Value, "00000")
If I click the add button and then the done button, I get the need store
number message from the BeforeUpdate event, and then the form closes. I'd
rather not get the error message here since we're not trying to save the
record.
If I click the add button twice in succession, first I get the need store
number error message, and then an access error message -- run time error
2501: the run command action was cancelled, from the code in the add button
Click event.
Thanks for any suggestions on how to get this to work better.
Beetle said:
In the table set the Required property of the field to Yes and you may
also want to set the Allow Zero Length property to No.
In the forms Before Update event you could use code like the following
as another level of validation;
Private Sub Form_BeforeUpdate (Cancel As Integer)
If Nz([Store], "") = "" Then
MsgBox "You must enter a Store value"
Cancel = True
Me![Store].SetFocus
End If
End Sub
Replace [Store] with the actual name of your Store field
--
_________
Sean Bailey
:
Thanks for the quick reply. The validation code IS in the associated field
in the underlying table.
I'd rather not create a table for stores if it can be avoided. This is just
a quick application to do one very specific thing that just takes a few hours
a month. Keeping a store table up to date -- we have over 7,000 stores --
would add a lot of needless complexity to it. That is, if I can get it to
work properly without one.
:
On Wed, 12 Nov 2008 11:40:02 -0800, dgunning
I have created a form to use for entering new records. I open this form with
the dataEntry property set to true. There are text boxes bound to the field
values and then a button I created to add an additional record. (I want to
create a form that the user will be able to enter data without using the
mouse.) The event code for this add button is simply DoCmd.RunCommand
acCmdRecordsGoToNew.
Every record must have a value in the Store field, so I created a validation
rule for the store field - Is Not Null. But this isn't working -- if the
add button is clicked without filling in the Store value, a blank record is
added to the table. No error message is displayed. How can I get this
validation rule to work?
Put your validation code in the associated field in the
underlying table.
Or...
If your "Store" field is for values that answere a question
like "which store?", or "Store Number", make a table for
"Stores" and use it as the lookup source for a combobox on
your form. Set the limit to list value to Yes.