Naming things on a form

kiplook

Registered User.
Local time
Yesterday, 19:29
Joined
Jun 28, 2002
Messages
18
When making command buttons in forms, such as a button that will open a form named Form B, I would name the button Open Form B - that is if I remember at the time.

Similarly, if I'm using a text box that displays a field named Price, I would name the text box Price and the label would be named Price Label.

The problem is that I usually forget and end up with names like Command1, Text3, and Label3. This makes for a messy database and when you try to reference something in that form later, you'll have no idea what Text3 is.

Is there a function in MS Access that will automatically assign more logical names? (I'm using Access 97)

Is there any freeware that will do this?
:confused:
 
kiplook said:
When making command buttons in forms, such as a button that will open a form named Form B, I would name the button Open Form B - that is if I remember at the time.

Unfortunately, that's quite a bad name to give any control.

Similarly, if I'm using a text box that displays a field named Price, I would name the text box Price and the label would be named Price Label.

Again, bad.

The problem is that I usually forget and end up with names like Command1, Text3, and Label3. This makes for a messy database and when you try to reference something in that form later, you'll have no idea what Text3 is.

You should get into the practice of naming controls the second you create them.

Is there a function in MS Access that will automatically assign more logical names? (I'm using Access 97)

No. Naming controls after you've probably bound them, referenced them, etc. will cause problems and Access just isn't smart enough in this way.


So, why are your names bad?

It's a good practice to prefix controls with an accepted standard - the most popular is the Hungarian Notation which means we prefix, for example, a textbox with txt; a combobox with cbo, a form with frm, etc.

This way it's easier for others to immediately know what items your code refers to - and especially to help yourself should you return to the code 6 months...12 months...etc, later where your own code will be alien to you. It also means - should you be in the habit of naming your controls the exact same as the field it's bound to - that you will lose the ability to refer directly to the underlying field.

Now, spaces in a name also cause havoc. If you start naming controls Total Cost then you are going to have to remember to enclose the textbox's name within square brackets when referring to it. This wouldn't be necessary if the textbox was called TotalCost. Notice also, that I capitalised each word in the title. That's a best practice, too. Technically though, it's best called txtTotalCost.

This applies to field names, tables names, form names, textboxes, listboxes, command buttons, and every control that it's possible to use within Access.

One further thing, you shouldn't name any object after a Reserved Word and you shouldn't include any of the special characters: ./ *:!#&-?; " ' $ in the name of anything either.

It would be nice if the people teaching courses on Access (be they school teachers or actual businesses that run these 2 day 'Advanced' courses) taught people at least this. :rolleyes:
 
Thanks for the info on naming things. I'll use those conventions from now on.

What I'm looking for, however, is some automated way of naming these things as I create them - not after word.

For example: After a text box is created when you bind it to a field named Price it will automatically change the name to txtPrice or perhaps to prevent any potential problems, ask me if it can change the name to txtPrice?

Thanks
 
There is no built-in way to do this. Perhaps there is an add-in floating around that will do this for you, but I don't know of any.

For most developers, it's just 2nd nature to immediately rename the controls after you've created them.
 
It would be nice if the people teaching courses on Access (be they school teachers or actual businesses that run these 2 day 'Advanced' courses) taught people at least this.

It would be nice if people were taught this stuff long before the advanced courses!!!!

Navyguy
 
Navyguy said:


It would be nice if people were taught this stuff long before the advanced courses!!!!

Navyguy
And even nicer if MS would at least use the naming convention that they so kindly advise us on (thanks Mile for the link) when the control is created, thus the creator then only needs to add something meaningful.

Incidentally, here's another link about naming conventions:

http://www.mvps.org/access/general/gen0012.htm

I take the option of adjusting things slightly, for example, my buttons all have the prefix 'btn' because, as the link above suggests, using something is better than nothing :)

Tim
 

Users who are viewing this thread

Back
Top Bottom