我有两个查询连接各自表中的值(tbl1& tb2)。如果没有匹配,则使用宏运行追加查询。 我遇到的问题是在VBA中找出正确的语法(因为我想使用自己的警告消息而不是标准的Access消息)。
我已将宏转换为VBA,我目前正在使用以下代码:
DoCmd.SetWarnings False
MsgBox "Append Successful", vbOKCancel, "Append"
DoCmd.OpenQuery "appendQuery"
DoCmd.SetWarnings True
主要问题是,无论附加是否成功,我仍然会收到相同的消息。有没有办法说出类似的话:
If 'concat1' matches 'concat2'
MsgBox "Record already exists. Append not executed"
Else run 'appendQuery'
MsgBox "Append Successful"
答案 0 :(得分:0)
使用vanilla Access来实现这一目标的最佳方式是实际上从一个完全不同的方向。首先根据Insert(append)查询的过滤条件执行Update查询,如果db.RecordsAffected为0,则记录不存在,您可以插入。
set db = CurrentDb
'we don't actually care about changing things, just want to see if the record exists
db.Execute("UPDATE sometable SET somevalue = somevalue WHERE ID = 1234")
If db.RecordsAffected = 0 Then
'id doesn't exist
db.Execute("INSERT INTO sometable (id,somevalue) VALUES (1234,'MyValue')")
Else
'id exists, handle the fact that it exists
End if
答案 1 :(得分:0)
查询看起来有点奇怪,但应该完全按照您的要求进行:
Dim db as DAO.Database
Dim SQL as string
SQL = "INSERT INTO blah (id, somevalue) " & _
"SELECT TOP 1 1, 'somevalue' FROM blah " & _
"WHERE NOT EXISTS (SELECT id FROM blah WHERE id=1 AND somevalue='somevalue');"
set db = currentdb
db.execute SQL
if db.RecordsAffected = 0 then
msgbox "Record already exists; append not executed."
else
msgbox "Record appended successfully."
end if