How to set table descriptions in vba

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created a table that has 2 columns (name, description) which list
table names and descriptions. What i would like to do, is after certain
tables are destroyed and recreated, I would like to add the stored
description to all tables.

The problem im having is that if a table does not have a description
entered, the code:
tbl.Properties("Description") = IIf(IsNull(rst![Description]), "",
rst![Description]) fails saying tbl.properties("Description") cannot be
found. however, this code works for tables that do have a description set.

the code that does the looping is:
For Each tbl In db.TableDefs
rst.Seek "=", tbl.Name
If Not rst.NoMatch Then tbl.Properties("Description") =
IIf(IsNull(rst![Description]), "", rst![Description])
Next tbl

how else could i achieve my goal?

Thanks for any help!
ben
 
Use error handling to trap the error (3270 from memory).

Example of error recovery reading the Description property:
http://allenbrowne.com/func-06.html

BTW, Name and Description would not be idea field names. Almost everything
in Access has a Name property, so there is a very good chance if you put
this field into a form that Access will get confused between the Name of the
form and the value in the Name control on the form. (Description is less
problematic, but many things do have a Description property.)
 
Thanks, but i guess i wasnt clear on my problem.

I dont need to trap the error, I still want to set the value of the table
descriptions, but its giving me an error saying the property isnt available
for that table. Is there a way of ensuring i set the property whether or not
there is a description currently.

Thanks,
Ben

Allen Browne said:
Use error handling to trap the error (3270 from memory).

Example of error recovery reading the Description property:
http://allenbrowne.com/func-06.html

BTW, Name and Description would not be idea field names. Almost everything
in Access has a Name property, so there is a very good chance if you put
this field into a form that Access will get confused between the Name of the
form and the value in the Name control on the form. (Description is less
problematic, but many things do have a Description property.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Ben said:
I have created a table that has 2 columns (name, description) which list
table names and descriptions. What i would like to do, is after certain
tables are destroyed and recreated, I would like to add the stored
description to all tables.

The problem im having is that if a table does not have a description
entered, the code:
tbl.Properties("Description") = IIf(IsNull(rst![Description]), "",
rst![Description]) fails saying tbl.properties("Description") cannot be
found. however, this code works for tables that do have a description
set.

the code that does the looping is:
For Each tbl In db.TableDefs
rst.Seek "=", tbl.Name
If Not rst.NoMatch Then tbl.Properties("Description") =
IIf(IsNull(rst![Description]), "", rst![Description])
Next tbl

how else could i achieve my goal?

Thanks for any help!
ben
 
If it is not available, then you can CreateProperty.

Here's a function you can use that sets the property if it exists, and
creates and sets it if it does not exist.

Example:
Call SetPropertyDAO(Currentdb.TableDefs("MyTable").Fields("MyField"), _
"Description", dbText, "This is my description")

Function SetPropertyDAO(obj As Object, strPropertyName As String, _
intType As Integer, varValue As Variant, Optional strErrMsg As String) As
Boolean
On Error GoTo ErrHandler
'Purpose: Set a property for an object, creating if necessary.
'Arguments: obj = the object whose property should be set.
' strPropertyName = the name of the property to set.
' intType = the type of property (needed for creating)
' varValue = the value to set this property to.
' strErrMsg = string to append any error message to.

If HasProperty(obj, strPropertyName) Then
obj.Properties(strPropertyName) = varValue
Else
obj.Properties.Append obj.CreateProperty(strPropertyName, intType,
varValue)
End If
SetPropertyDAO = True

ExitHandler:
Exit Function

ErrHandler:
strErrMsg = strErrMsg & obj.Name & "." & strPropertyName & _
" not set to " & varValue & ". Error " & Err.Number & " - " & _
Err.Description & vbCrLf
Resume ExitHandler
End Function

Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant

On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Ben said:
Thanks, but i guess i wasnt clear on my problem.

I dont need to trap the error, I still want to set the value of the table
descriptions, but its giving me an error saying the property isnt
available
for that table. Is there a way of ensuring i set the property whether or
not
there is a description currently.

Thanks,
Ben

Allen Browne said:
Use error handling to trap the error (3270 from memory).

Example of error recovery reading the Description property:
http://allenbrowne.com/func-06.html

BTW, Name and Description would not be idea field names. Almost
everything
in Access has a Name property, so there is a very good chance if you put
this field into a form that Access will get confused between the Name of
the
form and the value in the Name control on the form. (Description is less
problematic, but many things do have a Description property.)

Ben said:
I have created a table that has 2 columns (name, description) which list
table names and descriptions. What i would like to do, is after
certain
tables are destroyed and recreated, I would like to add the stored
description to all tables.

The problem im having is that if a table does not have a description
entered, the code:
tbl.Properties("Description") = IIf(IsNull(rst![Description]), "",
rst![Description]) fails saying tbl.properties("Description") cannot be
found. however, this code works for tables that do have a description
set.

the code that does the looping is:
For Each tbl In db.TableDefs
rst.Seek "=", tbl.Name
If Not rst.NoMatch Then tbl.Properties("Description") =
IIf(IsNull(rst![Description]), "", rst![Description])
Next tbl

how else could i achieve my goal?

Thanks for any help!
ben
 
Back
Top