Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 297980 - Last Review: July 13, 2004 - Revision: 3.4
How To Use ADOX to Create an AutoNumber Field of Replication ID Field Size
This article was previously published under Q297980
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 "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
- 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.
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:
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
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate