[AccessD] Adding Caption Property

max.wanadoo at gmail.com max.wanadoo at gmail.com
Mon Oct 1 14:14:41 CDT 2007


John,
Here is an example from the help file.  I am sure you can work through this
to do what you want.
I *think* that perhaps the Caption field is shown in Table Design view, even
though it has not actually been created, which is why when I run code like
this:

  Dim dbs As DAO.Database, sql as string 
  Set dbs = CurrentDb
  sql = "Create Table Table2 (MyDesc Text, MyNumber number)"
  dbs.Execute (sql)

And then look at it in Table Design view, I can see the Caption Property,
but I cannot see it in code UNLESS I first of all type a value into the
Caption Field, at which time (I think) Access actually creates the property.

Below is some code which should give you what you want.

I am running Access 2002-2003 Version 11.0 SP2

Regards
Max

CreateProperty Method Example

This example tries to set the value of a user-defined property. If the
property doesn't exist, it uses the CreateProperty method to create and set
the value of the new property. The SetProperty procedure is required for
this procedure to run.

Sub CreatePropertyX()

   Dim dbsNorthwind As Database
   Dim prpLoop As Property

   Set dbsNorthwind = OpenDatabase("Northwind.mdb")

   ' Set the Archive property to True.
   SetProperty dbsNorthwind, "Archive", True
   
   With dbsNorthwind
      Debug.Print "Properties of " & .Name
      
      ' Enumerate Properties collection of the Northwind 
      ' database.
      For Each prpLoop In .Properties
         If prpLoop <> "" Then Debug.Print "  " & _
            prpLoop.Name & " = " & prpLoop
      Next prpLoop

      ' Delete the new property since this is a 
      ' demonstration.
      .Properties.Delete "Archive"

      .Close
   End With

End Sub

Sub SetProperty(dbsTemp As Database, strName As String, _
   booTemp As Boolean)

   Dim prpNew As Property
   Dim errLoop As Error

   ' Attempt to set the specified property.
   On Error GoTo Err_Property
   dbsTemp.Properties("strName") = booTemp
   On Error GoTo 0

   Exit Sub

Err_Property:

   ' Error 3270 means that the property was not found.
   If DBEngine.Errors(0).Number = 3270 Then
      ' Create property, set its value, and append it to the 
      ' Properties collection.
      Set prpNew = dbsTemp.CreateProperty(strName, _
         dbBoolean, booTemp)
      dbsTemp.Properties.Append prpNew
      Resume Next
   Else
      ' If different error has occurred, display message.
      For Each errLoop In DBEngine.Errors
         MsgBox "Error number: " & errLoop.Number & vbCr & _
            errLoop.Description
      Next errLoop
      End
   End If

End Sub




More information about the AccessD mailing list