VBA中的语法错误SQL ACCDB执行期望'DELETE','INSERT','SELECT,'PROCEDURE'或'UPDATE'

问题描述 投票:0回答:1

我想创建一个按钮来计算Outlook中的操作。

我有以下代码,但遗憾的是它不起作用:

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection

conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\UHD-GEOBAN\SPOC und Incident\Screenshots\Sven\VBA\statistik.accdb; Persist Security Info=False;"
conn.Open
'DB: ID; currentDate; spocmail; onlinebankingmail
conn.Execute ("IF (NOT EXISTS(select * from tbl_statistics WHERE currentDate = '" & Date & "'))BEGIN INSERT INTO tbl_statistics(currentDate, spocmail) VALUES('" & Date & "', '1') END ELSE BEGIN UPDATE tbl_statistics SET spocmail = spocmail + 1 WHERE currentDate = '" & Date & "' END")

我还想知道,如果可以将长SQL语句放在VBA代码中的多行中

谢谢你

sql vba ms-access access-vba
1个回答
0
投票
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection

conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\UHD-GEOBAN\SPOC und Incident\Screenshots\Sven\VBA\statistik.accdb; Persist Security Info=False;"
conn.Open
set rs = new ADODB.Recordset
rs.open "Select * from tbl_statistics where currentdate = #" & format(date(),"dd mmm yyyy") & "#" ,conn
'unless you're in the US not specifying the date format will bite you
if rs.eof then 
     conn.execute "INSERT INTO tbl_statistics(currentDate, spocmail) VALUES('#" & format(Date,"dd mmm yyyy")  & "#', '1') 

else
   conn.execute "UPDATE tbl_statistics SET spocmail = spocmail + 1 WHERE currentDate = '#" & format(Date,"dd mmm yyyy" & "#"
end if
© www.soinside.com 2019 - 2024. All rights reserved.