Access VBA Docmd.OpenQuery可以运行更新查询,但Currentdb.Excecute无法执行

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

我如下创建了一个保存的更新查询,它具有控制值和IIf功能。

UPDATE SYS_AAAA_AAAH 
SET SYS_AAAA_AAAH.AAK = AAA & " " & AAB & IIf(IsNull(AAC),"","(" & AAC & ")") & IIf(IsNull(AAF),""," not null") & " comment '" & AAH & "',"
WHERE (((SYS_AAAA_AAAH.AAO)=[forms]![frmAdmiTabl]![CombSAAO]));

DoCmd.OpenQuery可以运行它,而Currentdb.Execute给出错误消息“参数太少”。我创建了另一个保存的更新查询,而没有来自控件或函数的输入,并且Currentdb.Execute正常运行。我不想看到Docmd.OpenQuery发出的警告消息,也不想通过打开和关闭警告来弄乱。无论如何要使Currentdb.Execute都可以处理?

access-vba updates dao
1个回答
0
投票

[当您想用另一列的值和IIf功能更新某些记录的列时,最好使用DAO.recordset editupdate

  Dim Rs_AAAH As DAO.Recordset

    Set Rs_AAAH = CurrentDb.OpenRecordset("select * from Table where AAO='" & Me.CombSAAO.Value & "'", dbOpenDynaset)

    Rs_AAAH.MoveFirst

    Do Until Rs_AAAH.EOF

        With Rs_AAAH
            .Edit
            .Fields("AAK").Value = Rs_AAAH.Fields("AAA") & " " & Rs_AAAH.Fields("AAB") & IIf(IsNull(Rs_AAAH.Fields("AAC")), "", "(" & Rs_AAAH.Fields("AAC") & ")") & IIf(IsNull(Rs_AAAH.Fields("AAF")), "", " not null") & " comment '" & Rs_AAAH.Fields("AAH") & "',"
            .Update
        End With

        Rs_AAAH.MoveNext
    Loop

    Rs_AAAH.Close
    Set Rs_AAAH = Nothing
© www.soinside.com 2019 - 2024. All rights reserved.