forms!formname!field vs. me.fieldname

  • Thread starter Thread starter Thomas Kroljic
  • Start date Start date
T

Thomas Kroljic

Can someone shed some light on the major difference between the following
two statements:

1 - forms!myform!textfield

2 - me.textfield


Is there a rule for when you should use one syntax over the other?

To me, statement 1 allows me to reference objects and data that appear
ON the form, it doesn't effect the underlying record set. Whereas statement
2, will immediately update the underlying record set. Is my logic correct?

Is there a document or something I can read that will shed light on this
subject.

Thank you,
Thomas J. Kroljic
 
The difference is in the context where you can use these expressions. There
is no difference with timing or updatability.

The first reference (using "Forms!MyForm!MyTextbox") works anywhere: any
code, Control Source of a text box, or even in the Criteria row of a query.

The second reference works only in code, and then only in the module of
MyForm. It is slightly more efficient (since Me is a currenly open object),
and significantly easier to debug (since Access already knows about all the
objects that are valid in Me).

It is the last aspect that makes it so useful: *anything* that helps you
write bug-free code is invaluable.
 
Thomas Kroljic said:
Can someone shed some light on the major difference between the following
two statements:

1 - forms!myform!textfield

2 - me.textfield


Is there a rule for when you should use one syntax over the other?

To me, statement 1 allows me to reference objects and data that appear
ON the form, it doesn't effect the underlying record set. Whereas statement
2, will immediately update the underlying record set. Is my logic correct?

Is there a document or something I can read that will shed light on this
subject.

No you are not correct. The difference and advantage of "Me" is that you
can change the name of your form and it won't break the code and you can
copy code from one form to another and the form reference doesn't have to
be changed.

You are also using a shorter reference which means an increase in
efficiency. Generally speaking the fewer bangs and dots that are in your
reference the more efficient it is because it is going through fewer
collections to be resolved.
 
Thomas said:
Can someone shed some light on the major difference between the following
two statements:

1 - forms!myform!textfield

2 - me.textfield


Is there a rule for when you should use one syntax over the other?

You must use the first syntax whenever textfield is not in
the same form as the code. If textfield is in the same form
as the code, then the second is easier to type, easier to
read and more portable (if you should ever change the name
of the form or copy the code to another form).

To me, statement 1 allows me to reference objects and data that appear
ON the form, it doesn't effect the underlying record set. Whereas statement
2, will immediately update the underlying record set. Is my logic correct?

No, that is not correct, see above. The other difference is
your use of . vs ! This difference is not significant to
the result of the code.

Note that either . or ! can be used to refer to a control on
the form and if there is no control named textfield, then it
will refer to a field named textfield in the form's record
source table/query (a good reason to name controls
differently from fields).

Another point is that changing the value of a bound control
will change the value of the field when the record is saved.
So, under almost all circumstances, you should be setting
the value of a control (primarily for clarity of your code).
--
Marsh
MVP [MS Access]


Is there a document or something I can read that will shed light on this
subject.

The Help file?
 
Allen,
Thanks for answering my two simple questions. I'll continue to use the
Me.txt_field in my vba code.
Austraila!!! And here I thought I'd get a response from someone in South
Philadelphia, Pa :)

Thomas J. Kroljic
 
Rick,
Thanks for the insight. I'll continue to use the Me.txt_field reference
in my vba code. I wasn't sure of the difference between the two statements,
now I am.

Thanks,
Thomas J. Kroljic
 
Marshall,
Thanks for answering my questions. Very helpful. I've got a comment or
two though:

<<You must use the first syntax whenever textfield is not in
the same form as the code. >>

I can still reference a textfield in another form as
form_frmname.txtfield. I've been doing this in the past within my vba code.
Isn't that correct?

