Update controls programmatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with several unbound text boxes presented to ask users to input
common data to be passed to several controls in several related records on a
subform. For example:

Main form
Dat1 text box
Dat2 text box
Dat3 text box
etc.

Sub form
Control1
Control2
Control3

Following is the code I am using in the On Click event in a Button control
to test the contents of each Dat control on the main form and update the
corresponding Control on the sub form is appropiate:

If Me.Parent.Dat1 > 0 Then
Me.Control1 = Me.Parent.Dat1
End If
If Me.Parent.Dat2 > 0 Then
Me.Control2 = Me.Parent.Dat2
End If
If Me.Parent.Dat3 > 0 Then
Me.Control3 = Me.Parent.Dat3
End If
..........

The main form presents over 20 different Dat controls. Is the code
structured properly or is there a better way to approach this task without
using over 20 If statements?
 
Rick,

Try something like:

For Each ctl in Me.Parent.Controls
If ctl.Name Like "Dat*" Then
vIndex = Right(ctl.Name, Len(ctl.Name)-3)
Me.Controls("Control" & vIndex) = Me.Parent.Controls(ctl.Name)
End If
Next

(watch out for wrapping in your newsreader!)

The code above will work for any number of DatX / ControlX. Note: code
is untested.

HTH,
Nikos
 
Hi,
You can use something like this. We'll assume you have 20 Dat controls
numbered consecutively from 1 to 20.

For i = 1 To 20
If Me.Parent("Dat" & i) > 0 Then
Me("Control" & i) = Me.Parent("Dat" & i)
End If
Next i

HTH
Dan Artuso, Access MVP
 
Dan:

I have re-posted my question to the newsgroup complete with my attempt to
write the code for this function.
 
Back
Top