2014-09-22 181 views
0

這裏是我當前的代碼:數據類型不匹配錯誤

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 + "#)" 

表中的主要員工是一個文本字段,

備份是一個文本字段,

周是一個日期/時間字段

我似乎無法弄清楚我要出錯的地方。有人可以快速看一下嗎?

謝謝

+0

你嘗試使用Chr(35)而不是#嗎?因爲這個,我過去有過問題。不確定SQL語法。 – Peekay 2014-09-23 05:53:06

回答

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 

看看你在即時窗口打印什麼。它應該幫助你進行調試。

+0

抱歉,我忘記了包含該代碼!但無論如何,我發現問題是與日期的格式。我複製粘貼了你的代碼,它的工作。謝謝! – santaaimonce 2014-09-23 13:55:39

+0

最受歡迎!祝你好運@santaai​​monce – PaulFrancis 2014-09-23 13:56:49