Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

Appending propertly to column not working ACCESS 2010

I need to append a displycontrol property to a newly created column in a table.  This is done with vba and I have the below, but it is not recognizing the table name.  


Set prp = CurrentDb.tblPerson("AAPerson").CreateProperty("DisplayControl", dbInteger, acCheckBox)
AAPerson.Properties.Append prp
Avatar of Norie
Norie

What error message are you actually getting?
Have you looked at TableDefs?

PS Sorry for the disjointed post, on phone.
Avatar of Sandra Smith

ASKER

that property already existed, so I changed it to be the below.  I want the display for the YesNo to be a checkbox. It goes through without throwing an error, but does not change the displaycontrol to a check box, it is still text.

CurrentDb.TableDefs("tblPerson").Fields("AAPerson").Properties("DisplayControl") = acCheckBox
Norie, no problem, so am I!
You are planning to do a whack of this stuff.
I think firing up TableDefs is going to be a better fit
I don't do a lot of DDL, but I think this is close
You need a tabledef, then the field, and you create the property of the field
Allen Browne's done the heavy lifting here
http://allenbrowne.com/func-dao.html#SetPropertyDAO

Dim TD As TableDef
dim prp as Property
dim fld as field
Set TD = CurrentDb.TableDefs("tblPerson")
set fld as TD.fields("AAPerson")
fld.Properties.Append fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)

If it exists, then you just need to change it's value
fld.Properties("DisplayControl") =acCheckBox
Yes, I came to that conclusion to created the below.  It is based on a procedure that does work, but now I get an error message Property Not Found, but the column does exist in the table, but with a text box for the YesNo.

Public Sub TestAAPerson()
On Error GoTo ErrorHandler
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim strTableName As String
    Dim rst As DAO.Recordset
    Dim i As Integer
   
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblPerson1")
        With rst
            For i = 1 To .Fields.Count - 1
                   If .Fields(i).Name = "AAPerson" Then    'Only modify one field
                        .Fields(i).Properties("DisplayControl") = acCheckBox
                   End If
             Next i
        End With

Exit_Errorhandler:
rst.Close
Set rst = Nothing
Set tdf = Nothing
Set dbs = Nothing
   
    Exit Sub
ErrorHandler:
    MsgBox "Error Number: " & Err.Number & " Description: " & Err.Description
    Resume Exit_Errorhandler
End Sub
I tried the below and still does notwork
Public Sub TestAAPerson()
'On the tblPerson, want to change DisplayControl to a checkbox rather
'than a text box for the YesNo column type.
On Error GoTo ErrorHandler
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim strTableName As String
    Dim rst As DAO.Recordset
    Dim i As Integer
    Dim fld As DAO.Field
   
Set dbs = CurrentDb
Set tdf = dbs.TableDefs("tblPerson")
'Set fld = tdf.Fields("AAPerson")
Set rst = dbs.OpenRecordset("tblPerson1")
        With rst
            For i = 1 To .Fields.Count - 1
                   If .Fields(i).Name = "AAPerson" Then    'Only modify Text fields
                   Set fld = tdf.Fields("AAPerson")
                   Call SetPropertyDAO(fld, "DisplayControl", dbInteger, CInt(acCheckBox))
                   Exit Sub
                   End If
             Next i
        End With

Exit_Errorhandler:
rst.Close
Set rst = Nothing
Set tdf = Nothing
Set dbs = Nothing
   
    Exit Sub
ErrorHandler:
    MsgBox "Error Number: " & Err.Number & " Description: " & Err.Description
    Resume Exit_Errorhandler
End Sub
After some hacking, this worked
Why the set statements had to go in favor of walking the collection to get it to work is a bit of a mystery

Option Compare Database
Option Explicit

Private Sub Command0_Click()
Dim TD As TableDef
Dim prp As Property
Dim fld As Field
For Each TD In CurrentDb.TableDefs
    If TD.Name = "tblPerson" Then
        For Each fld In TD.Fields
            If fld.Name = "AAPerson" Then
                If HasProperty(fld, "DisplayControl") = False Then
                    fld.Properties.Append fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
                Else
                    fld.Properties("DisplayControl") = acCheckBox
                End If
            End If
        Next fld
    End If
Next TD

 
End Sub

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

Open in new window

@Nick67, just a thought but instead of looping couldn't you use something like this?
Set TD = CurrentDb!tblPerson

Open in new window

If that works for the TableDef then perhaps something similar would work for the field.
More hacking made this work.
Note the need to actually instantiate a database object
CurrentDb just doesn't cut it for some reason.
Working sample posted

Dim db As Database
Dim TD As TableDef
Dim prp As Property
Dim fld As Field


Set db = CurrentDb
Set TD = db.TableDefs("tblPerson")
'here I am going to rub out AAPerson to get a clean slate
TD.Fields.Delete "AAPerson"
'here I'll append it as a Boolean field
TD.Fields.Append TD.CreateField("AAPerson", dbBoolean)
'and then make it a checkbox
Set fld = TD.Fields("AAPerson")
If HasProperty(fld, "DisplayControl") = False Then
    fld.Properties.Append fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
Else
    fld.Properties("DisplayControl") = acCheckBox
End If

'------------------------------------

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

Open in new window

test.mdb
Will try these first thing tomorrow, done with this for today out of sheer frustration and am heading home.
ASKER CERTIFIED SOLUTION
Avatar of Nick67
Nick67
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry have not worked on this, some medical issues came up.  At least this has gotten resolved, thank you.