在ACCESS vba SQL中找不到类型不匹配错误

时间:2018-05-24 16:16:42

标签: vba access-vba

我有一个在ACCESS中运行的长查询,但是当我将代码转换为VBA时,我一直遇到类型不匹配错误。我已经重建了SQL至少5次,并检查了Having子句中的所有内容,所有数据类型都匹配。 VBA COde与qbe网格中生成的代码完全匹配。因为它是一个选择查询,我不得不把它伪装成它看起来像一张桌子。我创建了运行完美的小型查询。我只是看不到类型错误。以下是代码和执行它的命令:

Dim PivotMaster As String
PivotMaster = "SELECT dbo_Transaction_Table.Account_Number, 
dbo_Transaction_Table.Transaction_Type, dbo_Transaction_Table.Sku," & _
" dbo_Transaction_Table.Date_of_Transaction, 
 dbo_Master_Accounts.Account_Type_2," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'yyy') AS [Year]," & 
_
" Format([dbo_Transaction_Table]![Date_of_Transaction],'mmm') AS [Month]," & 
_
" Month([dbo_Transaction_Table]![Date_of_Transaction]) AS MonthNo," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'ddd') AS [Day]," & _
" Format([dbo_Transaction_Table]![Time_of_Transaction],'hh') AS [Hour]," & _
" dbo_Transaction_Table.Quantity, [dbo_Transaction_Table]![Amount] AS 
UnitPrice," & _
" dbo_Transaction_Table.Extension, dbo_Transaction_Table.ArAmt," & _
" dbo_Transaction_Table.Balls, dbo_Transaction_Table.Location_Id, 
dbo_Location_Table.Location_Type," & _
" dbo_Location_Table.Location_Description, [dbo_Location_Table]! 
[Location_Type] & " - " & [dbo_Location_Table]![Location_Id] AS Location" & 
_
" FROM ((dbo_Transaction_Table INNER JOIN dbo_Inventory_Table ON 
dbo_Transaction_Table.Sku = dbo_Inventory_Table.Sku)" & _
" INNER JOIN dbo_Location_Table ON dbo_Transaction_Table.Location_Id = 
dbo_Location_Table.Location_Id)" & _
" INNER JOIN dbo_Master_Accounts ON dbo_Transaction_Table.Account_Number = 
dbo_Master_Accounts.Master_ID" & _
" GROUP BY dbo_Transaction_Table.Account_Number," & _
" dbo_Transaction_Table.Transaction_Type, dbo_Transaction_Table.Sku," & _
" dbo_Transaction_Table.Date_of_Transaction, 
dbo_Master_Accounts.Account_Type_2," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'yyyy'), 
Format([dbo_Transaction_Table]![Date_of_Transaction],'mmm')," & _
" Month([dbo_Transaction_Table]![Date_of_Transaction]), 
Format([dbo_Transaction_Table]![Date_of_Transaction],'ddd')," & _
" Format([dbo_Transaction_Table]![Time_of_Transaction],'hh'), 
dbo_Transaction_Table.Quantity, [dbo_Transaction_Table]![Amount]," & _
" dbo_Transaction_Table.Extension, dbo_Transaction_Table.ArAmt, 
dbo_Transaction_Table.Balls, dbo_Transaction_Table.Location_Id," & _
" dbo_Location_Table.Location_Type, dbo_Location_Table.Location_Description, 
[dbo_Location_Table]![Location_Type] & " - " & [dbo_Location_Table]! 
[Location_Id]" & _
" HAVING (((dbo_Transaction_Table.Transaction_Type)<>3) AND 
((dbo_Transaction_Table.Extension)>0))"

CurrentDb.CreateQueryDef "PivotMaster", PivotMaster
DoCmd.OpenQuery "PivotMaster"

由于

1 个答案:

答案 0 :(得分:0)

我刚刚复制并粘贴了您的代码,但如果它们出现在真实版本中,则会删除会导致问题的换行符。不知道他们是如何进入那里的,但是如果问题是:

