Microsoft KB Archive/297980

= How To Use ADOX to Create an AutoNumber Field of Replication ID Field Size =

Article ID: 297980

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Data Access Components 2.1
 * Microsoft Data Access Components 2.1 Service Pack 2
 * Microsoft Data Access Components 2.1 Service Pack 1
 * Microsoft Data Access Components 2.1 Service Pack 2
 * Microsoft Data Access Components 2.5
 * Microsoft Data Access Components 2.5 Service Pack 1
 * 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.



Step-by-Step Example
 Create a new Standard EXE project in Visual Basic. Form1 is added to the project by default. 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. Place a CommandButton control onto Form1.  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 &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Db1.mdb&quot;

Set cat = New ADOX.Catalog Set cat.ActiveConnection = cn   Set tblnam = New ADOX.Table Set clx = New ADOX.Column

tblnam.Name = &quot;ReplicationIDTest&quot; clx.ParentCatalog = cat clx.Type = adGUID clx.Name = &quot;IDField&quot; clx.Properties(&quot;AutoIncrement&quot;) = False clx.Properties(&quot;Fixed Length&quot;) = True clx.Properties(&quot;Jet OLEDB:AutoGenerate&quot;) = True clx.Properties(&quot;Jet OLEDB:Allow Zero Length&quot;) = True tblnam.Columns.Append clx tblnam.Columns.Append &quot;DataField&quot;, adInteger cat.Tables.Append tblnam

Set clx = Nothing Set cat = Nothing cn.Close Set cn = Nothing End Sub  Modify the cat.ActiveConnection assignment to point to a valid Microsoft Access Database file. 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.</ol>

<div class="references_section">