Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

PowerShell To Create Access Database

$
0
0
I found this other answer to this question, which I modified to be the following:

Function Create-DataBase($Db){
 $application = New-Object -ComObject Access.Application
 $application.NewCurrentDataBase($Db,10)
 $application.CloseCurrentDataBase()
 $application.Quit()
}
Function Invoke-ADOCommand($Db, $Command){
 $connection = New-Object -ComObject ADODB.Connection
 $connection.Open("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=$Db")
 $connection.Execute($command)
 $connection.Close()
}
$Db = "C:\ScheduledCodeRun\CollatedData\Database.mdb"
$table = "Table1"
$Fields = "F1 Counter, F2 Date, F3 Integer, F4 Text"
$command = "Create Table $table($fields)"
If(Test-Path $Db){
    Write-Host 'DB already exists' -fore green
}else{
    Create-DataBase $db
    Invoke-ADOCommand $Db 'Create Table Table1(F1 Counter, F2 Date, F3 Integer, F4 Text)'
}

*Note: I changed the $connection.Open("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=$Db") line to make the code work.

When I run that script, it creates the new Access database fine, a new table fine, but the column heading are "F1", "F2", "F3", and "F4". And if I change the text in the code to my custom names, the table won't create at all.

Can anyone help with what I'm doing wrong please?

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>