Substitute variable for Form field name

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
 
B

Brendan Reynolds

Try ...

Me.Controls(stTemp)

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

Guest

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top