Upserting in MS-access

后端 未结 5 1623
天涯浪人
天涯浪人 2020-11-22 15:29

I need to write an SQL query for MS-Access 2000 so that a row is updated if it exists, but inserted if it does not. (I believe this is called an \"upsert\")

i.e.

5条回答
  •  无人共我
    2020-11-22 16:12

    You don't need to catch the error. Instead, just run the INSERT statement and then check

    CurrentDb.RecordsAffected
    

    It will either be 1 or 0, depending.

    Note: It's not good practice to execute against CurrentDB. Better to capture the database to a local variable:

    Dim db As DAO.Database
    Set db = CurrentDb
    db.Execute(INSERT...)
    If db.RecordsAffected = 0 Then
      db.Execute(UPDATE...)
    End If
    

提交回复
热议问题