SQL日期在字符串中的问题

时间:2017-05-01 09:09:45

标签: sql excel vba

我有一个问题。我是sql的新手。在VBA(excel)中更有经验。所以我创建了一个带有文本框(带日期)的用户表单,我需要将它们传输到我的数据库。所以我正在使用insterttert声明。它适用于所有其他值,当我想将日期文本框添加到我的字符串中时。我收到语法错误。希望有人能提供帮助。

strQuery = "INSERT INTO[Database$] (date,allocation,hours,reason,team)" & _
vbCrLf & "VALUES (" & "#" & Chr$(39) & Firstdate & Chr$(39) & "#" & "," & 
Chr$(39) & Name_Abs & Chr$(39) & "," & Chr$(39) & Hours_t & Chr$(39) & "," & 
Chr$(39) & Reason & Chr$(39) & "," & Chr$(39) & Team & Chr$(39) & ");"

如果有人可以帮助我那会很棒......

1 个答案:

答案 0 :(得分:1)

在评论中,你问了太多(无关)的问题。我只回答你的“主要”问题。

  • 使用#附上您的日期,但不附加chr(39)

  • 纠正语法错误;缺少_来连接你的陈述。

应用这个,并假设其他一切都没问题,你应该查询:

strQuery = "INSERT INTO [Database$] (date,allocation,hours,reason,team)" & _
vbCrLf & "VALUES (" & "#" & firstDate & "#," & _
Chr$(39) & Name_Abs & Chr$(39) & "," & Chr$(39) & Hours_t & Chr$(39) & "," & _
Chr$(39) & Reason & Chr$(39) & "," & Chr$(39) & Team & Chr$(39) & ");"

修改

如图所示,某些字段名称中有空格(即“开始日期”而不是“日期”),请用“[]”括起来。优良的做法是系统地将其应用于所有领域。

strQuery = "INSERT INTO [Database$] ([start date], [allocation], [hours],[reason], [team])" & _
vbCrLf & "VALUES (" & "#" & firstDate & "#," & _
Chr$(39) & Name_Abs & Chr$(39) & "," & Chr$(39) & Hours_t & Chr$(39) & "," & _
Chr$(39) & Reason & Chr$(39) & "," & Chr$(39) & Team & Chr$(39) & ");"