update table properties automatically

lala

Registered User.
Local time
Yesterday, 19:41
Joined
Mar 20, 2002
Messages
741
is there a way to update table properties automatically?
either through VB or some other way

what i mean by PROPERTIES is when you right click on the table and click PROPERTIES and you can type your table description there


can i put that descriprion there automatically?

thank you
 
You can use this Function if you like which will change the properties of Table Fields (including he Description property). Place all the code below into a Database Code Module:


Code:
Public Function SetTableFieldProperty(DBName As String, TableName As String, TableFieldName As String, _
                                     strPropertyName As String, intType As Integer, _
                                     varValue As Variant, Optional ReturnError As String) As Boolean
[COLOR="DarkGreen"]   ' -----------------------------------------------------------------------------
   ' Description:
   ' ===========
   ' Purpose:
   ' Set or change a Table Field Property, creating the property if necessary.
   '
   ' Parameters:
   ' ==========
   ' DBName           = Path and name of the Database which contains
   '                    the table of the field property we want to
   '                    modify (change or add). If a empty string is
   '                    passed then the Database which calls this function
   '                    is assumed to contain the specified Table.
   ' TableName        = The of the Table which contains the field
   '                    property we want to change.
   ' TableFieldName   = The table Field whose property should be set.
   ' strPropertyName  = The name of the property to set.
   ' intType          = The type of property (needed for creating)
   '                    {See: PROPERTIES FOR TABLE  FIELD posted below}
   ' varValue         = The value to set this property to.
   ' ReturnError      = Allow you to supply an Custom Error message which is
   '                    prefixed any error displayed.
   '
   ' Syntax:
   ' ======
   ' MsgBox SetTableFieldProperty("", "Mailing List", "FirstName", "Decription", _
                                 10, "The Users First Name")
   '
   ' RETURNS:
   ' =======
   '  0 = Failure - most likely due to an Error of some kind.
   ' -1 = Successfull
   ' -2 = The Existing Table (old table) specified can't be
   '      found in the specified database.
   ' -3 = The New Table name specified already exists within
   '      the specified Database.
   ' -----------------------------------------------------------------------------
   '
   '    PROPERTIES FOR TABLE  FIELD
   ' Property Name    &   Value    Type
   '=====================================
   ' Description         Type: 10 (Text)
   ' Value               Type: 10 (Text)
   ' Attributes          Type: 4  (Long)
   ' CollatingOrder      Type: 3  (Integer)
   ' Type                Type: 3  (Integer)
   ' Name                Type: 12 (Memo)
   ' OrdinalPosition     Type: 3  (Integer)
   ' Size                Type: 4  (Long)
   ' Format              Type: 10 (Text)
   ' InputMask           Type: 10 (Text)
   ' Indexed             Type: 3  (Integer)
   ' SourceField         Type: 12 (Memo)
   ' SourceTable         Type: 12 (Memo)
   ' RowSource           Type: 12 (Memo)
   ' ValidateOnSet       Type: 1  (Boolean)
   ' DataUpdatable       Type: 1  (Boolean)
   ' ForeignName         Type: 12 (Memo)
   ' DefaultValue        Type: 12 (Memo)
   ' ValidationRule      Type: 12 (Memo)
   ' ValidationText      Type: 12 (Memo)
   ' Required            Type: 1  (Boolean)
   ' AllowZeroLength     Type: 1  (Boolean)
   ' FieldSize           Type: 4  (Long)
   ' OriginalValue       Type: 10 (Text)
   ' VisibleValue        Type: 10 (Text)
   ' Caption             Type: 12 (Memo)
   ' BoundColumn         Type: 3  (Integer)
   ' ColumnCount         Type: 3  (Integer)
   ' ColumnWidth         Type: 3  (Integer)
   ' ColumnOrder         Type: 3  (Integer)
   ' ColumnHidden        Type: 1  (Boolean)
   ' DisplayControl      Type: 3  (Integer)  In The LookUp Property Section
   '                              (Set to 109 for TextBox)
   '                              (Set to 110 for ListBox)
   '                              (Set to 111 for ComboBox)
   ' UnicodeCompression  Type: 1  (Boolean)
   ' IMEMode             Type: 2  (Byte)
   '                              0  =  No Control
   '                              1  =  On
   '                              2  =  Off
   '                              3  =  Disable
   '                              4  =  Hiragana
   '                              5  =  Full pitch Katakana
   '                              6  =  Half pitch Katakana
   '                              7  =  Full pitch Alpha/Num
   '                              8  =  Half pitch Alpha/Num
   '                              9  =  HangulFull
   '                              10 =  Hangul
   ' IMESentenceMode     Type: 2  (Byte)
   '                              0  =  Normal          (Phrase Predict)
   '                              1  =  Plural          (Plural Clause)
   '                              2  =  Speaking        (Conversation)
   '                              3  =  No Conversion   (None)
   ' GUID                Type: 9  (Binary)
   ' -----------------------------------------------------------------------[/COLOR]
   On Error GoTo ErrHandler
   Dim db As Database
   Dim tDef As TableDef
   Dim obj As Field
   Dim idx As Index
   
   If DBName <> "" Then
      If DoesFileExist(DBName) = False Then Exit Function
      Set db = OpenDatabase(DBName)
   Else
      Set db = CurrentDb
   End If
   Set tDef = db.TableDefs(TableName)
   
   If DoesTableExist(TableName, DBName) = False Then Exit Function
   
   For Each obj In tDef.Fields
     If obj.Name = TableFieldName Then
        ' Set the Indexed property
        If strPropertyName = "Indexed" Then
           Set idx = tDef.CreateIndex(TableFieldName)
           idx.Fields.Append idx.CreateField(TableFieldName)
           Select Case varValue
              Case 0    'Idexed property set to 'No'
                 idx.Unique = Null
              Case 1    'Idexed property set to 'Yes (No Duplicates)'
                 idx.Unique = True
              Case 2    'Idexed property set to 'Yes (Duplicates OK)'
                 idx.Unique = False
           End Select
           tDef.Indexes.Append idx
        ElseIf TableFieldHasProperty(obj, strPropertyName) Then 'other properties
           obj.Properties(strPropertyName) = varValue
        Else
           obj.Properties.Append obj.CreateProperty(strPropertyName, intType, varValue)
        End If
        SetTableFieldProperty = True
     End If
  Next obj

