Dynamic Subform???

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

I need to do something that I am sure is possible but I am
unsure how to best approach the problem.

I need to create a form with a sub form, the sub form will
be continuous, I want the fields displayed on the sub form
to be variable based on the value selected in a combo box
on the form. My concern is that each occurrence of the
sub form may have different fields "Exposed"

Does anyone know where I might find an example of anything
that may have been done similarly?? Basically the each
occurrence of the form would be built based on the value
of one field on that form.

Hope that this makes sense to SOMEBODY!!

I appreciate the help!

Rick
 
Hi,


The sub form is a form, you can create more controls than required, hide
them (unhide them) through their visible property, and bound them to
different fields (preferable in their onOpen event, but for a form, that is
not as a strict requirement) through their ControlSource property. Try to BE
SURE that NO control name matches a field name because if it happen and that
they are then not bound together, an error would occur (#name, or something
like that).



Hoping it may help,
Vanderghast, Access MVP
 
But in doing this, if I base the visible controls on a
selection of say a combo box, wont that change the
controls on ALL instances of the continuous form?? I
expected that that would most likely be the problem I
would run in to. I hope to try some different things
today, I was hoping that there were a more elaborate
solution some place to look at. I currently am doing
something similar in another database I have built but do
it by having different sub forms with different fields
that are toggled based on a value on the main form. This
solution will not work in this case because in this case
the sub form may have several child records, and they all
need to be displayed but with unique fields on each. I
may have blinders on and may be missing an obvious simple
solution.

Rick
 
Hi,


That would change the control of one instance since you do not change the
CLASS (the form template) but an instance, an object derived from the class.

If the "available" controls in the (sub) form are called like Extra1 to
Extra9 ( ie "Extra" & i ), you can then try something like:


=====================================
Private Sub Command4_Click() ' make the change under a command button click
Dim i As Long
Dim j As Long
Dim varItem As Variant
Const NumberOfExtra As Long = 9 'max number of controls available

i=1
For Each varItem In Me.ListBoxName.ItemsSelected

With Me.SubFormControlName.Form.Controls("Extra" & i)
.ControlSource= Me.ListBoxName.ItemData(varItem)
.Visible=True
End With
i=i+1
If i >= NumberOfExtra +1 then exit for

Next varItem

For j=i to NumberOfExtra
Me.SubFormControlName.Form.Controls("Extra" & j).Visible=False
Next j

End Sub
=============================================



There is nothing really complex. Note that if you SAVE the form while in
design, then it may happen that you change the CLASS of the subform and that
it takes the new controlsource values as part of the new design. You can
remove manually the saved control source value, in design mode, for the form
used as subform. Also, I assume that the form, used as subform, has already
its RecordSource set to the table/query from which you picked the fields
name.



Hoping it may help,
Vanderghast, Access MVP
 
Back
Top