Should Form Control names differ from Field names?

B

Bam Bam

I recall occasionally seeing posts where contributors suggest renaming
controls to something like
'txtCustomerName' rather than 'CustomerName', as it would be named in the
table. When building forms in
Access I generally drag and drop the fields I need over from the field list
and don't alter the control
names(which are the same as the table/query field names by default). I
notice that I often get 'setFocus'
problems in pop-up forms. I wonder if this could be because of fields and
controls having the same name.
Either way, I would really appreciate any thoughts or pointers to
information on this subject.

Regards

Bam Bam.
 
R

Rick Brandt

Bam Bam said:
I recall occasionally seeing posts where contributors suggest renaming
controls to something like
'txtCustomerName' rather than 'CustomerName', as it would be named in the
table. When building forms in
Access I generally drag and drop the fields I need over from the field list
and don't alter the control
names(which are the same as the table/query field names by default). I
notice that I often get 'setFocus'
problems in pop-up forms. I wonder if this could be because of fields and
controls having the same name.
Either way, I would really appreciate any thoughts or pointers to
information on this subject.

Some people report problems when they are the same. The only time I have ever
noticed the issue is when I solved problems caused when they were NOT the same.
It's more of a preference issue IMO.
 
K

Ken Snell [MVP]

The usual problems come when a person has a control name with the same name
as a field name, but the control is not bound to the field with that same
name. Then, if you use expressions that reference just the name, ACCESS
doesn't know whether you mean the field or the control. In these cases,
there may be an error or ACCESS may take the wrong one as its data source.

So long as a control is bound to a field that has the same name as the
control, changing their names is not necessary. (Just remember not to unbind
that control from that field!) I usually rename then when I know that I'll
be using the control in VBA code just so that I'll know which object I
specifically am referring to when reading / debugging the code.
 
B

Bam Bam

Ken,
when you say 'which object I specifically am referring to' do you mean the
control object as opposed to the field object?
 
K

Ken Snell [MVP]

Yes.

--

Ken Snell
<MS ACCESS MVP>

Bam Bam said:
Ken,
when you say 'which object I specifically am referring to' do you mean the
control object as opposed to the field object?
 
V

Van T. Dinh

I think Ken meant either. If he uses to "FieldName", he
refers to the Field. If he uses "txtFieldName" (or cbo,
lst, et...), he refers to the Control which is bound to
the Field "FieldName".

Personally, I _always_ use different nanes for the Field
and its bound Control. In addition, I always use full
references in code with qualifier such as "Me". This way,
I feel I can avoid ambiguities and get a good handle on
the codes I m doing.

HTH
Van T. Dinh
MVP (Access)
 
B

Bam Bam

Thanks for your thoughts folks!

All your general practices on control and field naming sound like the way to
go.

Bam Bam.
 
Top