473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

Control Object Reference (Me!)

Denburt
1,356 Expert 1GB
If you are in the VBA section of a form and want to refer to a control on that form then the quickest most efficient way of achieving a connection to this control would be the most direct Me!NewData.

(Copied from MS Access help file) also found here:
http://msdn2.microsoft.com/en-us/library/aa223099(office.11).aspx
Expand|Select|Wrap|Line Numbers
  1. ' Implicitly refer to NewData control in Controls collection.
  2. Me!NewData
  3.  
  4. ' Use if control name contains space.
  5. Me![New Data]
  6.  
  7. ' Performance slightly slower.
  8. Me("NewData")
  9.  
  10. ' Refer to a control by its index in the controls collection.
  11. Me(0)
  12.  
  13. ' Refer to a NewData control by using the subform Controls collection.
  14. Me.ctlSubForm.Controls!NewData
  15.  
  16. ' Explicitly refer to the NewData control in the Controls collection.
  17. Me.Controls!NewData
  18.  
  19. Me.Controls("NewData")
  20.  
  21. Me.Controls(0)
  22.  
I know quite frequently I might need my field name to be dynamic so I do use the following in such circumstances:

Expand|Select|Wrap|Line Numbers
  1. Me(MyVariable)
There is a huge misconception that it is ok to reference a control object as Me.NewData there are many reasons not to use this style simply put it is wrong. In all my years I have yet to find a use for Me.MyControl. If you have one feel free to PM me.

If you use a period after Me then the program is looking for a property or method not an object although MS Access can be forgiving the one time it fails on you will probably be when you need it most (Murphy’s law).
Many people say that they do it that way because of the properties dropdown box, yeah when I was a noob I used the properties box but still went back and changed the period to the exclamation. Sometime later I found that after I type Me! I press Control and hold it then hit the space bar viola a dropdown I can use.

Remember that when you name your controls try to avoid anything that can be used by MS Access such as Date. Date is a function in MS Access so it is not wise to name a field that. I know I may here some arguments over the following however I have had issues with some of the following so I always recommend that people avoid spaces in field names and any odd characters such as underscores.
Mar 21 '07 #1
9 71758
NeoPa
32,556 Expert Mod 16PB
There is a huge misconception that it is ok to reference a control object as Me.NewData there are many reasons not to use this style simply put it is wrong. In all my years I have yet to find a use for Me.MyControl. If you have one feel free to PM me.
We've had this discussion before (Referencing Controls on a Form) and I never got an answer as to why an object property should be treated any differently from any other property. I still feel the correct way to reference any property on a form is to use the '.' rather than the '!' (Which, after all, simply indicates that a shortcut syntax is being used). If a control is a member of the defined class (Which it clearly is) then no shortcut is required. Until you can explain why this is not the case, I don't think you're in a position to say that the '.' is wrong, simply or otherwise.
Mar 22 '07 #2
Denburt
1,356 Expert 1GB
I do agree that the control is a property of the form however it is also an object in and of itself.

I could be mistaken but from everything I know Me! refers to an object and Me. refers to a property.

Do you set the focus to a property or to an object?
Do you change the height of a property or an object?

In closing I have spent a lot of time correcting inherited databases that used Me. and for whatever reason it failed or better yet hung up and gave a debug error. That is the only reason I took the bold stance I did when I stated that it was wrong even though I didn't provide documentation.

I feel like I wrote this much better the first time but by the time I was done I needed to relog in lol I forgot to copy it.
Mar 22 '07 #3
NeoPa
32,556 Expert Mod 16PB
I can understand that last paragraph. That's happened to me so many times!
I'm starting to get where you're coming from with the object versus property stuff. I just couldn't follow what you meant by it before.
As far as I am aware though, a property can be a simple property (Integer; Boolean; String; etc) but it can also be any class (or object) defined as part of the holding class (or object). An object simply being an instance of a class.
My understanding of the '!' is that it is shorthand for another (more precise) way of referring to an item. For instance, Me.Controls("CtrlName") can be shortened to Me.Controls!CtrlName. The latter will actually be faster to execute as it can be early-bound by the compiler.
Don't get me wrong, I think you've done some good work here. I just didn't want anyone to think that this was a hard-and-fast fact. I still feel that it's open to a more definitive answer. I know why I think what I think (laid out here) but I'm not so sure Microsoft have followed my way of thinking (They'd be fools not to OBVIOUSLY), or even if they've followed one consistent way of thinking even.
Mar 23 '07 #4
Denburt
1,356 Expert 1GB
I'm starting to get where you're coming from with the object versus property stuff. I just couldn't follow what you meant by it before.
Good that makes me glad.

Don't get me wrong, I think you've done some good work here.
Thank you very much!

I just didn't want anyone to think that this was a hard-and-fast fact. I still feel that it's open to a more definitive answer. I know why I think what I think (laid out here) but I'm not so sure Microsoft have followed my way of thinking (They'd be fools not to OBVIOUSLY), or even if they've followed one consistent way of thinking even.
I completely agree, I have been looking for credible documentation on this topic for a long time now, I may just find it one day..... But until then :)
Mar 23 '07 #5
Denburt
1,356 Expert 1GB
I have been sending out emails in the hopes that I will find someone who has some documentation on this topic.
Mar 26 '07 #6
After trying to clarify this for myself since I came recently to Access from other programming languages I've come across an explanation. It is from the book Microsoft Office Access 2003 Inside Out by John L. Viescas.

