我对vba相当新(对于这个网站 - 如果我发布错误,请道歉),以前是一个SQL开发人员,但遗憾的是我的新工作只涉及到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;
您将对上述任何帮助表示感谢。
非常感谢
答案 0 :(得分:0)
此代码正在尝试执行SQL语句:更新表1
cmdT.CommandText = "Update Table 1"
cmdT.CommandType = adCmdText
cmdT.Execute
执行名为更新表1 的查询,您需要以下代码:
Docmd.OpenQuery "Update Table 1"
如果您不想要有关即将更新的行数的警告,
With DoCmd
.SetWarnings False
.OpenQuery "Update Table 1"
.SetWarnings True
End With
这是我用来使用访问项目和ADODB
运行参数化查询的代码Dim cmdl As ADODB.Command
Dim StrCon As New ADODB.Connection
Dim rsRecSet As New ADODB.Recordset
StrCon.Open CurrentProject.Connection
'Set CONNECTION timeout property
StrCon.CommandTimeout = 0
'Create a new command object to process the stored proc
Set cmdl = New ADODB.Command
With cmdl
.ActiveConnection = StrCon
'set COMMAND timeout property - query can time out on either the connection OR the command
.CommandTimeout = 0
.CommandText = "spCrossTabRun"
.CommandType = adCmdStoredProc
.Parameters.Refresh
.Parameters(1).value = "MyValue1"
.Parameters(2).value = "MyValue2"
.Parameters(3).value = "MyValue3"
Set rsRecSet = .Execute()
End With
答案 1 :(得分:0)
我有几个集中函数可以返回对adodb记录集的只读或读/写访问权限。像:
Public Function dbWrite(SQLQuery As String, Optional blDynamic As Boolean) As ADODB.Recordset
'Centralized function to read data from db, return a EDITABLE recordset
Dim r As New ADODB.Recordset
If blDynamic = True Then
r.Open SQLQuery, CurrentProject.Connection, adOpenDynamic, adLockOptimistic, dbSQLPassThrough
Else
r.Open SQLQuery, CurrentProject.Connection, adOpenForwardOnly, adLockOptimistic, dbSQLPassThrough
End If
Set dbWrite = r
Exit Function
End function
所以在新功能中调用它:
Public Function SomeFunction
Dim r As New ADODB.Recordset
Dim s As String
set r = dbwrite("SELECT * FROM TABLE;")
s = "My Parameterized Value"
r.AddNew
r![Fieldname] = s
r.Update
r.Close
set r = nothing
End function
使用dbWrite函数可以简化操作,并且一次解析一个字段名称可以轻松调用另一个函数,一个简单的IF或其他东西。