简体   繁体   English

使用VBA ADODB连接更新SQL数据库

[英]Updating SQL Database Using VBA ADODB Connection

I've been using ADODB for SQL queries to return data and copy it from a recordset to a workbook for a while and a new task is to update records but I have no clue on how to do update a record. 我一直在使用ADODB进行SQL查询,以返回数据并将其从记录集复制到工作簿已有一段时间,并且新任务是更新记录,但是我不知道如何更新记录。

This is an example of my code: 这是我的代码的示例:

Dim con As ADODB.Connection
Dim rec As ADODB.Recordset

Set con = New ADODB.Connection
Set rec = New ADODB.Recordset

Dim sql As String

With con
    .Provider = "MSDASQL"
    .ConnectionString = "DSN=ukfast"
    .Open
End With

sql = "UPDATE crm_clients " & _
      "SET cheque_number = '" & chqNo & "' " & _
      "WHERE id = '' "

For Selecting data it was as easy as recordset.copyFromRecordset , but I have no clue about pushing an update back up to the database. 对于选择数据,它与recordset.copyFromRecordset一样容易,但是我不知道将更新推回数据库的任何线索。 I tried the .update method but that only works for the record set itself not the database. 我尝试了.update方法,但它仅适用于记录集本身而不适用于数据库。 I've also looked for some sort of execute method but come up short. 我也一直在寻找某种执行方法,但是简短。

What is the correct approach for updating a record using VBA? 使用VBA更新记录的正确方法是什么?

您可以为此使用连接对象的Execute方法:

con.Execute(sql)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM