我对 vba (和这个网站 - 如果我发布不正确,请道歉),以前是 SQL 开发人员,但不幸的是我的新工作只涉及在 access 2010 中工作。我正在尝试构建一个将运行各种更新语句的 vba 宏。我已经在访问中构建了查询,我正在尝试在 vba 中调用查询。我曾尝试使用一个访问查询来执行此操作,但我遇到了运行时错误 - UPDATE 语句中的语法错误。查询在访问中运行良好,但 vba 函数因运行时错误而失败。
以下是我的 vba 函数:
Public Function TestUpdate1()
Dim cmdT As ADODB.Command
Dim cnn As ADODB.Connection
Dim prmT As ADODB.Parameter
Set cnn = Application.CurrentProject.Connection
Set cmdT = New ADODB.Command
Set cmdT.ActiveConnection = cnn
cmdT.CommandText = "Update Table 1"
cmdT.CommandType = adCmdText
'Set prmT = cmdT.Parameters("Acc_Date")
'prmT.Value = #12/31/2012#
cmdT.Execute
'Set cmdT = Nothing
'
'If Err <> 0 Then
' cmdT.ActiveConnection.RollbackTrans
'Else
' cmdT.ActiveConnection.CommitTrans
'End If
End Function
当我调试函数时,错误发生在 cmdt.Execute 行。
它正在执行的sql查询(更新表1)如下:
UPDATE Table_1 SET Product = IIf(Contract Like "*budget*amt*","BUDGET Annual",
IIf(Contract Like "*CLASSIC*AMT*","CLASSIC Annual",
IIf(Contract Like "*essential*AMT*","ESSENTIAL Annual",
IIf(Contract Like "*P*PLUS*AMT*","Premier Plus Annual",
IIf(Contract Like "*SELECT*AMT*","SELECT Annual",
IIf(Contract Like "*prestige*AMT*","PRESTIGE Annual",
IIf(Contract Like "*GAP*","GAP Productl",
IIf(Contract Like "*SINGLE*TRIP*","SINGLE TRIP",
IIf(Contract Like "*premier*","PREMIER Annual",
IIf(Contract Like "*standard*","STANDARD Annual",
IIf(Contract Like "*EVAC*","European VAC","???"))))))))))), End_Date = Depart_Date+Days;
您可以就上述内容提供的任何帮助将不胜感激。
非常感谢