Control name vs Field name

  • Thread starter Thread starter Chaplain Doug
  • Start date Start date
C

Chaplain Doug

I have the practice of naming my controls the same as the
field that is the data source of the control. I have three
questions:

1. Is there anything inherently wrong with naming the
control the same as the source field?

2. How do I reference the control vs the field in the
code behind the form? Say I have a control named SAP with
source field SAP. Is the control reference SAP and the
field reference [SAP] or vice versa?

3. Which should I work with in the code, the control name
or the field name. For instance if assigning a value
which should I use?

Thanks for the help.
 
Chaplain Doug said:
I have the practice of naming my controls the same as the
field that is the data source of the control. I have three
questions:

Thatis my practice as well.
1. Is there anything inherently wrong with naming the
control the same as the source field?

Some report that there can be issues with this, but my experience is that I
have more problems when the names are different and have never encountered
a problem when they were the same.

2. How do I reference the control vs the field in the
code behind the form? Say I have a control named SAP with
source field SAP. Is the control reference SAP and the
field reference [SAP] or vice versa?

If you make reference to a property that the control has, but the field
doesn't then the reference will automatically be correct. When referencing
a property that they both have and you need to be specific you can use a
more explicit syntax.

Me.Controls("ControlName")
Me.RecordsetClone.Fields ("FieldName")

3. Which should I work with in the code, the control name
or the field name. For instance if assigning a value
which should I use?

I (nearly) always name them the same and then use the dot in which case it
doesn't seem to really matter.

Me.SomeName = SomeValue

I don't know if the above is actually setting one of the two with the other
just inheriting the value or if it is assigning it to both of them. I just
know that it works. The only time I can think of where it might matter is
when a control is in the process of being changed. While certain events
are executing there might be a time where the value of the field is not the
same as the value of the control. I suppose that could be an issue, but
I've never encountered it.
 
In response:

1. Yes...and your next 2 questions make the point.

2. If you follow a proper naming convention, there wouldn't be a question.

3. It depends on what you are trying to do. In some cases, you'll want to
refer to the field...in others the control. The case of assigning a value,
either will work. But there are some properties/methods that apply only to
controls, which is why it is a good idea to differentiate the control from
the field.

There are issues you'll encounter besides the questions you have raised,
which is why many Access programmers follow a naming convention like LNC or
Reddick.

There is a free utility at web site below under Add-ins/Extras that helps
with control naming.
 
In response:

1. Yes...and your next 2 questions make the point.

2. If you follow a proper naming convention, there wouldn't be a question.

3. It depends on what you are trying to do. In some cases, you'll want to
refer to the field...in others the control. The case of assigning a value,
either will work. But there are some properties/methods that apply only to
controls, which is why it is a good idea to differentiate the control from
the field.

There are issues you'll encounter besides the questions you have raised,
which is why many Access programmers follow a naming convention like LNC or
Reddick.

There is a free utility at web site below under Add-ins/Extras that helps
with control naming.
 
I don't use Me.ControlName to refer to a control. Instead
if the control name is SAP i simply use SAP. But what is
the difference between SAP and [SAP]?



-----Original Message-----
I have the practice of naming my controls the same as the
field that is the data source of the control. I have three
questions:

Thatis my practice as well.
1. Is there anything inherently wrong with naming the
control the same as the source field?

Some report that there can be issues with this, but my experience is that I
have more problems when the names are different and have never encountered
a problem when they were the same.

2. How do I reference the control vs the field in the
code behind the form? Say I have a control named SAP with
source field SAP. Is the control reference SAP and the
field reference [SAP] or vice versa?

If you make reference to a property that the control has, but the field
doesn't then the reference will automatically be correct. When referencing
a property that they both have and you need to be specific you can use a
more explicit syntax.

Me.Controls("ControlName")
Me.RecordsetClone.Fields ("FieldName")

3. Which should I work with in the code, the control name
or the field name. For instance if assigning a value
which should I use?

I (nearly) always name them the same and then use the dot in which case it
doesn't seem to really matter.

Me.SomeName = SomeValue

I don't know if the above is actually setting one of the two with the other
just inheriting the value or if it is assigning it to both of them. I just
know that it works. The only time I can think of where it might matter is
when a control is in the process of being changed. While certain events
are executing there might be a time where the value of the field is not the
same as the value of the control. I suppose that could be an issue, but
I've never encountered it.


--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com


.
 
Doug,

I always name a bound control the same as the field it is bound to.
There is nothing inherently wrong with this, although many people follow
a different convention.

There is no differnce between SAP and [SAP].

Where problems sometimes arise, which is really a different question, is
if you have a control named the same as a field in the underlying record
source, but the control is not bound to that field. People sometimes
fall into this trap if they have a textbox named the same as the field
to which it is bound, and then decide to change the control source of
the textbox to a calculation expression, and forget to adjust the
control name.
 
Chaplain said:
I have the practice of naming my controls the same as the
field that is the data source of the control. I have three
questions:

1. Is there anything inherently wrong with naming the
control the same as the source field?

No, not inherently wrong. But it is amibiguous and can
cause confusion as your other two questions demonstrate.

Access wil generally use the control if it can. This leads
to a major source of confusion at least (in the developer's
mind). Lets say you have a text box named LastName and it's
bound to the LastName field. Later, you decide that you
want to display the FirstName field in the same text box, so
you edit the text box to use the expression:
=FirstName & " " & LastName
Why are you perplexed when the text box displays #Error?

2. How do I reference the control vs the field in the
code behind the form? Say I have a control named SAP with
source field SAP. Is the control reference SAP and the
field reference [SAP] or vice versa?

As explained else thread, you can do it, but why introduce
the problem.

3. Which should I work with in the code, the control name
or the field name. For instance if assigning a value
which should I use?

Use the control, 99.99% of the time, they're the same value
(you'd have to go out of your way to be dealing with the
other .01%).
 
Back
Top