Substitute variable for Form field name

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

Guest

I've created a 4 generation pedigree form. With a loop, I'm trying to Null
several fields on Form4 but my code is wrong. My code is using 'stTemp'
instead of the value of variable stTemp. The names go from H01...H31

Dim stFieldName As Integer
Dim stLetter As String
Dim stTemp As String
For stFieldName = 1 To 31
stTemp = IIf(stFieldName < 10, "H0" & LTrim(Str(stFieldName)), "H" &
LTrim((Str(stFieldName))))

MsgBox (stTemp)

Me![stTemp] = Null

Next stFieldName

I Get error "Can't find the field 'stTemp referenced to in your expression'
I can comment out Me![stTemp] and the Msgbox line will loop thru as expected.

Thanks in Advance for any help!

Scott
 
Try ...

Me.Controls(stTemp)

BTW: You can use CStr() instead of LTrim(Str()). CStr() does not add a
leading space as Str() does.
 
Thank You very much Brendan. I works like a charm.

Thanks again
Scott Skelton


Brendan Reynolds said:
Try ...

Me.Controls(stTemp)

BTW: You can use CStr() instead of LTrim(Str()). CStr() does not add a
leading space as Str() does.

--
Brendan Reynolds
Access MVP


Scott said:
I've created a 4 generation pedigree form. With a loop, I'm trying to Null
several fields on Form4 but my code is wrong. My code is using 'stTemp'
instead of the value of variable stTemp. The names go from H01...H31

Dim stFieldName As Integer
Dim stLetter As String
Dim stTemp As String
For stFieldName = 1 To 31
stTemp = IIf(stFieldName < 10, "H0" & LTrim(Str(stFieldName)), "H" &
LTrim((Str(stFieldName))))

MsgBox (stTemp)

Me![stTemp] = Null

Next stFieldName

I Get error "Can't find the field 'stTemp referenced to in your
expression'
I can comment out Me![stTemp] and the Msgbox line will loop thru as
expected.

Thanks in Advance for any help!

Scott
 
Thank You very much Brendan. It works like a charm.

Thanks again

Scott Skelton

Brendan Reynolds said:
Try ...

Me.Controls(stTemp)

BTW: You can use CStr() instead of LTrim(Str()). CStr() does not add a
leading space as Str() does.

--
Brendan Reynolds
Access MVP


Scott said:
I've created a 4 generation pedigree form. With a loop, I'm trying to Null
several fields on Form4 but my code is wrong. My code is using 'stTemp'
instead of the value of variable stTemp. The names go from H01...H31

Dim stFieldName As Integer
Dim stLetter As String
Dim stTemp As String
For stFieldName = 1 To 31
stTemp = IIf(stFieldName < 10, "H0" & LTrim(Str(stFieldName)), "H" &
LTrim((Str(stFieldName))))

MsgBox (stTemp)

Me![stTemp] = Null

Next stFieldName

I Get error "Can't find the field 'stTemp referenced to in your
expression'
I can comment out Me![stTemp] and the Msgbox line will loop thru as
expected.

Thanks in Advance for any help!

Scott
 
Back
Top