[英]My Microsoft Access Append Query (VBA and SQL) works for a literal, but not for a variable
This is Microsoft Access, VBA and SQL related.这是 Microsoft Access、VBA 和 SQL 相关的。
In VBA I make this SQL statement (see below) and run it.在 VBA 中,我创建了这个 SQL 语句(见下文)并运行它。 This correctly appends to testTable.这正确地附加到 testTable。 (The field in testTable is Date/Time and it has format 'mm/dd/yyyy hh:nn:ss') (testTable 中的字段是日期/时间,格式为 'mm/dd/yyyy hh:nn:ss')
sql = "INSERT INTO testTable (DateOpen) VALUES ('12/01/2021 12:13:14')
However, when I do this (below), the code prompts me with a "Enter Parameter Value" for dateVar.但是,当我这样做(如下)时,代码会提示我为 dateVar 输入“输入参数值”。 Why?为什么? I just told it what dateVar was in the SQL.:我刚刚告诉它 SQL 中的 dateVar 是什么:
DIM dateVar as string
dateVar = "12/01/2021 12:13:14"
sql = "INSERT INTO testTable (DateOpen) VALUES (dateVar)
When I add quotes (see below) the code does not prompt me with an Enter Parameter Value msgbox.当我添加引号(见下文)时,代码不会提示我输入参数值 msgbox。 I get a msgbox that says You Are About To Append.我收到一个消息框,上面写着你即将到达 Append。 But when it tries to append it fails with "Microsoft Access Can't Append the Records" due to a "type conversion error"但是,当它尝试 append 时,由于“类型转换错误”,它会失败并显示“Microsoft Access Can't Append the Records”
DIM dateVar as string
dateVar = "12/01/2021 12:13:14"
sql = "INSERT INTO testTable (DateOpen) VALUES ('dateVar')
Help Please??请帮忙??
Your concatenation of the dateVar
variable into the SQL string needs to be fixed:您需要修复将dateVar
变量连接到 SQL 字符串中:
Dim dateVar as string
dateVar = "12/01/2021 12:13:14"
sql = "INSERT INTO testTable (DateOpen) VALUES ('" & dateVar & "')"
If you're having problem building your SQL then Debug.Print sql
will let you see what's actually getting executed.如果您在构建 SQL 时遇到问题,那么Debug.Print sql
会让您看到实际执行的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.