GSWilson77
02-14-2013, 06:17 AM
Hi,
I'm fairly new to vba (and to this website - so apologies if I post this incorrectly), previously a SQL developer, but unfortunately my new job involves working in access 2010 only. I'm trying to build a vba macro which will run various update statements. I have built the queries in access and I'm trying to call the query in vba. I have tried doing this with one access query and I'm running inot a run time error - syntax error in UPDATE statement. The query runs fine in access but the vba function fails with the runtime error.
The following is my vba function:
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
When I debug the function the error occurs on line cmdt.Execute.
The sql query (Update Table 1) it is executing is as follows:
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;
Any help you can give on the above would be gratefully appreciated.
Many thanks
GW
EDIT : ADDED VBA TAGS Tommy
I'm fairly new to vba (and to this website - so apologies if I post this incorrectly), previously a SQL developer, but unfortunately my new job involves working in access 2010 only. I'm trying to build a vba macro which will run various update statements. I have built the queries in access and I'm trying to call the query in vba. I have tried doing this with one access query and I'm running inot a run time error - syntax error in UPDATE statement. The query runs fine in access but the vba function fails with the runtime error.
The following is my vba function:
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
When I debug the function the error occurs on line cmdt.Execute.
The sql query (Update Table 1) it is executing is as follows:
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;
Any help you can give on the above would be gratefully appreciated.
Many thanks
GW
EDIT : ADDED VBA TAGS Tommy