I have a question. What _exactly_ is the difference between...
Me.TradeType
and
Me!TradeType
Is there any?
-------------
Yes, the "." (dot) refers to methods and properties that are part of the
form. These typically are built in features.
The ! (bang) refers to user defined collections and values. Of course, they
often over lap. The other important factor is that "." values get resolved
at compile time, where as the "!" bang ones get resolved at runtime. This
is not really a performance issue, but if you have a form with abound
reocrdset, then you do NOT need to place the controls on the screen to
reference a value
So, me.ID or me!ID is will both work
However, if your code changes the recordsoruce at runtime, then a me.ID may
not exist anymore....(if you sql, or query for the form is changed).
So,
When you use me.ID, you will HAVE to put a control on the screen in order to
get at the value of me.ID (this is ONLY when you are setting the recordsouce
at runtime..and in fact no recordsouce is set when you start the form).
Further, you will instantly notice that the code can't compile unless you do
put a control called "id" on the screen when you don't have a reocrdosuce.
So, some actually prefer using the "dot", as it will NOT compile if you miss
type a field name, but in fact, the ! is more correct.
So, if the form is set to a table, or more the recommended a "query", then
the form inherits all the properties of the table, and thus you CAN as
mentioned used me.Fieldname. And, you can use me.Fieldname even if you don't
place a control on the form. However, this inheritance don't always work,
and of course if you set the forms reocrdset, then you can't resolve the
me.Fieldname at compile time. Often, if you change the query, or
table a form is attached to, you find some of your code might break.
So, the dot notation works for field names...but only due to the form
inheriting the properties of the table/query. Now, a form is consider a
"fixed" thing that you build. And, thus, if you put a control on the form
called ID, then in effect, this is a true member of the form (while YOU
created this member...it does belong to the form....so the rule of ! = user
stuff, and dot = built in stuff does get a bit confusing). However, just
think that once you permanently assign a query or table to a form, then it
permanently has all of the fields, and you can use me.
However, I often change the sql for a form, and specially more so for
a sub form. In this case, then I can't know if me.ID will work, and often
before I understood this stuff, I had to put a invisible control on the
form called me.id. If you use me!id, you don't every need that control
to actually be placed on the form.
So, if you are playing around with the reocrdsouce of a form, and you don't
have a correspond control on the screen then you then you CAN NOT use the
me.ID notation (at least reliability anyway) Thus, you have to use the user
defined notation of me!ID.
Often, to avoid any confusing here, a LOT of developers NEVER give the
control on the screen the same name as the underlying field in the
query/table
Thus, even if a control is NOT on the screen, you can always use
me!TheFieldName to
work with the data. And, to work with the control, one can ALWAYS use dot.
me.txtTheFieldName (or whatever naming convention you come up with).
Doing this will eliminate ALL confusing as to when you are referencing a
control. And, things really get nasty if you have a control with the same
name as a field, but set the datasouce to a different field! In fact, in
repots
I now very much avoided this, as they suffer MUCH when you have
controls and fields of the same name.
So, most of the time, you can use either..but the rule is for built in
features..you use "dot", and for your defneined values, you use !
Server database under the application, should I do this by setting the value
of the field on the screen (ie, Me!TradeTypeCombo) or the current record
(ie,
Me!TradeType), or should that make no difference at all?
Good question, now that I explained the difference. I would likely use
the me!TradeType to just set a value. However, since in fact you
DO expect the combo box to be on the screen, and you want it
to show all updates, then I would use the control name in this case.
And, you could even assume the control HAS to be on the screen, and
then you could even use the me. (dot). Thus, if you delete the control,
you code will now NOT compile anymore. So, you can use this to your
advantage.
On the other hand, since you might want to delete the control on eh screen
without
your code breaking..then again..you have to choose...
So, in your above example, if you need the
screen display to update..then use the control. And, the choice
between dot, and bang is this case is kind of up to you, and
your coding standards. For controls, I actually use the dot, but
this is a personal pref, and some would consider it not the best
choice. For all stuff like the "id" fields etc that don't show on the
form, I always now use the ! (bang). So, when you see a dot
notation in my code...I made the assuming that this is a control,
and it is on the screen....