Passing Date to Access Query with ADO within Excel VBA

hi Experts,  

Something is really frustrating me!  I have a query with 3 named parameters in Access that I wish to execute from excel VBA.  The three parameters are two dates and a string.   Now when I run similar code (on a different query in Access) with just a string parameter it all works great however when I have dates as parameters things go pear shaped with the error "Data Type Mismatch in Criteria Expression".  The relevant bits of code arte below:

With objcommand
.ActiveConnection = DBCONT ' Reference to a Connection object.
.CommandType = adCmdStoredProc
.CommandText = "q_PriceCheck"
End With
objcommand.Parameters.Refresh
 
 
OpeningDate = CDate(WorksheetFunction.Text(("31/08/14"), "mm/dd/yyyy"))
ClosingDate = CDate(WorksheetFunction.Text(("30/09/14"), "mm/dd/yyyy"))

Set prm = objcommand.CreateParameter("@ClosingDate", adDate, adParamInput)
    objcommand.Parameters.Append prm
    objcommand.Parameters("@ClosingDate").Value = ClosingDate


Set prm = objcommand.CreateParameter("@OpeningDate", adDate, adParamInput)

    objcommand.Parameters.Append prm
    objcommand.Parameters("@OpeningDate").Value = OpeningDate


Set prm = objcommand.CreateParameter("@PortfolioVal", adVarChar, adParamInput, 50)
    objcommand.Parameters.Append prm
    objcommand.Parameters("@PortfolioVal").Value = "Matt1"


Set rst = objcommand.Execute()
matt_mAsked:
Who is Participating?
 
matt_mAuthor Commented:
Hi all, after much experimentation it looks like sub-queries can not be included ion these situations.
0
 
chaauCommented:
The problem is with the format specifier here:
OpeningDate = CDate(WorksheetFunction.Text(("31/08/14"), "mm/dd/yyyy"))
ClosingDate = CDate(WorksheetFunction.Text(("30/09/14"), "mm/dd/yyyy"))

Open in new window

You need to use dd/mm/yyyy because this is what your dates are:
OpeningDate = CDate(WorksheetFunction.Text(("31/08/2014"), "dd/mm/yyyy"))
ClosingDate = CDate(WorksheetFunction.Text(("30/09/2014"), "dd/mm/yyyy"))

Open in new window

And use 4 digit years
0
 
matt_mAuthor Commented:
Thanks for the reply - I changed the code as follows but still got the same error.  I'm wondering if it has anything to do with the the Set prm ? Set prm = objcommand.CreateParameter("@ClosingDate", adDate, adParamInput)


My changed code is:


Set objcommand = New ADODB.Command
With objcommand
.ActiveConnection = DBCONT ' Reference to a Connection object.
.CommandType = adCmdStoredProc
.CommandText = "q_PriceCheck"
End With
objcommand.Parameters.Refresh
 
 
OpeningDate = CDate(WorksheetFunction.Text(("31/08/2014"), "dd/mm/yyyy"))
ClosingDate = CDate(WorksheetFunction.Text(("30/09/2014"), "dd/mm/yyyy"))

Set prm = objcommand.CreateParameter("@ClosingDate", adDate, adParamInput)
    objcommand.Parameters.Append prm
    objcommand.Parameters("@ClosingDate").Value = ClosingDate


Set prm = objcommand.CreateParameter("@OpeningDate", adDate, adParamInput)

    objcommand.Parameters.Append prm
    objcommand.Parameters("@OpeningDate").Value = OpeningDate


Set prm = objcommand.CreateParameter("@PortfolioVal", adVarChar, adParamInput, 50)
    objcommand.Parameters.Append prm
    objcommand.Parameters("@PortfolioVal").Value = "Matt1"


Set rst = objcommand.Execute()
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Gustav BrockCIOCommented:
I think you are overcomplicating this. DateSerial is the function to use:

    OpeningDate = DateSerial(2014, 08, 31)
    ClosingDate = DateSerial(2014, 9, 30)

/gustav
0
 
matt_mAuthor Commented:
Thanks Gustav - still the same error though "Data Type Mismatch in Criteria Expression".
0
 
jkpieterseCommented:
I never worked with parameter queries before, so forgive my ignorance, but:

What if you change the type of the date parameters to string and format them like so:
"#yyyy-mm-dd#"

So Aug 31, 2014 becomes:

"#2014-08-31#"
0
 
Gustav BrockCIOCommented:
How are OpeningDate declared?
Are the date fields in the table of data type Date?
What does the actual SQL look like?

/gustav
0
 
Gustav BrockCIOCommented:
> What if you change the type of the date parameters to string and
> format them like so: "#yyyy-mm-dd#"

Then you will add further confusion. Dates should always be handled like dates. No exception.

/gustav
0
 
jkpieterseCommented:
I did mention I have no experience using parameter queries :-) I tens to build the entire SQL statement as a string variable and pass that to ADO. In such a case you use the #yyyy-mm-dd# format in the WHERE clause.
Just thought it was worth a shot.
0
 
Gustav BrockCIOCommented:
True. Then you need a string expression for the date value.

/gustav
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You could always just bypass the Query entirely, and use straight SQL:

Dim sql As String
sql = "SELECT * FROM SomeTable WHERE StartDate=#2014-01-01#"
rst.Open sql, connection
0
 
chaauCommented:
I think you need three separate prm variables for the parameters, like prm1, prm2, prm3. Also, I do not think the @ sign is necessary. In Access the parameters are named in square brackets, like [OpeningDate]
0
 
matt_mAuthor Commented:
thanks for your advice guys.  As an update:

- I tried prm1, prm2 prm3 - same error message
- I created a different query in Access with one date variable and similar code in excel vba to above.  Works fine - I can have the date variable in quite a few different formats and all good.
- this led me back to the original problem query.  It has 3 sub queries - each with two variables, for a total of 6.  However the same named variable is common twice so there are only really 3 named variables in the final query (that uses the three subqueries) ie when run in Access you only need to enter the three variables for it to run.

Hope this last bit of info helps.  There are some workarounds I can probably implement but it would be good to know whey this isn't working!
-
0
 
Gustav BrockCIOCommented:
If you rename all six parameters uniqely and expand your code above for these, and that works, then you have the answer.

/gustav
0
 
chaauCommented:
Show us the query, we will probably help you modify it so it refers to each of the parameters once
0
 
Gustav BrockCIOCommented:
Well, we don't know what you have been doing, but it doesn't correlate with your initial remark:

Now when I run similar code (on a different query in Access) with just a string parameter it all works great however when I have dates as parameters things go pear shaped with the error "Data Type Mismatch in Criteria Expression".  

/gustav
0
 
matt_mAuthor Commented:
Solution found
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.