Skip to main content

How To Create a Table with Primary Key Through ADOX

ADOX is an extension to ActiveX Data Objects that allows the manipulation of the database schema. This article illustrates how to use ADOX to create a table and add a Primary Key.

NOTE: Not all OLE DB providers support the interfaces required to support ADOX methods. With those providers, you have to use Data Definition Queries or another object model to manipulate the database schema.

The first procedure in the example below creates a new table in an existing Microsoft Access database, creates a new field in that table, then creates a primary key index. When adding a single-field primary key, you do not need to use the ADOX Key object.

The second procedure utilizes the ADOX Key object to add a multiple field key to a table.

Steps to Create the Sample Application

  1. In Microsoft Visual Basic 5.0 or 6.0, create a new Standard EXE project. Form1 is created by default.
  2. On the Project menu, select References to add the following type libraries:
    Microsoft ActiveX Data Objects 2.1 Library
    Microsoft ADO Ext. 2.1 for DDL and Security
  3. Add two Command buttons (Command1 and Command2) and the following code to the Form1:
    Option Explicit

    Private Sub Command1_Click()
    '
    ' This code adds a single-field Primary key
    '
    Dim Cn As ADODB.Connection, Cat As ADOX.Catalog, objTable As ADOX.Table

    Set Cn = New ADODB.Connection
    Set Cat = New ADOX.Catalog
    Set objTable = New ADOX.Table

    'Open the connection
    Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=biblio.mdb"

    'Open the Catalog
    Set Cat.ActiveConnection = Cn

    'Create the table
    objTable.Name = "Test_Table"

    'Create and Append a new field to the "Test_Table" Columns Collection
    objTable.Columns.Append "PrimaryKey_Field", adInteger

    'Create and Append a new key. Note that we are merely passing
    'the "PimaryKey_Field" column as the source of the primary key. This
    'new Key will be Appended to the Keys Collection of "Test_Table"
    objTable.Keys.Append "PrimaryKey", adKeyPrimary, "PrimaryKey_Field"

    'Append the newly created table to the Tables Collection
    Cat.Tables.Append objTable

    ' clean up objects
    Set objKey = Nothing
    Set objTable = Nothing
    Set Cat = Nothing
    Cn.Close
    Set Cn = Nothing
    End Sub

    Private Sub Command2_Click()
    '
    ' This code adds a multi-field Primary Key
    '
    Dim Cn As ADODB.Connection, Cat As ADOX.Catalog
    Dim objTable As ADOX.Table, objKey As ADOX.Key

    Set Cn = New ADODB.Connection
    Set Cat = New ADOX.Catalog
    Set objTable = New ADOX.Table
    Set objKey = New ADOX.Key

    Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=biblio.mdb"
    Set Cat.ActiveConnection = Cn
    objTable.Name = "Test_Table2"

    objTable.Columns.Append "PrimaryKey_Field1", adInteger
    objTable.Columns.Append "PrimaryKey_Field2", adInteger

    objKey.Name = "PrimaryKey"
    objKey.Type = adKeyPrimary
    objKey.Columns.Append "PrimaryKey_Field1"
    objKey.Columns.Append "PrimaryKey_Field2"

    objTable.Keys.Append objKey

    Cat.Tables.Append objTable

    ' clean up objects
    Set objKey = Nothing
    Set objTable = Nothing
    Set Cat = Nothing
    Cn.Close
    Set Cn = Nothing
    End Sub
    NOTE: You might have to adjust the connect string to point to a valid Jet database.
  4. Run the application and click the Command buttons. You can check the table definitions for Test_Table and TestTable2 in Microsoft Access 97, Microsoft Access 2000, or the Visual Basic Visual Data Manager add-in

Comments

Popular posts from this blog

How To Use ADOX with Excel Data from Visual Basic or VBA

The ActiveX Data Objects Extensions for Data Definition Language and Security (ADOX) gives ActiveX Data Objects (ADO) developers the capabilities that are present in Data Access Objects (DAO) to manipulate databases, tables, fields and other database objects programmatically. Since the release of Microsoft OLE DB Provider for Jet version 4.0, which supports the ISAM drivers that the Jet engine uses with desktop databases other than Microsoft Access, ADO developers have been able to use both ADO and ADOX with Microsoft Excel workbooks and worksheets. However, because an Excel worksheet is not an ordinary database, ADO has some limitations when it is used with Excel. In particular, many ADOX methods either do not work or give unexpected results when they are used with Excel. This article documents many of the limitations of ADOX and Excel. For additional information about the use of ADO with Excel, click the article number below to view the article in the Microsoft Knowledge Base: 25781...

Get Address on the basis of Latitude and longitude using google

To get Address on the basis of Latitude and longitude using google you can use below code, it will ask for permission and then show the latitude and longitude in text box Use the below Script: Latitude: <input type="text" id="txtLatitude" value="18.92488028662047" /> Latitude: <input type="text" id="txtLongitude" value="72.8232192993164" /> <input type="button" value="Get Address" onclick="GetAddress()" /> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> function GetAddress() {     var lat = parseFloat(document.getElementById("txtLatitude").value);     var lng = parseFloat(document.getElementById("txtLongitude").value);     var latlng = new google.maps.LatLng(lat, lng);     var geocoder = geocoder = new google.maps.G...

Basic Linux Command

Minimize the current file in Vim to minimize the current open file in VIM editor in linux: ctrl + z Maximize the current file in Vim to maximize the current open file in VIM editor in linux: fg [1|2|...] Linux Command -------------- service httpd restart service firewalld status yum install vim nmtui // For Network Manager service network restart route -n yum install  httpd wget http://downloads2.goautodial.org/centos/7/goautodial.repo yum install wget yum install -y epel-release yum update -y yum -y groupinstall "Development Tools" yum install -y httpd php-common php-pdo php php-pear php-mbstring php-cli php-gd php-imap php-devel phpsysinfo php-mysql phpmyadmin mod_ssl mariadb mariadb-server mariadb-devel perl-DBI perl-DBD-MySQL perl-Digest-HMAC perl-YAML perl-ExtUtils-ParseXS perl-NetAddr-IP perl-Crypt-SSLeay perl-Curses perl-DBD-Pg perl-Module-ScanDeps perl-Text-CSV perl-HTML-Template perl-IO-Compres...

Connect us

Name

Email *

Message *