ACC2002: How to Programmatically Specify the Display Control for a Field

Article translations Article translations
Article ID: 304274 - View products that this article applies to.
This article was previously published under Q304274
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.mdb).

Expand all | Collapse all

SUMMARY

This article shows you how to programmatically create a table and to specify the display control for a field. The sample code in this article creates the following table in the Northwind sample database:
Collapse this tableExpand this table
Field Name:Data Type:Display Control:
IDAutoNumber
YesNoYes/NoCheck Box

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.

  1. Start Access.
  2. On the Help menu, point to Sample Databases, and then click Northwind Sample Database.
  3. Create a new module, and then type or paste the following code:
    Function AddYesNoField()
    'This sample function creates a table named tblYesNo.
    'It adds an AutoNumber field named ID and a yes/no
    'field named YesNo, with the display control as a check box.
    
    Dim db As DAO.Database
    Dim t As TableDef
    Dim f1 As Field
    Dim f2 As Field
    Dim p As Property
    
    Set db = CurrentDb
    
    'Create the table.
    Set t = db.CreateTableDef("tblYesNo")
    
    'Create the ID field.
    Set f1 = t.CreateField("ID", dbLong)
    
    'ID field is AutoNumber.
    f1.Attributes = dbAutoIncrField
    t.Fields.Append f1
    
    'Create the YesNo field.
    Set f2 = t.CreateField("YesNo", dbBoolean)
    t.Fields.Append f2
    db.TableDefs.Append t
    Set f2 = db.TableDefs("tblYesNo").Fields("YesNo")
    
    'Set the DisplayControl property to Check Box.
    Set p = f2.CreateProperty("DisplayControl", dbInteger, 106)
    f2.Properties.Append p
    
    Set db = Nothing
    End Function
  4. Save the module as modMakeTable.
  5. In the Immediate Window type the following line, and then press ENTER:
    ?AddYesNoField()
  6. On the File menu, click Close and Return to Microsoft Access.
  7. Open the tblYesNo table.

    Note that this table has two fields that have the properties that are listed in the "Summary" section of this article.
NOTE: In the code, 106 indicates a check box. You can set other controls with the following values:
  • 109 - Text Box
  • 110 - List Box
  • 111 - Combo Box

Properties

Article ID: 304274 - Last Review: June 23, 2005 - Revision: 4.0
APPLIES TO
  • Microsoft Access 2002 Standard Edition
Keywords: 
kbhowto KB304274

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com