How To Use ADOX to Create an AutoNumber Field of Replication ID Field Size (297980)



The information in this article applies to:

  • Microsoft Data Access Components 2.1
  • Microsoft Data Access Components 2.1 (GA)
  • Microsoft Data Access Components 2.1 SP1
  • Microsoft Data Access Components 2.1 SP2
  • Microsoft Data Access Components 2.5
  • Microsoft Data Access Components 2.5 SP1
  • Microsoft Data Access Components 2.6

This article was previously published under Q297980

SUMMARY

You can use the Microsoft Access user interface to create an AutoNumber field, in which the field size can be Long Integer or Replication ID. If you select Replication ID as the field size, a globally unique identifier (GUID) is automatically generated each time a new record is added to the table.

This article demonstrates how to use the ActiveX Data Objects Extensibility (ADOX) model to programmatically create an AutoNumber field with the Replication ID field size.

MORE INFORMATION

Step-by-Step Example

  1. Create a new Standard EXE project in Visual Basic. Form1 is added to the project by default.
  2. From the Project menu, click References. From the list of available components, select the Microsoft ActiveX Data Objects 2.1 and Microsoft ADO Ext. 2.1 for DDL and Security check boxes.
  3. Place a CommandButton control onto Form1.
  4. Paste the following code in the Declarations section of Form1:
    Option Explicit
    
    Private Sub Command1_Click()
    
        Dim cn As ADODB.Connection
        Dim clx As ADOX.Column
        Dim cat As ADOX.Catalog
        Dim tblnam As ADOX.Table
        
        Set cn = New ADODB.Connection
        cn.CursorLocation = adUseClient
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Db1.mdb"
    
        Set cat = New ADOX.Catalog
        Set cat.ActiveConnection = cn
        Set tblnam = New ADOX.Table
        
        Set clx = New ADOX.Column
    
        tblnam.Name = "ReplicationIDTest"
        clx.ParentCatalog = cat
        
        clx.Type = adGUID
        clx.Name = "IDField"
        clx.Properties("AutoIncrement") = False
        clx.Properties("Fixed Length") = True
        clx.Properties("Jet OLEDB:AutoGenerate") = True
        clx.Properties("Jet OLEDB:Allow Zero Length") = True
        tblnam.Columns.Append clx
        tblnam.Columns.Append "DataField", adInteger
        cat.Tables.Append tblnam
    
        Set clx = Nothing
        Set cat = Nothing
        cn.Close
        Set cn = Nothing
    End Sub
    					
  5. Modify the cat.ActiveConnection assignment to point to a valid Microsoft Access Database file.
  6. Run the project, and click Command1. Note that a table called ReplicationIDTest is created in the database. View the table in Design Mode, and notice that the IDField field definition is AutoNumber, and the Field Size is Replication ID.

REFERENCES

For more information, see the "Data Types" section of Appendix A in the Microsoft Jet Engine Programmer's Guide at the following Microsoft Web site:

Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbhowto KB297980