3

I have the following PowerShell code which adds a new line of data into a MS Access database (based on user input) and works perfectly.

if ($NewAccounts ="Y") {
  $cursor = 3
  $lock = 3

  $Ado = New-Object -ComObject ADODB.Connection 
  $recordset = New-Object -ComObject ADODB.Recordset

  $Ado.Open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=$Source")

  $query = "Select * from [Sheet1]"

  $recordset.Open($query, $ado, $cursor, $lock)

  $recordset.AddNew() 

  $recordset.Fields.Item("Account") = $AccName
  $recordset.Fields.Item("Serial") = $CGBSerial
  $recordset.Fields.Item("SAExpiry") = $SAEDate.ToString("dd/MM/yyyy")
  $recordset.Fields.Item("SAValidatedPerson") = $SAPerson
  $recordset.Fields.Item("DataCollection") = $DCRun
  $recordset.Fields.Item("DataCollectionDate") = $DCRunDate
  $recordset.Fields.Item("DataCollectionPerson") = $DCPerson
  $recordset.Fields.Item("Version") = $Version
  $recordset.Fields.Item("VersionDateValidated") = Get-Date -Format d
  $recordset.Fields.Item("VersionValidatedPerson") = $logontrim

  $recordset.Update()

  $recordset.Close() 
  $ado.Close()
}

However, I cannot seem to update a row in the database that already exists. Is it possible to update a row, rather than creating an entirely new row?

2
  • "I cannot seem to" is a very vague description. Do you get error messages? If so please post them Commented Apr 9, 2017 at 14:25
  • I should have specified that I could not work out the syntax at all - it was more about understanding how the command should be overall. Commented Apr 10, 2017 at 17:34

1 Answer 1

6

$recordset.AddNew() appends a new empty record to the recordset. To update an existing record you need to navigate to the record you want to modify first, and then change the values of that record.

$recordset.Open($query, $ado, $cursor, $lock)

while ($recordset.Fields.Item('Account').Value -ne $AccName) {
  $recordset.MoveNext()
}

$recordset.Fields.Item('Serial') = $CGBSerial
...

$recordset.Update()
$recordset.Close()

However, you can't use MoveNext() with a static cursor, so you need to change the cursor type to adOpenForwardOnly ($cursor = 0).

Alternatively you could use a prepared statement:

$cn = New-Object -ComObject 'ADODB.Connection'
$cn.ConnectionString = "..."
$cn.Open()

$cmd = New-Object -ComObject 'ADODB.Command'
$cmd.CommandText = 'UPDATE [Sheet1] SET Serial=?, SAExpiry=?, ... WHERE Account=?'
$cmd.Parameters.Append($cmd.CreateParameter('@p1', 200, 1, 50, $CGBSerial))
$cmd.Parameters.Append($cmd.CreateParameter('@p2', 7, 1, $null, $SAEDate))
...

$cmd.Execute()

$cn.Close()
Sign up to request clarification or add additional context in comments.

2 Comments

That works absolutely perfectly - I used your first example with $recordset.Open... Thank you for such a rapid response. would there be an easy way to delete a record using the same method?
@PowershellPauper Please check the documentation. $recordset.Delete(1) might work, but I haven't tested it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.