subform variable

G

Guest

I have a function that receives a "Form" variable and loops through the
form's controls to make sure they're valid before moving on. I also want to
validate the controls on the form's sub form if a sub form exists. How do I
reference a form's sub form as a variable. A sample of my code is below:

*******************************************
For Each ctl In frm.Controls
If ctl.ControlType = acSubform And ctl.Tag = "!" Then
If
validate_the_form(Forms!frm_project_detail.frm_sub_resource.Form) = False Then
validate = False
Exit For
End If
End If
Next ctl

*************************************************

Whilst this routine works fine. How do I replace the
"Forms!frm_project_detail.frm_sub_resource.Form" with a variable that handles
all subforms on a form.

Thanks in advance.

Ian
 
A

Allen Browne

Your variable ctl is already a reference to the subform control, so you can
just use:
If validate_the_form(ctl.Form) = False Then

Of course that subform might itself contain a subform, since subforms can be
nested to 7 levels. So you might want to write a piece of recursive code
that calls itself again to handle each subform and its subform and so on.

There is an example of that kind of code in this article:
Locking bound controls
at:
http://allenbrowne.com/ser-56.html
You will see that it loops through the controls on the form just like you
code, and in the case of an acSubform, the function calls another instance
of itself to hanlde the subform by passing ctl.Form just as we suggested
above. (The code in the article doesn't return a value, but it is easily
adjusted so that it does.)
 
G

Guest

Allen that's excellent and works a treat. I'm fairly new to VB and Access and
this is probably the only combination I didn't try. I'll also have a look at
your article as this sounds like what I'm trying to achieve.

Thanks very much for your help.
Regards
Ian
 

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