vba多条件查询更新EXCEL数据导access数据库
功能:根据项目号和步骤ID,更新指定步骤完成时间。这里里边要匹配两个条件一个是项目ID “”projectID“” 另一个是 “”StepID“” 步骤ID。
Sub SaveFini() Dim rs As New ADODB.Recordset Dim cnn As String Dim sql As String Dim a(7) As String Dim s(7) As String pn = ThisWorkbook.Sheets("A3").Range("AF4").Value 'The location of the cell where the completion date is locate a(1) = "U7" a(2) = "U16" a(3) = "U32" a(4) = "U39" a(5) = "AG7" a(6) = "AG29" a(7) = "AG42" With ThisWorkbook.Sheets("A3") 'Extract the step name s(1) = Left(.Range("L7").Value, 6) s(2) = Left(.Range("L16").Value, 6) s(3) = Left(.Range("L32").Value, 6) s(4) = Left(.Range("L39").Value, 6) s(5) = Left(.Range("X7").Value, 6) s(6) = Left(.Range("X29").Value, 6) s(7) = Left(.Range("X42").Value, 6) cnn = "Provider=Microsoft.ACE.OLEDB.16.0;" & _ "Data Source=" & ThisWorkbook.Path & ".\A3db2019.accdb" On Error Resume Next For n = 1 To 7 sql = "select StepID,FiniDate,ProjectID from A3_Plan where projectID='" & pn & "'and StepID='" & s(n) & "'" Set rs = New Recordset rs.Open sql, cnn, 2, 3 rs.Fields("FiniDate") = .Range(a(n)).Value rs.Update Next End With End Sub