Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Append DisplayControl Property to Yes/No Field in VBA

Posted on 2011-09-14
8
Medium Priority
?
1,221 Views
Last Modified: 2012-05-12
Hello Experts!

I have the following function:
Public Function CreateTable(strTableName As String, rsTableInfo As DAO.Recordset) As Boolean
'Purpose: Create Table based on input criteria
'Requires: Desired Name and recordset
'           Recordset = ColumnName, FieldType
'Returns: True if table is created

'On Error GoTo HandleErr
Dim dbs As Database
Dim tbl As TableDef
Dim fld As DAO.Field
Dim prp As DAO.Property

CreateTable = False

Set dbs = CurrentDb

'First delete table if exists
DropTable strTableName

Set tbl = dbs.CreateTableDef(strTableName)

'Loop through recordset of table particular information
With rsTableInfo
    If .RecordCount <> 0 Then 'stuff is here!
        .MoveFirst
        Do While Not .EOF
            Set fld = tbl.CreateField(!ColumnName, !ColumnType)
            
            If !ColumnType = dbBoolean Then 'need to set display property to checkbox
                Set prp = fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
                fld.Properties.Append prp
            End If
            
            tbl.Fields.Append fld
            
            .MoveNext
        Loop
    Else
        MsgBox "No Recordset data to create table.", vbInformation, "Error: No Data"
        GoTo Done
    End If
End With

dbs.TableDefs.Append tbl
dbs.TableDefs.Refresh


CreateTable = True
Done:
On Error Resume Next
    dbs.Close
    Set dbs = Nothing
    Exit Function

HandleErr:
    MsgBox "Error " & Err.Number & " while creating table" & vbCrLf & Err.Description, vbCritical, "Error Creating Table"
    Resume Done
End Function

Open in new window


This function is passed a recordset that has the list of column names and column types that need to be created in a temp table.

Creation of each column works without a problem. However, when I tried to add code to change and dbBoolean (Yes/No) field to give it the DisplayProperty of a CheckBox (Versus the default of a text box that shows 0 for false and -1 for true) I receive an error message.

The error is on this line:
                Set prp = fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
                fld.Properties.Append prp
(specifically the error is thrown when i try to append the property)

Error: 3219, invalid operation


What is odd is if I remove the property code and let the table be created I can manually go into design mode and change the displaycontrol to a checkbox without a problem.

What is the cause of the append error? Am I missing something?

Thank you!
0
Comment
Question by:w00tw00t111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 48

Expert Comment

by:Dale Fye
ID: 36537733
have you tried appending the field to the tabledef before setting the DisplayControl property.
0
 

Author Comment

by:w00tw00t111
ID: 36537993
Yes and it threw the same error
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 36538041
Have you checked out the recommendations here or here

Both of these are EE questions that address your issue.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:w00tw00t111
ID: 36538218
My code effectively mimics both of those posts code however instead of hardcoding the column names I'm referencing the recordsets data.

Still isn't working :(
0
 

Author Comment

by:w00tw00t111
ID: 36538231
Public Sub test()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim pt As DAO.Property

Set db = CurrentDb
Set tbl = db.TableDefs("tmpExportList")
Set fld = tbl.Fields("TTechnology")
Set pt = fld.CreateProperty("displaycontrol", dbInteger, acCheckBox)

tbl.Fields("TTechnology").Properties.Append pt
db.TableDefs.Refresh

End Sub

Open in new window

Written as a separate sub this code works.

If I use the function posted above to create the table and then run this sub, it will change one column to checkbox fields.

However, there are several columns that need to be changed (and each time the procedure is run the number that need to be changed can vary)...

Still trying to pinpoint how to "loop" through the boolean columns and set their properties
0
 

Accepted Solution

by:
w00tw00t111 earned 0 total points
ID: 36538268
Got it!

Although not how I originally thought to do it, here's a sub that will run through all fields in a table and if it finds a dbBoolean field it sets the property accordingly

 
Public Sub test()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim pt As DAO.Property

Set db = CurrentDb
Set tbl = db.TableDefs("tmpExportList")
For Each fld In tbl.Fields
    
    If fld.Type = dbBoolean Then 'make checkbox
    'Set fld = tbl.Fields("TTechnology")
        Set pt = fld.CreateProperty("displaycontrol", dbInteger, acCheckBox)

        'tbl.Fields("TTechnology").Properties.Append pt
        fld.Properties.Append pt
    End If
Next fld

db.TableDefs.Refresh

End Sub

Open in new window

0
 

Author Closing Comment

by:w00tw00t111
ID: 36558787
Created solution
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 36538286
so you have tried:

Do While Not .EOF
    Set fld = tbl.CreateField(!ColumnName, !ColumnType)
    tbl.Fields.Append fld
    tbl.fields.Refresh
           
    If !ColumnType = dbBoolean Then 'need to set display property to checkbox
        Set prp = fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
        tbl.Fields(!ColumnName).Properties.Append prp
    end if
           
    .MoveNext
Loop
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question