尝试从Excel中更新Access表中的logging

我认为我的VBA应该像你在下面看到的那样,但是它不工作。 我收到一条错误消息,内容如下:“没有给出一个或多个必需参数的值”。

Sub Execute_UpdateQuery() Dim NumOfRec As Integer Dim strPath As String Dim rCell As Range Dim rRng As Range Dim sht As Worksheet Dim LastRow As Long DBFullName = ThisWorkbook.Path & "\Stakeholder.accdb" Set cn = CreateObject("ADODB.Connection") cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";" NumOfRec = 0 Dim i As Integer, j As Integer With Worksheets("Temp") Set sht = ThisWorkbook.Worksheets("Temp") LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row i = 11 Set rRng = Sheet1.Range("A11:A" & LastRow) For Each rCell In rRng.Cells For j = 2 To 8 Debug.Print .Cells(i, j).Value TheUpdate = .Cells(10, j).Value cn.Execute "UPDATE ALLL_HISTORY SET " & TheUpdate & " = '" & .Cells(i, j).Value & _ "' WHERE DESC1 = " & "'" & .Cells(i, 1).Value & "'", , adExecuteNoRecords NumOfRec = NumOfRec + 1 Next j i = i + 1 Next rCell End With MsgBox (NumOfRec & " records were updated.") cn.Close Set conn = Nothing End Sub 

这些图像可能有帮助。

在这里输入图像说明

在这里输入图像说明

有什么想法可能是错的? 这应该是非常接近!

当您将3-SYN传递给SQL时,它会尝试将此numeric value 3 minus value of SYNparsing为numeric value 3 minus value of SYN – 其中SYN不是列名,因此被假定为参数。

该错误表明它无法find该参数。 使用引号括起来的值告诉SQL它是文本,并且应该解决你的问题。

 cn.Execute "UPDATE ALLL_HISTORY SET " & TheUpdate & " = '" & .Cells(i, j).Value & _ "' WHERE DESC1 = " & .Cells(i, 1).Value, NumOfRec, adExecuteNoRecords