As for your last point (last paragraph), I'm not sure I understand what
it is you are trying to explain (it's due to my lack of Access knowledge)...


Marshall Barton said:
Thomas said:
Can someone shed some light on the major difference between the following
two statements:

1 - forms!myform!textfield
2 - me.textfield


Is there a rule for when you should use one syntax over the other?

You must use the first syntax whenever textfield is not in
the same form as the code. If textfield is in the same form
as the code, then the second is easier to type, easier to
read and more portable (if you should ever change the name
of the form or copy the code to another form).

To me, statement 1 allows me to reference objects and data that appear
ON the form, it doesn't effect the underlying record set. Whereas statement
2, will immediately update the underlying record set. Is my logic
correct?

No, that is not correct, see above. The other difference is
your use of . vs ! This difference is not significant to
the result of the code.

Note that either . or ! can be used to refer to a control on
the form and if there is no control named textfield, then it
will refer to a field named textfield in the form's record
source table/query (a good reason to name controls
differently from fields).

Another point is that changing the value of a bound control
will change the value of the field when the record is saved.
So, under almost all circumstances, you should be setting
the value of a control (primarily for clarity of your code).
--
Marsh
MVP [MS Access]


Is there a document or something I can read that will shed light on this
subject.

The Help file?
 
Thomas said:
Marshall,
Thanks for answering my questions. Very helpful. I've got a comment or
two though:

<<You must use the first syntax whenever textfield is not in
the same form as the code. >>

I can still reference a textfield in another form as
form_frmname.txtfield. I've been doing this in the past within my vba code.
Isn't that correct?

In many circumstances, yes you can, but there are situations
where that may be ambiguous. Technically, you should only
use that syntax in special cases, which are beyond the scope
of this thread.

As for your last point (last paragraph), I'm not sure I understand what
it is you are trying to explain (it's due to my lack of Access knowledge)...

Bottom line is that if a bound control and a record source
field have the same name, you can not tell by looking at the
code or a text box expression which one you're referring to.
So. if you use a control or field reference in either of
those ways, make sure the control is named something other
than the name of a field. Many people use a 3 letter prefix
that indicates the type of the control, e.g. a text box
bound to the field Amount would be named txtAmount.

To take the next step, use a prefix on your VBA variables as
well. E.g. Dim strAddress As String, intCounter As Integer
 
Marshall,
Thanks for expanding on your original statements. Very helpful.
For more advance discussions of vba behind a Access form, what
forum would be best to use?

Thanks,
Thomas J. Kroljic

Marshall Barton said:
Thomas said:
Marshall,
Thanks for answering my questions. Very helpful. I've got a comment or
two though:

<<You must use the first syntax whenever textfield is not in
the same form as the code. >>

I can still reference a textfield in another form as
form_frmname.txtfield. I've been doing this in the past within my vba code.
Isn't that correct?

In many circumstances, yes you can, but there are situations
where that may be ambiguous. Technically, you should only
use that syntax in special cases, which are beyond the scope
of this thread.

As for your last point (last paragraph), I'm not sure I understand what
it is you are trying to explain (it's due to my lack of Access
knowledge)...

Bottom line is that if a bound control and a record source
field have the same name, you can not tell by looking at the
code or a text box expression which one you're referring to.
So. if you use a control or field reference in either of
those ways, make sure the control is named something other
than the name of a field. Many people use a 3 letter prefix
that indicates the type of the control, e.g. a text box
bound to the field Amount would be named txtAmount.

To take the next step, use a prefix on your VBA variables as
well. E.g. Dim strAddress As String, intCounter As Integer
--
Marsh
MVP [MS Access]



 
I think you're in the right forum, but a message can only
explain so much. I son't know which ones, but you may be
able to find a book or an advanced course that provides
broader details about this stuff.
--
Marsh
MVP [MS Access]



Thomas said:
Marshall,
Thanks for expanding on your original statements. Very helpful.
For more advance discussions of vba behind a Access form, what
forum would be best to use?

Thomas said:
Marshall,
Thanks for answering my questions. Very helpful. I've got a comment or
two though:

<<You must use the first syntax whenever textfield is not in
the same form as the code. >>

I can still reference a textfield in another form as
form_frmname.txtfield. I've been doing this in the past within my vba code.
Isn't that correct?

In many circumstances, yes you can, but there are situations
where that may be ambiguous. Technically, you should only
use that syntax in special cases, which are beyond the scope
of this thread.

As for your last point (last paragraph), I'm not sure I understand what
it is you are trying to explain (it's due to my lack of Access
knowledge)...

Bottom line is that if a bound control and a record source
field have the same name, you can not tell by looking at the
code or a text box expression which one you're referring to.
So. if you use a control or field reference in either of
those ways, make sure the control is named something other
than the name of a field. Many people use a 3 letter prefix
that indicates the type of the control, e.g. a text box
bound to the field Amount would be named txtAmount.

To take the next step, use a prefix on your VBA variables as
well. E.g. Dim strAddress As String, intCounter As Integer
--
Marsh
MVP [MS Access]



Can someone shed some light on the major difference between the following
two statements:

1 - forms!myform!textfield

2 - me.textfield


Is there a rule for when you should use one syntax over the other?

You must use the first syntax whenever textfield is not in
the same form as the code. If textfield is in the same form
as the code, then the second is easier to type, easier to
read and more portable (if you should ever change the name
of the form or copy the code to another form).


To me, statement 1 allows me to reference objects and data that
appear
ON the form, it doesn't effect the underlying record set. Whereas
statement
2, will immediately update the underlying record set. Is my logic
correct?

No, that is not correct, see above. The other difference is
your use of . vs ! This difference is not significant to
the result of the code.

Note that either . or ! can be used to refer to a control on
the form and if there is no control named textfield, then it
will refer to a field named textfield in the form's record
source table/query (a good reason to name controls
differently from fields).

Another point is that changing the value of a bound control
will change the value of the field when the record is saved.
So, under almost all circumstances, you should be setting
the value of a control (primarily for clarity of your code).



Is there a document or something I can read that will shed light on
this
subject.

The Help file?
 
Back
Top