Reference a field on a subform (1 Viewer)

keiths

Registered User.
Local time
Yesterday, 19:20
Joined
Feb 10, 2005
Messages
28
I have 10 fields on a subform. they are named value1 to value10 consecutively. I would like to loop thru them, and get their values to use in subsequent events. I tried the following to reference them...

Dim iLoop as integer
Dim fldVal as control
Dim ItemValue as string

For iLoop = 1 to 10
fldVal = ("Forms![Form 1]![Subform 1]!value" & iLoop)
ItemValue = fldVal
Next iLoop

I also tried a couple of other variations, but cannot get this **** thing to work. Any help would be appreciated.
 

pono1

Registered User.
Local time
Yesterday, 18:20
Joined
Jun 23, 2002
Messages
1,186
Keith,

Typically a subform will contain many records but I assume here, given your question, that yours will always hold one.

If so, one way to do what you want is to create a public variable in your main form.

Code:
Option Compare Database
Option Explicit

Public sRec As String
-------------------------

And then put something like the following in the Current event of your subform...

Code:
Private Sub Form_Current()

Dim ctl As Control
Dim strValues As String

'Create comma delimited string.
   For Each ctl In Me
       If Left(ctl.Name, 5) = "Value" Then
           strValues = strValues & ctl.Value & ","
       End If
   Next ctl
'Trim string.
   strValues = Left(strValues, Len(strValues) - 1)

'Assign to variable in Main form.
   Me.Parent.sRec = strValues

'Test
   Debug.Print Me.Parent.sRec

End Sub

Regards,
Tim
 

RoyVidar

Registered User.
Local time
Today, 03:20
Joined
Sep 25, 2000
Messages
805
Try something like this:

Dim iLoop as integer
Dim fldVal as <datatype fitting the return value from the control>

For iLoop = 1 to 10
fldVal = Forms("Form 1")("Subform 1").form("value" & iLoop).value
' the below would probably also work
' fldVal = me("Subform 1").form("value" & iLoop).value
Next iLoop

Note that when referencing a subform, one uses the subform control name, which may differ from the subform name as viewed in the database window. See
http://support.microsoft.com/?kbid=209099
for how to refer to subform/subreport controls.

You could also try something like this:

dim frm as form
set frm = me("Subform 1").form
For iLoop = 1 to 10
fldVal = frm("value" & iLoop).value
next iLoop
 

Users who are viewing this thread

Top Bottom