0

I am trying to run the following query:

SELECT Project.* FROM Project WHERE (((Projects.[End Date (PPL)])>=DateAdd('m',-6,Date())));

It runs fine in Access (2007) however when i move it over to VBA:

strQry = "SELECT Project.* FROM Project WHERE (((Projects.[End Date (PPL)])>=DateAdd('m',-6,Date())));"

I get an error message saying 'No value given for one or more required parameters'

I can get it running with the following code but it doesn't provide the correct information:

strQry = "SELECT Project.* FROM Project WHERE Project.[End Date (PPL)] >= DateAdd('m',-6,Date());"

Any help would be great.

8
  • 2
    Is the table called Project or Projects? Commented Oct 6, 2015 at 12:50
  • are you running this in Excel? Does the DateAdd function work the same in Excel as in Access? When you say that it doesn't provide the correct information, what is incorrect about the information? That will give you a clue as to what needs to be adjusted. Commented Oct 6, 2015 at 13:12
  • @ScottHoltzman, DateAdd() is a function in Access SQL and as long Excel connects to Access via ODBC, the query can use any Access SQL function/syntax irregardless of Excel, just as if Access connects to SQL Server via ODBC, query can use any syntax in MSSQL outside of Access since processing occurs in external database engine and only data is sent back to client (here being Excel). Commented Oct 6, 2015 at 13:36
  • 1
    Please provide more of your code such as ODBC connection between Excel/Access, available tables and schema: Project and/or Projects. Commented Oct 6, 2015 at 13:38
  • 1
    @Parfait I don't think that's right. I can't use the Nz() function via ODBC or OLEDB. Only JET* functions are available, not Access specific ones. Commented Oct 6, 2015 at 14:29

1 Answer 1

3

The definitive answer to your question is here, in StackOverflow:

VBA function in Excel ADODB query

The short answer is:

You're using a dialect of SQL called Jet-SQL, and most people call it from Microsoft Access, a database application which makes VBA functions available to the SQL engine.

If you run Jet SQL from any other platform - like an ODBC or ADODB wrapper -the VBA functions aren't available: all you have is the Jet-SQL native functions.

There's a list of the native Jet-SQL functions here: it's almost complete, and you can see that it's rather short:

MS Access: Functions - Listed by Category

...And it doesn't include NZ()

It's somewhat confusing that these functions look like the familiar VBA - and annoying that the web page listing them is labelled 'MS-Access' rather than Jet SQL.

There is an interesting item in that list: the inline 'If' function, IIF(), and you can use it as a substitute for NZ:


SELECT
    IIF(
        table1.[RIC] IS NOT NULL, 
        table1.[RIC],
        table2.[ISIN] + ', CODE:ISIN'
        )                               AS [RetrievalCode],
    table3.[Value], 
    table3.[DateStamp] 
FROM
    * The rest of your Query  *

Hope that helps: I suspect that it won't, because you may be calling a named query (or SQL with a named subquery in it), and all the non-native function calls in those queries are invalid unless the whole thing is called from within MS-Access.

You can, of course, make a 'schema' query and extract the SQL, parse it out, and replace all the VBA with native Jet-SQL functions. But the syntax for Jet-SQL gets arcane when you're outside it's intended environment - the Query-By-Example visual query builder inside Microsoft Access - so that suggestion may be more time and trouble than doing it some other way.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, BUT it was project not projectS. Funny.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.