将日期插入 MS Access vba 查询
我正在尝试使用 MS Acess 中的 VBA 将当前日期插入到表中。无论我如何尝试,日期都会插入为 12/30/1899。有什么建议可以解决这个问题吗?这是我的代码
Dim StrSQL As String
Dim db As Database
Dim InID As Integer
Dim Inputcasedate As String
Dim InputCaseType_Id As Integer
InID = Me.PgmPart_ID
Inputcasedate = Date
InputCaseType_Id = 1
StrSQL = "INSERT INTO CaseNotes (PgmPart_ID,CaseType_ID,CaseDate) " & _
"VALUES (" & InID & "," & InputCaseType_Id & "," & Inputcasedate & ")"
I'm trying to insert the current date into a table using VBA in MS Acess. The date gets inserted as 12/30/1899 No matter what I try. Any advise to fix this? Here is my code
Dim StrSQL As String
Dim db As Database
Dim InID As Integer
Dim Inputcasedate As String
Dim InputCaseType_Id As Integer
InID = Me.PgmPart_ID
Inputcasedate = Date
InputCaseType_Id = 1
StrSQL = "INSERT INTO CaseNotes (PgmPart_ID,CaseType_ID,CaseDate) " & _
"VALUES (" & InID & "," & InputCaseType_Id & "," & Inputcasedate & ")"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 MS Access 中,文字日期值必须由井号/数字/井号标签符号分隔:
#
。但是,与 VBA 一样,Access SQL 维护
Date()
函数。因此,请在 SQL 语句内使用此表达式并避免连接 VBA 变量。但是,请考虑使用参数化 SQL< 的行业最佳实践/a> MS Access 的 DAO 通过 QueryDefs.Parameters。这样做不需要值分隔符,例如字符串的引号或日期的数字,并在应用程序层 (VBA) 和数据库之间对齐数据类型。
In MS Access, literal date values must be delimited by pound/numeral/hashtag symbols:
#
.However, like VBA, Access SQL maintains the
Date()
function. So use this expression inside the SQL statement and avoid the concatenated VBA variable.However, consider the industry best practice with parameterized SQL which MS Access's DAO supports via QueryDefs.Parameters. Doing so, requires no value delimiters like quotes for strings or numerals for dates and aligns data types between app layer (VBA) and database.