Excel VBA ADO 更新 SQL Table/Record
Excel VBA ADO UPDATE SQL Table/Record
我已经设法更新 SQL table 并使用此 SQL 字符串进行记录
"UPDATE Breach_Test_Key SET [VAL_BREACH_REASON] = 'SOME BREACH REASON' WHERE [ID] = 1"
我正在努力实现的两件事是:
- 更新SQLTable中的两个特定列,如何定义两个
SET 中的列?
- 我还需要更新 Excel 中 table 中的所有记录
进入 SQL table(它们将全部存在于 SQL table)。
ID 字段将始终匹配,因为数据来自此 table。
有人可以指导我完成 SQL 结构吗?
感谢@MatteoNNZ 对第 1 部分的帮助,这是我现在用来更新多列的代码
uSQL = "UPDATE Breach_Test_Key SET [VAL_BREACH_REASON] = 'SOME BREACH REASON1',[VAL_BREACH_DETAIL] = 'SOME BREACH DETAIL1' WHERE [ID] = 1"
所以对于第二部分,我实际上还没有足够的东西 post 但是我在字符串中指定了一个值的地方我宁愿它是动态的循环遍历 excel table/column。有什么指点吗?
好的,我得到了我想要的结果。
正在使用 vba 中的循环选项按预期更新记录。
这可能不是实现我想要实现的目标的最漂亮方法,但它确实有效。
如果有人想提出改进建议,我会洗耳恭听。
但这是我的最终宏:
Private Sub UpdateTable()
Dim cnn As ADODB.Connection
Dim uSQL As String
Dim strWard As String
Dim strDate As Date
Dim strUser As String
'=====USER NAME VBA================
Set WSHnet = CreateObject("WScript.Network")
UserName = WSHnet.UserName
UserDomain = WSHnet.UserDomain
Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
UserFullName = objUser.FullName
'===================================
'strDate is a stamp of the date and time when the update button was pressed
strDate = Format(Now(), "dd/mm/yyyy hh:mm:ss")
'User is the username of the person logged into the PC the report is updated from
strUser = UserFullName
'Set Connection string
Set cnn = New Connection
cnnstr = "Provider=SQLOLEDB; " & _
"Data Source=ServerName; " & _
"Initial Catalog=dbName;" & _
**USER DETAILS HERE**
'Connect and Run the SQL statement to update the reords.
cnn.Open cnnstr
Dim row As Range
For Each row In [tbl_data].Rows
uSQL = "UPDATE Breach_Test_Key SET [VAL_BREACH_REASON] = '" & (row.Columns(row.ListObject.ListColumns("VAL_BREACH_REASON").Index).Value) & _
"' ,[VAL_BREACH_DETAIL] = ' " & (row.Columns(row.ListObject.ListColumns("VAL_BREACH_DETAIL").Index).Value) & _
"' WHERE [ID] = '" & (row.Columns(row.ListObject.ListColumns("ID").Index).Value) & "'"
'Debug.Print uSQL
cnn.Execute uSQL
Next
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub
我已经设法更新 SQL table 并使用此 SQL 字符串进行记录
"UPDATE Breach_Test_Key SET [VAL_BREACH_REASON] = 'SOME BREACH REASON' WHERE [ID] = 1"
我正在努力实现的两件事是:
- 更新SQLTable中的两个特定列,如何定义两个 SET 中的列?
- 我还需要更新 Excel 中 table 中的所有记录 进入 SQL table(它们将全部存在于 SQL table)。
ID 字段将始终匹配,因为数据来自此 table。
有人可以指导我完成 SQL 结构吗?
感谢@MatteoNNZ 对第 1 部分的帮助,这是我现在用来更新多列的代码
uSQL = "UPDATE Breach_Test_Key SET [VAL_BREACH_REASON] = 'SOME BREACH REASON1',[VAL_BREACH_DETAIL] = 'SOME BREACH DETAIL1' WHERE [ID] = 1"
所以对于第二部分,我实际上还没有足够的东西 post 但是我在字符串中指定了一个值的地方我宁愿它是动态的循环遍历 excel table/column。有什么指点吗?
好的,我得到了我想要的结果。 正在使用 vba 中的循环选项按预期更新记录。 这可能不是实现我想要实现的目标的最漂亮方法,但它确实有效。
如果有人想提出改进建议,我会洗耳恭听。 但这是我的最终宏:
Private Sub UpdateTable()
Dim cnn As ADODB.Connection
Dim uSQL As String
Dim strWard As String
Dim strDate As Date
Dim strUser As String
'=====USER NAME VBA================
Set WSHnet = CreateObject("WScript.Network")
UserName = WSHnet.UserName
UserDomain = WSHnet.UserDomain
Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
UserFullName = objUser.FullName
'===================================
'strDate is a stamp of the date and time when the update button was pressed
strDate = Format(Now(), "dd/mm/yyyy hh:mm:ss")
'User is the username of the person logged into the PC the report is updated from
strUser = UserFullName
'Set Connection string
Set cnn = New Connection
cnnstr = "Provider=SQLOLEDB; " & _
"Data Source=ServerName; " & _
"Initial Catalog=dbName;" & _
**USER DETAILS HERE**
'Connect and Run the SQL statement to update the reords.
cnn.Open cnnstr
Dim row As Range
For Each row In [tbl_data].Rows
uSQL = "UPDATE Breach_Test_Key SET [VAL_BREACH_REASON] = '" & (row.Columns(row.ListObject.ListColumns("VAL_BREACH_REASON").Index).Value) & _
"' ,[VAL_BREACH_DETAIL] = ' " & (row.Columns(row.ListObject.ListColumns("VAL_BREACH_DETAIL").Index).Value) & _
"' WHERE [ID] = '" & (row.Columns(row.ListObject.ListColumns("ID").Index).Value) & "'"
'Debug.Print uSQL
cnn.Execute uSQL
Next
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub