This question demonstrates how to do it with VBA, but I would like to do it with PowerShell, so it can be scripted and scheduled.
I expected the implementation should be identical as they both allegedly use the same COM but it seems QueryDefs
is not defined in the COM that PowerShell accesses as it returns nothing.
# Open file in Access.
$accessCOM = New-Object -ComObject Access.Application
$accessCOM.OpenCurrentDataBase($dbPath, $false)
# get list of queries
$queries = $accessCOM.CurrentDB.QueryDefs # returns nothing.
# export query to file
ForEach ($query in $queries) {
$name = $query.name
$sql = $query.sql
$dest = "$queryDest\$name.sql"
Write-Output $sql > $dest
}
I can use .CurrentData.AllQueries
but that returns a collection of AccessObjects and I don't know how to get the SQL from them. Is there any other way to get the QueryDefs or at least the SQL from the name?