数据类型不匹配错误

时间:2014-09-22 20:38:29

标签: vba ms-access

这是我目前的代码:

Dim SQL As String
Dim firstDate As Date
Dim firstemployee As String
Dim secondemployee As String
firstemployee = DLookup("[empName]", "[TempTable]", "[ID] = 1")
secondemployee = DLookup("[empName]", "[TempTable]", "[ID] = 2")
SQL = "INSERT INTO [Weeks on Call]([Primary Employee], [Backup Employee]," & _
      " [Week]) VALUES ('" + firstemployee + "','" + secondemployee + "'," & _
      "#" + firstDate + "#)"

表中的主要员工是文本字段

备份是文本字段,

周是日期/时间字段

我似乎无法弄清楚我哪里出错了。有人可以快点看一下吗?

由于

1 个答案:

答案 0 :(得分:1)

我看不到你为任何变量赋值。请尝试以下方法。

Dim strSQL As String
Dim firstDate As Date
Dim firstemployee As String
Dim secondemployee As String

firstDate = Date()
firstemployee = "Santa"
secondemployee = "Aimonce"

strSQL = "INSERT INTO [Weeks on Call] ([Primary Employee], [Backup Employee], [Week]) " & _
      "VALUES ('" & firstemployee & "', '" & secondemployee & "', " & Format(firstDate, "\#mm\/dd\/yyyy\#") & ")"
Debug.Print strSQL

查看您在即时窗口中打印的内容。它应该可以帮助您进行调试。