title: Run-time error: Invalid parameter 1 specified for datepart - VBA - SQL
body: <p>I'm running some code in Excel VBA which queries a database then brings the data into Excel for formatting.  </p>
  
  <p>It worked fine the last time I ran it (that old chestnut) but today I've come to run the monthly report and it is throwing up a datepart error as</p>
  
  <blockquote>
    <p>Invalid parameter 1 specified for datepart  </p>
  </blockquote>
  
  <p>Here is the code:  </p>
  
  <pre><code>    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]," &amp; _
      "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 " &amp; _
      "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 " &amp; _
      "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, " &amp; _
      "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) &lt;&gt; " &amp; "'EMBROIDERY'" &amp; ") And ((Year([DATE_CREATED]) * 12 + " &amp; _
      "DatePart(" &amp; "'m'" &amp; ", [DATE_CREATED])) = Year(Date) * 12 + DatePart(" &amp; "'m'" &amp; ", Date) - 1)) ORDER BY dbo_COMPANY.COMPANY_NAME, dbo_SORDER.DATE_CREATED;"
  </code></pre>
  
  <p>Apologies for the mass of text on long lines.<br>
  As mentioned the code throws up the error: Invalid parameter 1 specified for date part.  I've tried converting the dates as someone mentioned on a forum post but that hasn't been successful.  </p>
  
  <p>If someone has any ideas that would be amazing!</p>
  
  <p>EDIT: </p>
  
  <p>Removing the quotes around the <strong>m</strong> in datepart does not work in Access.  It pops up asking for a value for <strong>m</strong>
  The SQL query I have works fine in Access, I have added it below: </p>
  
  <pre><code>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)&lt;&gt;"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;
  </code></pre>