validate a form

M

Microsoft

Access 2007

I have a form that has 18 combobox controls all named starting with
cbx......

I want to be able to validate all the controls to make sure that all of them
have had an item chosen from the list as soon as one is found that hasn't
been validated the code would break and return to the offending combobox.

Is it possible to loop through all the controls rather than writing code for
each individual one?

If yes, please could you tell me how to do it as I have not done this
before.

Thanks
 
D

Dale Fye

Sure. I'm not sure what order they will be handled in, probably the order
they were placed on the form, rather than the tab order. Something like this
should work. You could also enter the message you want to display as the Tag
property of the combo box, then instead of displaying a standard message, you
could display the text stored in the tag property

Private sub Form_BeforeUpdate(Cancel as Integer)

Dim ctrl as control

For each ctrl in me.controls

'you could check for the control type (111 for combo box) or the
control name
if left(ctrl.name, 3) = "cbx" then
if len(ctrl.value & "") = 0 then
msgbox "Enter value for the control"
'msgbox ctrl.tag 'for a less generic message
ctrl.setfocus
Cancel = true
exit sub
endif
endif
Next ctrl

End Sub

HTH
Dale
 
M

Microsoft

Thanks for this.
There is one little problem in that when I try to set the focus back with
the control that is incorrect it says it cannot do it.

What do I need to do

Thanks again for your help
 

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