Microsoft Access Database Development With VBA

ADO Topics: Creating a Table

   

Description

To create a table in ADO, formulate a SQL statement that creates a table and let ADO execute it. To execute a SQL statement in ADO, call the Execute() method of the Connection class. Its syntax is:

Connection.Execute ExecString, RecordsAffected, Options 

In this syntax, the SQL statement that creates the database can be passed as the first argument. The other two arguments are optional.

Examples

Here is an example that uses an Application.CurrentProject.Connection:

Private Sub cmdTable_Click()
    Dim conCurrent As ADODB.Connection
    
    Set conCurrent = Application.CurrentProject.Connection
    
    conCurrent.Execute "CREATE TABLE Customers(FirstName Text, " & _
                       "                       LastName Char);"
    
    MsgBox "A table named Customers has been created."
    conCurrent.Close
End Sub

Here is an example that creates a simple table with one column:

Private Sub cmdCreateTable_Click()
    Dim conDatabase As ADODB.Connection
    
    Set conDatabase = CurrentProject.AccessConnection
    conDatabase.Execute "CREATE TABLE Employees(FirstName Text(50));"
End Sub

Here is an example that creates a table with two columns:

Private Sub cmdCreateTable_Click()
    Dim conDatabase As ADODB.Connection
    
    Set conDatabase = CurrentProject.AccessConnection
    conDatabase.Execute "CREATE TABLE Employees(FirstName Text(50), " & _
                        "                       Lastname Text(50));"
End Sub
 
 
     
 

Home Copyright © 2013-2015, FunctionX, Inc. Home