Programmatically Set Text Box Control Source On Form Load

T

Tony

Hi All,

I don't know if this can be done, but I'm trying to programmatically set the
control source of a text box conditionally when a form loads. For example,
if condtion A is true the control is bound to field1, else it's bound to
field2. I've tried the below with no success:

If conditionA = True then

Me.txtTextBox.ControlSource = "=Me![field1]"

else

Me.txtTextBox.ControlSource = "=Me![field2]"

End If

Any help that can offered is appreciated. Thanks.
 
F

fredg

Hi All,

I don't know if this can be done, but I'm trying to programmatically set the
control source of a text box conditionally when a form loads. For example,
if condtion A is true the control is bound to field1, else it's bound to
field2. I've tried the below with no success:

If conditionA = True then

Me.txtTextBox.ControlSource = "=Me![field1]"

else

Me.txtTextBox.ControlSource = "=Me![field2]"

End If

Any help that can offered is appreciated. Thanks.

You can't use the Me keyword in the control source of a control.

Me.txtTextBox.ControlSource = "=[field1]"
Else
Me.txtTextBox.ControlSource = "=[field2]"

should work.
Make sure the name of the control is not Field1 or Field2).
 
G

Guest

Try:
If conditionA = True then
Me.txtTextBox.ControlSource = "field1"
Else
Me.txtTextBox.ControlSource = "field2"
End If

Steve
 
T

Tony

Both ways worked perfectly. Thanks a lot for the help. One question: is
one way better than the other? I've always gone with the convention of
wrapping field names in brackets. If I don't go that way, are there any
pitfalls that either of you know of?

Again, many thanks.
 
G

Guest

Wrapping field names in brackets is never a bad idea.
If you don't, there is one major pitfall. If you inadvertently use a
reserved workd as a name, Access can get very confused and will start
mumbling nonsense :)
--
Dave Hargis, Microsoft Access MVP


Tony said:
Both ways worked perfectly. Thanks a lot for the help. One question: is
one way better than the other? I've always gone with the convention of
wrapping field names in brackets. If I don't go that way, are there any
pitfalls that either of you know of?

Again, many thanks.

Tony said:
Hi All,

I don't know if this can be done, but I'm trying to programmatically set
the control source of a text box conditionally when a form loads. For
example, if condtion A is true the control is bound to field1, else it's
bound to field2. I've tried the below with no success:

If conditionA = True then

Me.txtTextBox.ControlSource = "=Me![field1]"

else

Me.txtTextBox.ControlSource = "=Me![field2]"

End If

Any help that can offered is appreciated. Thanks.
 
T

Tony

Thanks Dave,

That's the way I'll go; I've tripped myself up in the past using reserved
words. I appreciate the feedback.

Tony

Klatuu said:
Wrapping field names in brackets is never a bad idea.
If you don't, there is one major pitfall. If you inadvertently use a
reserved workd as a name, Access can get very confused and will start
mumbling nonsense :)
--
Dave Hargis, Microsoft Access MVP


Tony said:
Both ways worked perfectly. Thanks a lot for the help. One question: is
one way better than the other? I've always gone with the convention of
wrapping field names in brackets. If I don't go that way, are there any
pitfalls that either of you know of?

Again, many thanks.

Tony said:
Hi All,

I don't know if this can be done, but I'm trying to programmatically
set
the control source of a text box conditionally when a form loads. For
example, if condtion A is true the control is bound to field1, else
it's
bound to field2. I've tried the below with no success:

If conditionA = True then

Me.txtTextBox.ControlSource = "=Me![field1]"

else

Me.txtTextBox.ControlSource = "=Me![field2]"

End If

Any help that can offered is appreciated. Thanks.
 
G

Guest

Glad I could help.
Remember some reserved words are Date, Time, Type, Name, etc
But a reversed word is drow
--
Dave Hargis, Microsoft Access MVP


Tony said:
Thanks Dave,

That's the way I'll go; I've tripped myself up in the past using reserved
words. I appreciate the feedback.

Tony

Klatuu said:
Wrapping field names in brackets is never a bad idea.
If you don't, there is one major pitfall. If you inadvertently use a
reserved workd as a name, Access can get very confused and will start
mumbling nonsense :)
--
Dave Hargis, Microsoft Access MVP


Tony said:
Both ways worked perfectly. Thanks a lot for the help. One question: is
one way better than the other? I've always gone with the convention of
wrapping field names in brackets. If I don't go that way, are there any
pitfalls that either of you know of?

Again, many thanks.

Hi All,

I don't know if this can be done, but I'm trying to programmatically
set
the control source of a text box conditionally when a form loads. For
example, if condtion A is true the control is bound to field1, else
it's
bound to field2. I've tried the below with no success:

If conditionA = True then

Me.txtTextBox.ControlSource = "=Me![field1]"

else

Me.txtTextBox.ControlSource = "=Me![field2]"

End If

Any help that can offered is appreciated. Thanks.
 
T

Tony

Never heard that one before; very good...


Klatuu said:
Glad I could help.
Remember some reserved words are Date, Time, Type, Name, etc
But a reversed word is drow
--
Dave Hargis, Microsoft Access MVP


Tony said:
Thanks Dave,

That's the way I'll go; I've tripped myself up in the past using reserved
words. I appreciate the feedback.

Tony

Klatuu said:
Wrapping field names in brackets is never a bad idea.
If you don't, there is one major pitfall. If you inadvertently use a
reserved workd as a name, Access can get very confused and will start
mumbling nonsense :)
--
Dave Hargis, Microsoft Access MVP


:

Both ways worked perfectly. Thanks a lot for the help. One question:
is
one way better than the other? I've always gone with the convention
of
wrapping field names in brackets. If I don't go that way, are there
any
pitfalls that either of you know of?

Again, many thanks.

Hi All,

I don't know if this can be done, but I'm trying to programmatically
set
the control source of a text box conditionally when a form loads.
For
example, if condtion A is true the control is bound to field1, else
it's
bound to field2. I've tried the below with no success:

If conditionA = True then

Me.txtTextBox.ControlSource = "=Me![field1]"

else

Me.txtTextBox.ControlSource = "=Me![field2]"

End If

Any help that can offered is appreciated. Thanks.
 

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