Daryl,
Thanks for the advice on setting index properties - it's not
something that I've ever done; I normally simply set the Indexed
property to one of the options available on the General tab of the
table's property sheet. However, changing this to Ignore Nulls has
no effect.
Setting a breakpoint shows that the message is triggered when the
Sub ends (ie. after the End Sub statement, which is immediately
after the End If statement).
Checking the table after entering a valid CAR number in the textbox
confirms that the record is not added at this time. I'm completely
puzzled as to why the validation message is occurring when the
record should not be being saved.
In desperation, thinking that maybe the textbox had previously had a
validation rule applied to it in a previous life (it's likely to be
a copy of a copy of a copy ..., since I often generate new forms
from something similar (perhaps even in a different database) and
simply change names and recordsources), I deleted the textbox and
added a new one (directly form the toolbox, giving it a different
name) which I then bound to the CARNumber field. I entered the same
code for this new control - which certainly has never had any
validation associated with it - and found exactly the same behaviour.
I've now implemented a work-around, as follows:
Dim ValidData as Boolean 'Set module-level variable
Private Function ValidData() As Boolean
ValidData = True
If Not (Text27 Like "C####") Then
ValidData = False
MsgBox "Error"
Text27.SetFocus
End If
End Function
And finally, in the form's cmdAddCAR button (the only way out which
saves the record - there's also a Cancel button which has Me.Undo
and then closes the form):
Private Sub cmdAddCAR_Click()
If ValidData Then DoCmd.Close acForm, "frmAddCAR"
End Sub
This code works as I expect - no validation message occurs. But I'm
still deeply puzzled.
Rob
Daryl said:
Rob -
As for the ignore nulls, bring up the table design and look at the
indexes. That is where you can set them to be unique (though the key
field is automatically set this way). This is also where the
'ignore nulls' is. You may want to validate the other indexes
while you are there, as Access will build indexes automatically if
you copy/paste some types of fields from one table to another.
I would also add a breakpoint on your BeforeUpdate event and step
through the code to find out where the error message comes from.
There may be something else happening that we aren't thinking of.
Hii Daryl,
Thanks for the response. That's maybe on the right track, but I'm
still not sure exactly what's happening, or how to fix it. If I
comment out the .Undo line I still have the same error. [FYI:
running Access 2003, SP3]. The form is opened for a new entry
record (for details, see my response to Jeff Boyce in another
branch of this thread), via: DoCmd.GoToRecord , , acNewRec
in the form's Open event.
I can let the user retype the entry, but I'm trying to clear the
textbox control before that. And, as I say, removing the
txtCARDocNumber.Undo
statement doesn't prevent the error.
I don't understand why this code, and the error message, would be
generated from the BeforeUpdate event of a form control - not the
BeforeUpdate of the form itself. That event should not to be
trying to save the record - which is when I would expect an error
to arise from a table property (Required field, duplicates not
allowed). I would expect that to occur (if the texbox control,
bound to the field, contains erroneous data) when an event which
causes the record to be saved occurs - such as closing the form.
I'm also puzzled by your statement "... set the 'ignore nulls'
property of the index." I can't find this property anywhere in the
table's property dialog; how can I set this (I'm using an Access
.mdb back-end, not a SQLServer file).
Further comments/explanation welcomed,
Rob
Daryl S wrote:
Rob -
One thought - is your txtCARDocNumber.Undo statement making this
field null? If so, you would be violating the unique index. Do
you need to undo this, or can you let the user re-type it? If
you want the nulls allowed as values, then you can set the
'ignore nulls' property of the index.
I have a form with a textbox control bound to a table field. The
table has no validation properties set for this field; the field
is indexed with 'No Duplicates'. The textbox itself has no
validation properties set for the field. I have the following
code in the textbox's BeforeUpdate event:
If Not (txtCARDocNumber Like "C####") Then
MsgBox "The CAR Number must be in the format 'Cnnnn'," &
vbNewLine _
& "where n is any numeric character." _
, vbExclamation + vbOKOnly, "CAR Number Format
Incorrect ..."
Cancel = True
txtCARDocNumber.Undo
End If
If I enter an invalid entry into the textbox, my message box
appears. But I then get a message from Access "The value in the
field or record violates the validation rule for the record or
field. ..." Why is this happening, and how can I prevent it.
I've tried adding DoCmd.SetWarnings False
before the If statement, but that has no effect.
TIA,
Rob
.
.
.