The first thing you should examine is whether the data input can be
controlled by referential integrity. If for instance the data is a company
name in a customers column in an orders table and you want to ensure that
only a valid company name is entered then you should have a Customers table
with one row per customer and enforce referential integrity so that only
values which exist in the customers table can be entered in the Orders table.
In fact a numeric CustomerID foreign key column referencing a numeric
CustomerID primary key of the Customers table would be better as names are
not necessarily unique. Data entry of the foreign key value would normally
be via selection form a combo box's list.
If you can't enforce a valid value by referential integrity then a
validation rule might suffice if you can devise a suitable expression which
ensures that the data entered satisfies the rule.
If the validation criteria are too complex to handle it easily with a
validation rule then you might be able to put some validation code in the
BeforeUpdate event procedure of the control on your data input form (data
should only ever be entered via a form, never in raw datasheet view). If the
value entered does not meet the validation criteria set the return value of
the Cancel argument to True.
If you can't validate the data at the first time of entry and want to adopt
your 'double entry' approach then you could try putting the following code in
the control's BeforeUpdate event procedure. It uses Static variables to keep
track of what's been entered and whether it’s the first time or not:
Static i As Integer
Static strVal As String
Dim ctrl As Control
Set ctrl = Me.ActiveControl
If Not IsNull(ctrl) Then
If i = 0 Then
i = 1
strVal = ctrl
MsgBox "Please enter data again.", vbInformation, "Confirm"
Cancel = True
ctrl.Undo
Else
i = 0
If ctrl <> strVal Then
MsgBox "Values entered do not match. Try again",
vbExclamation, "Warning"
Cancel = True
ctrl.Undo
End If
End If
End If
This will carry on forcing the user to enter the value until the same value
has been entered twice in succession.
Ken Sheridan
Stafford, England