Creating a validation routine for multiple forms using an array

D

DawnTreader

Hello All

i have 6 forms that i need to validate when people enter in a new record to
those forms. when the user "saves" the record and closes the form i want to
do a double check to make sure that fields are not empty and have the correct
data in them. for instance i want to make sure that dates are entered as to
when the record happened, and some records require 2 dates that must be in
sequence.

i have been doing this on an individual form basis, but i am looking at the
amount of things i am having to code and wondering if there is a way to mark
fields on a form as part of the validation process and then pass the fields
and the data to a public routine or function that would figure out the
validity of the record and then get the user to fix the data.

i was thinking that if my function had a variant variable that was passed by
the form with the fields that were required then the function would check to
see if they were null and then either it would tell the user what fields were
missing data or send back a "go ahead, save and close" to the form.

i thought that this would save me code, but i dont know if it is a good idea
or how to go about it if it is a good idea.

any suggestions?
 
D

Dale_Fye via AccessMonster.com

Never tried this, but this is how I would approach it.

1. In each forms BeforeUpdate event, call the function, passing it the forms
name and a control variable (which would be used by the function to identify
the control to set the focus to upon return).

It might look like:

Private Sub Form_BeforeUpdate(Cancel as Integer)

Dim ctrl as control

Cancel = ContainsNullOrBlank(me, ctrl)
if Cancel = true then
msgbox "The control '" & ctrl.name & "' requires a value!"
ctrl.setfocus
endif

End Sub

Then the function ContainsNullOrBlank would look something like:

Public Function ContainsNullOrBlank(frm as Form, ByRef ctrl as Control) as
Boolean

Dim frmCtrl as control

'Use the Tag property of the controls that you want to prohibit Nulls or
Blanks
for each frmCtrl in frm.Controls
if instr(frmCtrl.Tag, "NoNulls") > 0 Then
if Len(frmCtrl.Value & "") = 0 then
set ctrl = frmCtrl
ContainsNullOrBlank = True
Exit Function
endif
endif
Next
ContainsNullOrBlank = false

End Function

Unfortunately, this would not work for a multi-select list box (since it does
not have a "value"), but you could add some code to function
ContainsNullOrBlank to check if the control is a multi-select list, and if so,
check to see whether the frmCtrl.ItemsSelected.Count property is > 0.

HTH
Dale
 
D

DawnTreader

Hello Dale

Wow. that is fantastic and a little creepy...

i was preparing for this idea by going to the forms and putting tags in the
controls "NoNull"...

i guess great minds think alike.

your code looks great, i have yet to try to implement it. will post the
final result. thanks again! :)
 

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