繁体   English   中英

VBA 代码中 WHERE 关键字的正确语法语法是什么

[英]What is the correct syntax Syntax for WHERE keyword in VBA Code

我正在构建一个字符串,它将在我的访问数据库中执行 sql 。 为了正确引用来源,我使用Allen Brown Audit的指南作为模板来保存对我的主数据库的更改。

这是目前给我带来麻烦的代码:

' If this was not a new record, save the old values.
If Not bWasNewRecord Then
    sSQL = "INSERT INTO " & sAudTmpTable & " ( audType, audDate, audUser, ID, Campus, Building, Room, `Device Type`, Model, Domain, `Serial Tag`, Barcode, HostName, `Custodian Name`, Notes ) " & _
        "SELECT 'EditFrom' AS Expr1, Now() AS Expr2, NetworkUserName() AS Expr3, " & sTable & "ID, Campus, Building, Room, `Device Type`, Model, Domain, `Serial Tag`, Barcode, Hostname, `Custodian Name`, Notes " & _
        "FROM " & sTable & " WHERE " & sTable & "." & sKeyField & " = " & lngKeyValue & ";"
    db.Execute sSQL, dbFailOnError
End If

目前它返回“参数太少。预期为 1。”的错误,这让我认为是 WHERE 子句和之后的字符串连接导致了它。 我的主要问题是这个问题是否是语法问题,我只需要在某处转义或添加双引号,还是语句的逻辑问题,以及我正在尝试做的事情:这是 select 来自条件,更改记录的 ID 与数据库中的 ID 匹配? 欢迎任何注释或评论,并感谢您的时间。

如果您使用的是 SQL 服务器,您将需要类似

SELECT Room
, [Device Type]
, Model
, Domain
, [Serial Tag]

对于其他 RDBMS(可能是 JetDB = Access),您需要引号而不是方括号。 (引号实际上也适用于 SQL 服务器,我通常只使用方括号。)

SELECT Room
, "Device Type"
, Model
, Domain
, "Serial Tag"

...对于区分大小写的 RDBMS(如 Oracle),请引用所有内容(无论如何都不会受到伤害)...

SELECT "Room"
, "Device Type"
, "Model"
, "Domain"
, "Serial Tag"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM