在花了好几个小时试图解决这个问题后,我不确定如何继续。我试图将电子表格中的数据添加到Access表中,但我不希望表中有任何重复项,我希望更新现有行的版本来替换现有行。我觉得我已经厌倦了互联网上与此问题相关的内容。我使用了以下Stack Overflow链接(How to check for record by using ID, then if record exists update if not add new record)中的线程帮助我解决了很多问题,但是我对代码的解释并没有做我需要做的事情。我没有语法错误;但是我的SQL语句(应该通过确定电子表格数据是否与当前数据库条目匹配来过滤记录集)在数据库表中每个电子表格条目至少有五个完全重复时返回一个空记录集。我完全不知道是什么问题。以下是我的代码:
Public Sub ADOFromExcelToAccess()
'Variable definition
InputDate = Tracking.Sheets("P&Q Weekly Summary").Range("E3").Value
Call SetDataConnection
Set DataRecordset = New ADODB.Recordset
With DataRecordset
For DataRowCount = 2 To TrackingLastRow
Part_Number = Tracking.Sheets("Operator Data").Range("A" & DataRowCount).Value
Week_Day = Tracking.Sheets("Operator Data").Range("B" & DataRowCount).Value
Weekday_Name = WeekdayName(Weekday(Week_Day))
Quantity = Tracking.Sheets("Operator Data").Range("C" & DataRowCount).Value
Machine = Tracking.Sheets("Operator Data").Range("G" & DataRowCount).Value
'Attempting to find duplicate entry
SQLCmd = "SELECT * FROM Raw_Data WHERE [Part] = '" & Part_Number & "' AND [Day] = " & Week_Day & " AND [Quantity] = " & Quantity & " AND [Machine] = '" & Machine & "';"
.Open Source:=SQLCmd, ActiveConnection:=DBConnection, CursorType:=adOpenDynamic, LockType:=adLockOptimistic, Options:=adCmdText
If (.BOF) Or (.EOF) Then
MsgBox .RecordCount
'No match found; add new record
.AddNew
.Fields("Part") = Part_Number
.Fields("Week") = InputDate
.Fields("Day") = Week_Day
.Fields("Weekday") = Weekday_Name
.Fields("Quantity") = Quantity
.Fields("Adjusted Quantity") = Tracking.Sheets("Operator Data").Range("D" & DataRowCount).Value
.Fields("Sample") = Tracking.Sheets("Operator Data").Range("E" & DataRowCount).Value
.Fields("Rejected") = Tracking.Sheets("Operator Data").Range("F" & DataRowCount).Value
.Fields("Machine") = Machine
.Fields("Cycle") = Tracking.Sheets("Operator Data").Range("H" & DataRowCount).Value
.Fields("Operator") = Tracking.Sheets("Operator Data").Range("I" & DataRowCount).Value
.Update
DataRowCount = DataRowCount + 1
'If current line already exists in database
Else
.Fields("Part") = Part_Number
.Fields("Week") = InputDate
.Fields("Day") = Week_Day
.Fields("Weekday") = Weekday_Name
.Fields("Quantity") = Quantity
.Fields("Adjusted Quantity") = Tracking.Sheets("Operator Data").Range("D" & DataRowCount).Value
.Fields("Sample") = Tracking.Sheets("Operator Data").Range("E" & DataRowCount).Value
.Fields("Rejected") = Tracking.Sheets("Operator Data").Range("F" & DataRowCount).Value
.Fields("Machine") = Machine
.Fields("Cycle") = Tracking.Sheets("Operator Data").Range("H" & DataRowCount).Value
.Fields("Operator") = Tracking.Sheets("Operator Data").Range("I" & DataRowCount).Value
.Update
DataRowCount = DataRowCount + 1
End If
.Close
Next DataRowCount
End With
Set DataRecordset = Nothing
DBConnection.Close
Set DBConnection = Nothing
End Sub
感谢您的帮助。我非常感激。
答案 0 :(得分:0)
我的SQL语句中的问题是日期标准。显然,每个MSDN(http://msdn.microsoft.com/en-us/library/dd627355(v=office.12).aspx#Access2007BuildingSQLStatements_UsingDateTimeCriteria)的日期和时间标准需要特殊语法。而不是以下WHERE子句:
WHERE [Day] = " & Week_Day & "
以下代码反映了这些变化,特别是必要的#:
WHERE [Day] = #" & Week_Day & "#
以上链接相当清楚地证明了这一点。如果不通过我的WHERE语句解析,我就不会发现这一点。除了具有日期标准的那个之外,所有其他都很好。问题解决了。