Record Saving Controls

  • Thread starter Thread starter JohnWL
  • Start date Start date
J

JohnWL

Here's a general question for consideration. I've used two approaches to
this. I want to control the data entered into a record via form so that it
only gets added to the table if it has valid data in certain fields.
Sometimes, using the Required flag in the table def is not desirable - just
causes other problems. I want to prevent getting a table with lots of
irrelevant records that need to be deleted.

1. If I build in my own checks and balances, it works fairly well, but gets
more and more complex as I add sub-forms and tabs and calculated fields and
so on to the form. I've tried keeping a module variable that tracks whether
the data has changed anywhere on the form, and prevent closing the form or
moving to another record unless those changes are either explicity Saved or
Undone via command buttons. But one form got so complex, I was spending way
too much time trying to keep this method working reliably. And I had to
capture keystrokes to prevent using the Esc key, disable the Close box, etc.

2. If I use Access 2002 built-in features without the above kind of
safeguards, any partial record is automatically added to the table if the
user moves to another record or closes the form. Thus, a corrupted table.
I'm trying to use te form's Dirty property, but that raises new challenges.
For example, code sets some default values in certain fields when a new
record is added. That sets the Dirty property to True, so I have to set it
False. A pop-up form allows a user to enter some calculations and post the
results to the previous form, but that doesn't activate the Dirty property
reliably. I prefer the simplistic approach of Access built-in features and
design, but its allowing too many faulty records to be recorded.

How do you control data entry in a complex application so that records are
guaranteed valid in certain fields? Using the Required data and field
validation don't seem to provide the total solution - they often create
other problems. Any suggestions based on your experience?

John Loewen
 
Why not use the Form Before Update Event to validate data at the form level
instead of validating at the field level?

Private Sub Form_BeforeUpdate(Cancel As Integer)
If ValidityCheck() Then
'all data is valid
Else
' there is a problem, therefore do not allow the save and keep the user
on the offending record.
Cancel = True
End If
End Sub

Private Function ValidityCheck() As Boolean
ValidityCheck = True
IF isnull(me.somefield) then
' assuming you do not want to allow nulls in this field
ValidityCheck = false
' maybe pop up an error message and place the cursor in me.somefield
end if

IF me.SomeNumber < 10 then
ValidityCheck = False
' again maybe pop up an error message and/or place the cursor in
me.somenumber
end if
End Function
 
Back
Top