Dim PivotMaster As String
PivotMaster = "SELECT dbo_Transaction_Table.Account_Number," & _ 
" dbo_Transaction_Table.Transaction_Type, dbo_Transaction_Table.Sku," & _
" dbo_Transaction_Table.Date_of_Transaction, dbo_Master_Accounts.Account_Type_2," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'yyy') AS [Year]," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'mmm') AS [Month]," & _
" Month([dbo_Transaction_Table]![Date_of_Transaction]) AS MonthNo," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'ddd') AS [Day]," & _
" Format([dbo_Transaction_Table]![Time_of_Transaction],'hh') AS [Hour]," & _
" dbo_Transaction_Table.Quantity, [dbo_Transaction_Table]![Amount] AS UnitPrice," & _
" dbo_Transaction_Table.Extension, dbo_Transaction_Table.ArAmt," & _
" dbo_Transaction_Table.Balls, dbo_Transaction_Table.Location_Id," & _
" dbo_Location_Table.Location_Type, dbo_Location_Table.Location_Description," & _
" [dbo_Location_Table]![Location_Type] & ' - ' &" & _
" [dbo_Location_Table]![Location_Id] AS Location" & _
" FROM ((dbo_Transaction_Table INNER JOIN dbo_Inventory_Table ON" & _
" dbo_Transaction_Table.Sku = dbo_Inventory_Table.Sku)" & _
" INNER JOIN dbo_Location_Table ON dbo_Transaction_Table.Location_Id =" & _
" dbo_Location_Table.Location_Id)" & _
" INNER JOIN dbo_Master_Accounts ON dbo_Transaction_Table.Account_Number =" & _  
" dbo_Master_Accounts.Master_ID" & _
" GROUP BY dbo_Transaction_Table.Account_Number," & _
" dbo_Transaction_Table.Transaction_Type, dbo_Transaction_Table.Sku," & _
" dbo_Transaction_Table.Date_of_Transaction, dbo_Master_Accounts.Account_Type_2," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'yyyy')," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'mmm')," & _
" Month([dbo_Transaction_Table]![Date_of_Transaction])," & _
" Format([dbo_Transaction_Table]![Date_of_Transaction],'ddd')," & _
" Format([dbo_Transaction_Table]![Time_of_Transaction],'hh')," & _
" dbo_Transaction_Table.Quantity, [dbo_Transaction_Table]![Amount]," & _
" dbo_Transaction_Table.Extension, dbo_Transaction_Table.ArAmt," & _
" dbo_Transaction_Table.Balls, dbo_Transaction_Table.Location_Id," & _
" dbo_Location_Table.Location_Type, dbo_Location_Table.Location_Description," & _
" [dbo_Location_Table]![Location_Type] & ' - ' [dbo_Location_Table]![Location_Id]" & _
" HAVING (((dbo_Transaction_Table.Transaction_Type)<>3) AND" & _
" ((dbo_Transaction_Table.Extension)>0))"

CurrentDb.CreateQueryDef "PivotMaster", PivotMaster
DoCmd.OpenQuery "PivotMaster"

为了澄清(如您所知),如果您因任何原因(例如构建SELECT语句)将字符串放在VBA中,则需要关闭显式引号并添加连接符号,然后是下划线告诉VBA,无论该行是什么进程,下一行都是相同的部分,然后点击回车以获得换行符。

end of explicit quote text" & _输入。

修复此问题会显示一些对我没有意义的明确引用(以及缺少引用)。我猜你的意图是你想在几个地方找SELECT [dbo_Location_Table]![Location_Type] & " - " & [dbo_Location_Table]![Location_Id] AS Location之类的东西。 SQL字符串中的双引号会混淆VBA,并且它认为你想从另一个中减去字符串的一部分,这可能会给你一个类型不匹配。我用单引号替换了它们,SQL可以理解,但允许你在显式引用中做出明确的引用,从而避免了这种混淆。