Set table field caption via code?

  • Thread starter Thread starter Steve Duff [MVP]
  • Start date Start date
S

Steve Duff [MVP]

Is there a way to set the caption property of a
table field/column in Access2K programmatically?
ADOX doesn't seem to expose this.

TIA

Steve
 
Hi Steve,

I don't really know if you can do it in ADO, but if you want to do it via
DAO, here is an example:

Sub fieldCaption()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim prp As DAO.Property

Set db = CurrentDb
Set tdf = db.TableDefs("MyTable")
Set fld = tdf.Fields("MyField")

On Error GoTo err_Property
fld.Properties("Caption") = "MyCaption"

exit_Sub:

Set fld = Nothing
Set tdf = Nothing
Set db = Nothing

Exit Sub

err_Property:

If Err.Number = 3270 Then 'property doesn't exists
Set prp = fld.CreateProperty("Caption", dbText, "MyCaption")
fld.Properties.Append prp
Set prp = Nothing
Resume Next
Else
MsgBox Err.Description
GoTo exit_Sub
End If

End Sub

HTH :-)

--
Saludos desde Barcelona
Juan M. Afan de Ribera
<MVP Ms Access>
http://www.juanmafan.tk
http://www.clikear.com/webs4/juanmafan
 
Is there a way to set the caption property of a
table field/column in Access2K programmatically?
ADOX doesn't seem to expose this.

ADOX only knows about the data model, and the caption property is not part
of that -- it is part of the Access GUI. Its one and only purpose is to
provide a default value for labels for bound controls when they are placed
on a new form.

There are, frankly, no situations where you need to get at the Caption
property programmatically:

If users have access to the GUI to create forms and reports, then they can
use the table designer to add the property themselves.

If they do not have the ability to create forms and reports, then they will
never even be aware of the existence of the Caption property, never mind
miss the fact that it is not set.

HTH


Tim F
 
When I cut-and-paste the code into VB (only replacing existing-database/table references with declarations of new ones), I get an error message 'Invalid operation' on the line where the new 'Caption' property is appended to Properties.

----- Juan M. Afan de Ribera wrote: -----

Hi Steve,

I don't really know if you can do it in ADO, but if you want to do it via
DAO, here is an example:

Sub fieldCaption()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim prp As DAO.Property

Set db = CurrentDb
Set tdf = db.TableDefs("MyTable")
Set fld = tdf.Fields("MyField")

On Error GoTo err_Property
fld.Properties("Caption") = "MyCaption"

exit_Sub:

Set fld = Nothing
Set tdf = Nothing
Set db = Nothing

Exit Sub

err_Property:

If Err.Number = 3270 Then 'property doesn't exists
Set prp = fld.CreateProperty("Caption", dbText, "MyCaption")
fld.Properties.Append prp
Set prp = Nothing
Resume Next
Else
MsgBox Err.Description
GoTo exit_Sub
End If

End Sub

HTH :-)

--
Saludos desde Barcelona
Juan M. Afan de Ribera
<MVP Ms Access>
http://www.juanmafan.tk
http://www.clikear.com/webs4/juanmafan
 
Back
Top