Here is that part:

When to Use “!” and “.”

You’ve probably noticed that a complex, fully qualified name of an object or a property in Access or Visual Basic contains exclamation points (!) and periods (.) that separate the parts of the name.

Use an exclamation point preceding a name when the name refers to an object that is in the preceding object or collection of objects. A name following an exclamation point is generally the name of an object you created (such as a form or a table). Names following an exclamation point must be enclosed in brackets ([ ]) if they contain embedded blank spaces or a special character, such as an underscore (_). You must also enclose the name of an object you
created in brackets if the name is the same as an Access or SQL reserved word. For example, most objects have a Name property—if you name a control or field “Name,” you must use brackets when you reference your object.

To make this distinction clear, you might want to get into the habit of always enclosing in brackets names that follow an exclamation point, even though brackets are not required for names that don’t use blank spaces or special characters. Access automatically inserts brackets around names in property sheets, design grids, and action arguments.

Use a period preceding a name that refers to a collection name, a property name, or the name of a method that you can perform against the preceding object. (Names following a period should never contain blank spaces.) In other words, use a period when the following name is of the preceding name (as in the TableDefs collection of the Databases(0) object, the Count property of the TableDefs collection, or the MoveLast method of the DAO Recordset object).

This distinction is particularly important when referencing something that has the same name as the name of a property. For example, the reference

DBEngine.Workspaces(0).Databases(0).TableDefs(13). Name

refers to the name of the fourteenth TableDef object in the current database. In the Contacts .mdb database, if you use Debug.Print to display this reference, Visual Basic returns the value tblCompanyContacts.
However, the reference

DBEngine.Workspaces(0).Databases(0).TableDefs(13)![Name]

refers to the contents of a field called Name (if one exists) in the fourteenth TableDef object in the current database. In the LawTrack Contacts database, this reference returns an error because there is no Name field in the tblCompanyContacts table.
Jun 10 '07 #7
FishVal
2,653 Expert 2GB
I do agree that the control is a property of the form however it is also an object in and of itself.

I could be mistaken but from everything I know Me! refers to an object and Me. refers to a property.

Do you set the focus to a property or to an object?
Do you change the height of a property or an object?
Nice!

So the following expression is invalid for the same reasons.
MsgBox b * val(c)
Variable is multiplied by function and the result this goes to the screen. :-D

What concerns the use of Me.ControlName expression vs. Me![ControlName]:
  • For each form Access creates its own class.
  • Now somewhat speculative thing (I didn't check whether it is really so) - a default property for this class is one returning Form object, so Me![ControlName] appears to be shothand to Me.Form![ControlName]
  • Default property of the Form class is Controls - so we have Me![ControlName] = Me.Form.Controls![ControlName]
  • Default property of the Controls class is Item - Me![ControlName] = Me.Form.Controls.Item("ControlName")
  • So its obvious that expression like Me![ControlName] actually calls a Controls collection Item method or property, which in its turn searches the collection and returns corresponding object

To the other hand expression like Me.ControlName calls the topmost class property, which in its turn returns an object of one particulary control. I'm not sure about the mechanism - whether it returns object directly or through Controls collection. In anyway it should be born in mind that using "Me." style expression is not worser and maybe even better than using "Me!" for static control referencing.

Best regards
Jun 12 '07 #8
Denburt
1,356 Expert 1GB
I would like to thank both of the preceding posters, especially for the reference to the book and the fine explanations.
Jun 13 '07 #9
Much ado about nothing.

Here is a link that you might find interesting.

http://blogs.msdn.com/access/archive...t-or-bang.aspx

Here are some tests I ran using a timer. The only difference between the Bang Loops and the Dot Loops were the Bang and the Dot in the code. As you can see there is no practical difference in speed whether you use a Dot or a Bang.

Test 1:
Bang 1000 Loops Time = 360
Bang 1000 Loops Time = 360
Bang 1000 Loops Time = 343
Bang 1000 Loops Time = 360
Bang 1000 Loops Time = 360
Bang 1000 Loops Time = 375

Dot 1000 Loops Time = 391
Dot 1000 Loops Time = 359
Dot 1000 Loops Time = 360
Dot 1000 Loops Time = 391
Dot 1000 Loops Time = 375
Dot 1000 Loops Time = 375

Test 2:
Bang 10000 Loops Time = 531
Bang 10000 Loops Time = 563
Bang 10000 Loops Time = 547
Bang 10000 Loops Time = 547
Bang 10000 Loops Time = 531
Bang 10000 Loops Time = 546

Dot 10000 Loops Time = 547
Dot 10000 Loops Time = 563
Dot 10000 Loops Time = 547
Dot 10000 Loops Time = 547
Dot 10000 Loops Time = 547
Dot 10000 Loops Time = 547

Does it really matter which you use?

Patrick Wood
Nov 19 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
4
by: Frawls | last post by:
Hi, I get the following error when trying to run a search on my aspx site, this error only occours if the product im searching for does not exist. Can anybody explain this please and help me...
1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
6
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page...
1
by: Nathan Sokalski | last post by:
I have a UserControl that I declare programmatically as follows: Dim userctrl as New rightside_portal() The codebehind file for this UserControl looks like the following: Partial Public...
35
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an...
1
by: Don | last post by:
I'm getting the following exception displayed in the task list at design time for my project: "Code generation for property 'Controls' failed. Error was: 'Object reference not set to an...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.