简体   繁体   English

如何在访问 VBA 的 sql 命令中包含参数?

[英]How to include parameter in sql command in access VBA?

I am new to Access VBA.我是 Access VBA 的新手。 I am passing a parameter (t) which is the result of a function(myfunc) to a table(myTable), it has ID and time column.我正在传递一个参数 (t),它是一个函数 (myfunc) 的结果到一个表 (myTable),它有 ID 和时间列。 Here is my code.这是我的代码。 It does not work.它不起作用。 Where is the problem?问题出在哪里? Thanks.谢谢。

Sub test()
Dim timetem As String
Dim t As String * 50

timetem = DLookup("[time]", "myTable", "[ID]=1")

t = myfunc(timetem)

DoCmd.RunSQL "UPDATE myTable SET [time] ='" & t & "'WHERE [ID] =1;"

End Sub

Please post the specific error code you're getting.请发布您收到的特定错误代码。 There are two issues with your code, Firstly, what datatype is [time]?您的代码有两个问题,首先,[time] 是什么数据类型?

If the time column in the database is not a string, this code will not work.如果数据库中的时间列不是字符串,则此代码将不起作用。 You should match the data type of timetem with the data type of the [time] column in your DB.您应该将 timetem 的数据类型与数据库中 [time] 列的数据类型相匹配。

If time should be a date/time field then t should be escaped with # instead of '如果时间应该是日期/时间字段,那么 t 应该用 # 而不是 '

I think there should also be a space here:我认为这里也应该有一个空格:

"'WHERE [ID]

should probably be应该是

"' WHERE [ID]

Other pointers: You don't need to specify the size of t.其他指针:您不需要指定 t 的大小。 Actually if you're trying to pass a date type as a string you probably would need to use the Format() function.实际上,如果您尝试将日期类型作为字符串传递,您可能需要使用 Format() 函数。

Also, DLookup is evil.此外,DLookup 是邪恶的。

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

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