我在Excel VBA中运行一些代码来查询数据库然后将数据带入Excel进行格式化。
我最后一次运行它(旧栗子)时运行正常但是今天我来运行月度报告并且它抛出了一个日期部分错误
为datepart指定的参数1无效
以下是代码:
StrQuery = "SELECT dbo_MANUFACTURER.COMPANY_NAME AS BRAND, dbo_COMPANY.COMPANY_NAME, dbo_AGENTS.SHORT_DESC AS AGENT, dbo_STOCK_SUB_TYPE.SHORT_DESC AS [STOCK TYPE], Replace(Replace(dbo_COMPANY.CURRENCY_ID,'110','EURO'),'1','GBP') AS [CURRENCY]," & _
"Sum(([Unit_Net]*[QTY_SOLD])-[DELIVERY_TOTAL]) AS [NET TOTAL], dbo_SORDER.DATE_CREATED, dbo_SORDER.SORDER_CODE FROM (((((dbo_COMPANY INNER JOIN dbo_SORDER ON dbo_COMPANY.COMPANY_ID = dbo_SORDER.COMPANY_ID) INNER JOIN " & _
"dbo_SORDER_ITEM ON dbo_SORDER.SORDER_ID = dbo_SORDER_ITEM.SORDER_ID) INNER JOIN dbo_STOCK ON dbo_SORDER_ITEM.STOCK_ID = dbo_STOCK.STOCK_ID) INNER JOIN dbo_STOCK_SUB_TYPE ON dbo_STOCK.SSTYPE_ID = dbo_STOCK_SUB_TYPE.SSTYPE_ID) INNER JOIN " & _
"dbo_AGENTS ON dbo_COMPANY.AGENT_ID = dbo_AGENTS.AGENT_ID) INNER JOIN dbo_MANUFACTURER ON dbo_STOCK.MANUF_ID = dbo_MANUFACTURER.MANUF_ID GROUP BY dbo_MANUFACTURER.COMPANY_NAME, dbo_COMPANY.COMPANY_NAME, dbo_AGENTS.SHORT_DESC, " & _
"dbo_STOCK_SUB_TYPE.SHORT_DESC, dbo_SORDER.DATE_CREATED, dbo_SORDER.SORDER_CODE, dbo_COMPANY.CURRENCY_ID, dbo_STOCK.MANUF_ID HAVING (((dbo_STOCK_SUB_TYPE.SHORT_DESC) <> " & "'EMBROIDERY'" & ") And ((Year([DATE_CREATED]) * 12 + " & _
"DatePart(" & "'m'" & ", [DATE_CREATED])) = Year(Date) * 12 + DatePart(" & "'m'" & ", Date) - 1)) ORDER BY dbo_COMPANY.COMPANY_NAME, dbo_SORDER.DATE_CREATED;"
为长线上的大量文字道歉。
如上所述,代码会引发错误:为日期部分指定的参数1无效。我试过将日期转换为论坛帖子中提到的但是没有成功的日期。
如果有人有任何令人惊讶的想法!
编辑:
删除datepart中 m 周围的引号在Access中不起作用。它弹出来询问 m 的值 我在Access中运行的SQL查询很好,我在下面添加了它:
SELECT
dbo_MANUFACTURER.COMPANY_NAME AS BRAND, dbo_COMPANY.COMPANY_NAME,
dbo_AGENTS.SHORT_DESC AS AGENT, dbo_STOCK_SUB_TYPE.SHORT_DESC AS [STOCK TYPE], Replace(Replace(dbo_COMPANY.CURRENCY_ID,'110','EURO'),'1','GBP') AS [CURRENCY], Sum(([Unit_Net]*[QTY_SOLD])-[DELIVERY_TOTAL]) AS [NET TOTAL], dbo_SORDER.DATE_CREATED, dbo_SORDER.SORDER_CODE
FROM
(((((dbo_COMPANY INNER JOIN dbo_SORDER ON dbo_COMPANY.COMPANY_ID = dbo_SORDER.COMPANY_ID) INNER JOIN dbo_SORDER_ITEM ON dbo_SORDER.SORDER_ID = dbo_SORDER_ITEM.SORDER_ID) INNER JOIN dbo_STOCK ON dbo_SORDER_ITEM.STOCK_ID = dbo_STOCK.STOCK_ID) INNER JOIN dbo_STOCK_SUB_TYPE ON dbo_STOCK.SSTYPE_ID = dbo_STOCK_SUB_TYPE.SSTYPE_ID) INNER JOIN dbo_AGENTS ON dbo_COMPANY.AGENT_ID = dbo_AGENTS.AGENT_ID) INNER JOIN dbo_MANUFACTURER ON dbo_STOCK.MANUF_ID = dbo_MANUFACTURER.MANUF_ID
GROUP BY
dbo_MANUFACTURER.COMPANY_NAME, dbo_COMPANY.COMPANY_NAME, dbo_AGENTS.SHORT_DESC, dbo_STOCK_SUB_TYPE.SHORT_DESC, dbo_SORDER.DATE_CREATED, dbo_SORDER.SORDER_CODE, dbo_COMPANY.CURRENCY_ID, dbo_STOCK.MANUF_ID
HAVING
(((dbo_STOCK_SUB_TYPE.SHORT_DESC)<>"EMBROIDERY") AND ((Year([DATE_CREATED])*12+DatePart(m,[DATE_CREATED]))=Year(Date())*12+DatePart(m,Date())-1))
ORDER BY dbo_COMPANY.COMPANY_NAME, dbo_SORDER.DATE_CREATED;
答案 0 :(得分:2)
只需尝试使用以下查询
SELECT DATEPART('m', GETDATE())
返回
为datepart指定的参数1无效。
MSAccess DatePart
,需要在设置周围加双引号。因此,尝试使用双引号而不是单引号将解决您的问题。
所以你的工作查询将是:
SELECT dbo_MANUFACTURER.COMPANY_NAME AS BRAND,
dbo_COMPANY.COMPANY_NAME,
dbo_AGENTS.SHORT_DESC AS AGENT,
dbo_STOCK_SUB_TYPE.SHORT_DESC AS [STOCK TYPE],
Replace(Replace(dbo_COMPANY.CURRENCY_ID,'110','EURO'),'1','GBP') AS [CURRENCY],
Sum(([Unit_Net]*[QTY_SOLD])-[DELIVERY_TOTAL]) AS [NET TOTAL],
dbo_SORDER.DATE_CREATED,
dbo_SORDER.SORDER_CODE
FROM (((((
dbo_COMPANY
INNER JOIN dbo_SORDER ON dbo_COMPANY.COMPANY_ID = dbo_SORDER.COMPANY_ID)
INNER JOIN dbo_SORDER_ITEM ON dbo_SORDER.SORDER_ID = dbo_SORDER_ITEM.SORDER_ID)
INNER JOIN dbo_STOCK ON dbo_SORDER_ITEM.STOCK_ID = dbo_STOCK.STOCK_ID)
INNER JOIN dbo_STOCK_SUB_TYPE ON dbo_STOCK.SSTYPE_ID = dbo_STOCK_SUB_TYPE.SSTYPE_ID)
INNER JOIN dbo_AGENTS ON dbo_COMPANY.AGENT_ID = dbo_AGENTS.AGENT_ID)
INNER JOIN dbo_MANUFACTURER ON dbo_STOCK.MANUF_ID = dbo_MANUFACTURER.MANUF_ID
GROUP BY dbo_MANUFACTURER.COMPANY_NAME,
dbo_COMPANY.COMPANY_NAME,
dbo_AGENTS.SHORT_DESC,
dbo_STOCK_SUB_TYPE.SHORT_DESC,
dbo_SORDER.DATE_CREATED,
dbo_SORDER.SORDER_CODE,
dbo_COMPANY.CURRENCY_ID,
dbo_STOCK.MANUF_ID
HAVING (((dbo_STOCK_SUB_TYPE.SHORT_DESC) <> 'EMBROIDERY')
And ((Year([DATE_CREATED]) * 12
+ DatePart("m", [DATE_CREATED])) = Year(Date) * 12
+ DatePart("m", Date) - 1))
ORDER BY dbo_COMPANY.COMPANY_NAME,
dbo_SORDER.DATE_CREATED;
答案 1 :(得分:2)
从根本上说,您使用相同的命名函数DatePart
但使用不同参数的SQL方言令人困惑。根据Excel应用程序的体系结构层,您需要根据使用的SQL方言调整DatePart
函数:ACE / Jet SQL或SQL Server TSQL。
Excel - &gt;访问 - &gt; SQL Server (链接表)
如果Excel连接到Access数据库且此Access数据库使用 SQL Server链接表(Access对象),那么您必须遵守ACE / Jet SQL 需要第一个论点的方言 DatePart 是一个双字或单字括起来的字符串文字 引用@Arulkumar在回答中显示:
DatePart('m', [DATE_CREATED])
Excel - &gt; SQL Server (ADO)
如果Excel通过VBA中的ADO连接直接连接到SQL Server 然后你必须坚持SQL Server TSQL方言,它要求DatePart的第一个参数是一个命名值而不是字符串文字为@GarethD 在评论中描述:
DatePart(MONTH, [DATE_CREATED])
DatePart(MM, [DATE_CREATED])
DatePart(M, [DATE_CREATED])
Excel - &gt;访问 - &gt; SQL Server (pass-thru)
如果Excel连接到Access数据库调用Access传递查询(不是链接表),并且此传递查询(脚本化并保存在Access数据库中)通过ODBC / OLEDB连接到SQL Server,那么您必须遵守SQL Server TSQL方言如上所述。
根据表的命名,我相信您正在使用MSSQL链接表连接到Access,因为SQL Server的对象限定符维护模式以及以句点分隔的表加字段:
dbo.COMPANY.COMPANY_NAME
当链接到Access时,不允许表名中有多个句点,因此默认情况下会替换为下划线:
dbo_COMPANY.COMPANY_NAME
无效对象错误表示Access数据库中不存在Access链接表,或者连接的SQL Server数据库中不存在该表。