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.
