New Access Table

I have already presented a function to create a new access table but I wasn’t very happy with it because we had to supply the full table creation SQL script.

I have altered that function so it creates an empty table. We can then use the Add-AccessColumn function to add columns. This also means I have a function to add columns to a table as and when I need them.

The function to create a table becomes

001
002
003
004
005
006
007
008
009
010
function New-AccessTable {
## assumes database is open
param (
    [string]$table,
    [System.Data.OleDb.OleDbConnection]$connection
)
    $sql = " CREATE TABLE $table"
    $cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
    $cmd.ExecuteNonQuery()
}

 

Our parameters become the connection and a table name.  I don’t check that the table name exists – that will be a future refinement. We then issue a simple CREATE TABLE command.

Leave a Reply