Link to home
Start Free TrialLog in
Avatar of Fritz Paul
Fritz PaulFlag for South Africa

asked on

How to remove all "captions" from all field properties from all tables in database

How can I use VBA to remove all captions from all field properties from all tables in my database.
I use Access 2010.
Avatar of als315
als315
Flag of Russian Federation image

Try this code:
Public Sub remove_captions()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set db = CurrentDb
On Error Resume Next
For Each tdf In db.TableDefs
    ' ignore system and temporary tables
    If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") Then
        For Each fld In tdf.Fields
            If fld.Properties("Caption") <> "" Then
                Debug.Print tdf.Name, fld.Name, fld.Properties("Caption")
                fld.Properties.Delete "Caption"
            End If
        Next
    End If
Next
End Sub

Open in new window

Paul,

Can I ask why?  Using the Caption property of the field allows you to give the field a meaningful name which shows up in queries and datasheets without actually changing the name of the field.  It is frequently more useful to display those captions than the actual name of your fields

Dale
Avatar of Fritz Paul

ASKER

@als315
Thanks that looks super. However when I run the code I get an error. Can you see the problem?
User generated image
Was my code modified? There are no FIndRecord or FindNext in my code.
Open debugger and try to find line with error
@Dale Fye
Thanks for your interest.
I have often wondered about this issue. Adding these captions cause so much confusion when you try to edit forms and queries.
I paste a simple example below.
To make the connection between the data on a report or form and the table is so much more difficult. And whenever you want to change the caption in a datasheet to a new one or to another language, then you really get entangled, because you are just ignored if you add a caption in the query if there is a caption in the table.
So I just wonder why don't you give the field a descriptive name in the first place?
I try to do that and then I add captions if required in the queries.
In this case I work with a legacy database with many tables and fields. It was well written, but unfortunately the programmer used a lot of captions of which some are the same as the field names and some differ. I have wasted so much time editing this database that I now decided to clean the whole thing.
Do you think I am missing something here?
User generated image
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for that sample.
I missed a reference in my database.
It works perfectly now.
Thanks
You are welcome
Do you think I am missing something here?
No, if it is a mess, it is a mess.
So I just wonder why don't you give the field a descriptive name in the first place?
I would hate "The ID for the Plant" as a caption, too.
Captions can be nice, in that when you create a new bound control, the label for the control picks up the caption name -- so for PlantID, a caption like "Plant ID" can be nice.
(spaces in field names are abominations!  But nice in labels)

I don't permit the user to see queries, or datasheets, and in design view, a bound control has the field name in the control, so I don't think I would find captions confusing.

And whenever you want to change the caption in a datasheet to a new one or to another language, then you really get entangled, because you are just ignored if you add a caption in the query if there is a caption in the table.
That isn't phrased well.
I hadn't realized that field aliases would be ignored -- because I generally don't use captions -- but you are right that an attempt to alias a column that has a caption fails.
If I put MyNewName:[SomeField] in a query, I DEFINITELY expect to see the alias MyNewName and not the caption of SomeField.  That would be an absolute pain in the rear.

It turns out you can override the caption inherited from the table by right-clicking the field and choosing Properties|Caption, but I could see why you may not want that.

There is also
fld.Properties("Caption").Inherited
It may be too late now, but you could perhaps have kept the captions intact, but prevented them from propagating  to labels and datasheets in whole or in part.
Nick67 thanks for your detailed comments.
Regards.
It was an interesting question.
When I built my tables, I didn't put captions to very many fields (as of now I have 1595 fields in 163 tables) and had always thought about a UI to do so -- but since most of the work that captioning could have saved me is done, I never have.

But aliasing failures would be dreadfully annoying -- but I guess if you knew about what a caption does to aliasing, then the few times you wanted a  custom column caption may not have outweighed the thousands of labels that had to be reworded.

So, thank you for an interesting question