ExitHandler:
   db.Close
   Set db = Nothing
   Set tDef = Nothing
   Set obj = Nothing
   Set idx = Nothing
   Exit Function

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

Public Function DoesFileExist(PathStrg As String) As Integer
    Dim a$
    On Error Resume Next
    a$ = Dir(PathStrg, 14)
    If a$ <> "" And Err = 0 Then DoesFileExist = -1 Else Err = 0
End Function

Public Function DoesTableExist(TableName As String, Optional DBName As String, Optional LogSuccess As String, _
                               Optional LogFailure As String) As Boolean
   ' ---------------------------------------------------------------------
   ' Returns:
   ' True (-1) if Table Exists (logged as [1]).
   ' False (0) if table does not Exist.
   ' ---------------------------------------------------------------------
   Dim db As Database
   Dim i As Integer
   Dim tbl As String
   
   If DBName <> "" Then
      Set db = OpenDatabase(DBName)
   Else
      Set db = CurrentDb
   End If
   
   db.TableDefs.Refresh
   tbl = Trim(TableName)
   If Left$(tbl, 1) = "[" And Right$(tbl, 1) = "]" Then
      tbl = Mid$(tbl, 2, Len(tbl) - 2)
   End If
   For i = 0 To db.TableDefs.Count - 1
       If tbl = db.TableDefs(i).Name Then
           'Table Exists
           DoesTableExist = True
           Exit For
       End If
   Next i
   db.Close
   Set db = Nothing
End Function

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

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

The Function to Carry out the task is named SetTableFieldProperty and as an example of how to use it:

Code:
Call SetTableFieldProperty("", "Mailing List", "FirstName", "Decription", _
                                 10, "The Users First Name")

The parameters are explained within the Function with comments. There are also three other functions which must accompany the SetTableFieldProperty Function and they are DoesFileExist to make sure the specified Database (.mdb) file (if provided) actually exists, DoesTableExist to make sure the specified Table actually exists in the Database, and TableFieldHasProperty to make sure the specified property actually exists within the specified Table.

.
 
i haven't tried it yet, but i'm sure it will work

thank you very much
 
ok, i tried using it and can't get it done

i need to change the table's description
if you go into TABLES in access, chose VIEW to be DETAILS, and then you will see one of the columns is DESCRIPTION

this is the one i'm trying to change using VB

by looking at the code, there's a parameter there, TABLE FIELD NAME that i'm not sure what to set it as


also, how do i get it to work?
what command do i use before the function for it to add text in the description field?
 
I see now what you are trying to do. You want to add a Description to the Table within the Database Object Window.

Off hand....I'm afraid I can't help you. The code I posted is for changing the properties of a Field within a Table. Obviously no good for what you want to do.

But this now raises a question........why would you want to?

.
 
i have a few monthly projects that i automated

all the table names stay the same from month to month, the process is fully automated, and it creates the database with all the tables that i need

the problem is that aonce we start working with them, and sometimes we need 2 or 3 of the same tables from different months, it gets confusing
they used to name each table with the month name in the name
but since i automated it, the only way to do that is by hand

which is not a big deal, but i have more processes running based on those tables and again, they're automated and if every month that source table's names change - they won't be automated anymore

so i at least want to put month in the description, so we can always know which month this is

sorry, i'm probably not explaining this good at all
 
i have done this, but i cant recall where, and i cant find the code

i vaguely recollect it being like a user defined property, as i think they arent there original, but once created you cant get rid of them again.
 
Would it be easier to introduce another field and have that filled with current month or whatever identifier. If you find code to change the description of the table then something will have to run it, so add a field instead and fill it with the identifier.

If you used a field with an identifier then you could do more with it. I don't think you could do anything with the "description" of the table.
 
i did find it, and will post it when i get to a computer

as far as a month, yes, if i was the only one using this, i'd probably not need anything at all

but they're used to seeng month names in the name of the file itself

as far as somthing having to run it, that's not a problem, i have a bunch of code running to create the tables, so few more lines don't matter

thank you everyone!!
 

Users who are viewing this thread

Back
Top Bottom