Referencing Form Fields Dynamicly

  • Thread starter Thread starter rburna
  • Start date Start date
R

rburna

Ok, I can't seem to find any information on accessing a field name
dynamicly....

Here is the code

dim iCount
dim iTotal
iTotal = 0
do while iCount <= 16
tThisField = "txtOct" & iCount ' This specifies the field I should be
checking
iTotal = iTotal + VAL(Eval(tThisField))
loop

Logicly this should work, but I keep running into an error telling me
that it can't find the field txtOct1 which is a valid field on the
form....

What am I doing wrong?

Richard
 
Ok, I can't seem to find any information on accessing a field name
dynamicly....

Here is the code

dim iCount
dim iTotal
iTotal = 0
do while iCount <= 16
tThisField = "txtOct" & iCount ' This specifies the field I should
be checking
iTotal = iTotal + VAL(Eval(tThisField))
loop

Logicly this should work, but I keep running into an error telling me
that it can't find the field txtOct1 which is a valid field on the
form....

What am I doing wrong?

Richard

Try something like this

iTotal = iTotal + me.controls("txtOct" & cstr(iCount)).value
 
A few problems:

1) "Dim iCount" and "Dim iTotal" will give you a Variant, not an Integer. If
you want an Integer, then it should be "Dim iCount As Integer" and "Dim
iTotal As Integer".

2) There is nothing in our Do While loop to increment iCount, so it will
always be <=16 (i.e. infinite loop). I believe you may want a For...Next
loop instead. (For iCount = 1 To 16)

3) I assume that tThisField is supposed to be a text variable, but it hasn't
been defined (Dim tThisField As String).

4) The syntax to refer to a field by concatenating the name as you are is

tThisField = Me.Controls("txtOct" & iCount)
or
tThisField = Me.Recordset.Fields("txtOct" & iCount)

I would prefer the first one. I've never used the second one, but I believe
it would work. In the first one you are actually referring to the name of
the control bound to the desired field, not to the field itself.
 
Wayne,

Thanks for the input using

tThisField = Me.Controls("txtOct" & iCount)

was able to give me the reference to the value I was looking for

Thanks.....
Richard
 
Back
Top