我试图对多个表格进行DELETE
查询(删除整行),但我无法让它工作。
代码的第一部分有效。这可确保导入的数据来自昨天或手动给定的任何其他日期。
Private AutoDate As Date
Private NewDate As String
Private Db As Database
Sub GetDate() ' DATUM BEPALEN
AutoDate = Date - 1
MsgBox (AutoDate), (vbYesNo), ("Datum")
If MsgBox(AutoDate, vbYesNo, "Datum") = vbYes Then
NewDate = AutoDate
Call DeleteDate
Else ' No
Call ManDate
End If
End Sub
Sub ManDate()
NewDate = InputBox("Gelieve de correcte datum in te voeren", "Datum wijzigen", AutoDate)
If StrPtr(NewDate) = 0 Then
MsgBox ("Gelieve opnieuw te beginnen")
Else
MsgBox (NewDate)
Call DeleteDate
End If
End Sub
但是现在我想首先删除该日期的激活数据,以防止在追加时出现双打(或错误)。要附加4个不同的表,我必须将DELETE
查询运行到4个不同的表。所以现在我试图把它放在一个数组中。但那就是出错的地方。
RUN-TIME ERROR 3075:查询中的语法错误(缺少运算符) 表达' Stock_CC ='。
现在我发现问题出在NewDate
如果在Msgbox(Newdate)
行开始之前插入SQL
,我就会得到正确的日期。
否则不是......
cName
应为Stock_CC.InputDate
,tname
应为Stock_CC
。
Private Sub ClearRecord()
Dim AllTables(0 To 3, 0 To 2) As String
AllTables(0, 0) = "Stock_CC"
AllTables(0, 1) = "Stock_CC.InputDate"
AllTables(1, 0) = "Wips_CC"
AllTables(1, 1) = "Wips_CC.InputDate"
AllTables(2, 0) = "CCA_cc"
AllTables(2, 1) = "CCA_cc.InputDate"
AllTables(3, 0) = "Eps_cc"
AllTables(3, 1) = "Eps_cc.InputDate"
Dim i As Integer For i = LBound(AllTables) To UBound(AllTables)
DeleteDate AllTables(0, i), AllTables(i, 0)
Next i
End Sub
Sub DeleteDate(cName As String, tName As String) Dim SQL As String
SQL = "DELETE " & cName & " FROM " & tName & " WHERE " & cName & "=" & NewDate & " ;"
DoCmd.RunSQL SQL
End Sub
我做错了什么?
答案 0 :(得分:2)
VBA无法扩展字符串中的变量。您需要将字符串连接(连接)在一起。也许这就是你想要的?
SQL = "DELETE FROM " & tName & " WHERE " & cName & " = '" & NewDate.ToString("yyyy-MM-dd") & "';"
最终,您希望生成的字符串看起来像这样:
DELETE FROM Stock_CC WHERE Stock_CC.InputDate = '2015-01-01';