Change Table Property Description with VBA

G

Gary S

I want to change the table property description with VBA code.
How should I go about that?

Also, if I wanted just to read the table proeprty description
using VBA, how is that done?
 
D

Douglas J. Steele

CurrentDb.TableDefs("NameOfTable").Properties("Description") will read it,
but only if a description exists. (If one doesn't, you'll get an runtime
error 3270 ("Property not found").

If the property does exist, changing it's value is as simple as:

CurrentDb.TableDefs("NameOfTable").Properties("Description") = "This is the
new description"

If it doesn't, you need to use the CreateProperty method.

Here's some untested code.

Function GetTableDescription(TableName As String) As String
On Error GoTo ErrHandler

Dim strDescription As String

strDescription = CurrentDb.TableDefs(TableName).Properties("Description")

ExitHere:
GetTableDescription = strDescription
Exit Function

ErrHandler:
Select Case Err.Number
Case 3270 ' Property Not Found
strDescription = "No description exists"
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
Resume ExitHere

End Function

Sub SetTableDescription(TableName As String, Description As String)
On Error GoTo ErrHandler

Dim tdfTable As DAO.TableDef
Dim prpDesc As DAO.Property

CurrentDb.TableDefs(TableName).Properties("Description") = _
Description

ExitHere:
Exit Sub

ErrHandler:
Select Case Err.Number
Case 3270 'Property Not Found
Set tdfTable = CurrentDb.TableDefs("TableName")
Set prpDesc = tdfTable.CreateProperty( _
"Description", dbText, Description)
tdfTable.Properties.Append prpDesc
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
Resume ExitHere

End Sub


You'd use them as:

Dim strOldDescription As String

strOldDescription = GetTableDescription("TableName")
Call SetTableDescription("TableName", "New description")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top