Solved

Passing Date to Access Query with ADO within Excel VBA

Posted on 2014-10-01
17
576 Views
Last Modified: 2014-11-14
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()
0
Comment
Question by:matt_m
  • 6
  • 5
  • 3
  • +2
17 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 40356026
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
 

Author Comment

by:matt_m
ID: 40356235
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40356366
I think you are overcomplicating this. DateSerial is the function to use:

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

/gustav
0
 

Author Comment

by:matt_m
ID: 40356473
Thanks Gustav - still the same error though "Data Type Mismatch in Criteria Expression".
0
 
LVL 11

Expert Comment

by:jkpieterse
ID: 40356477
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40356484
How are OpeningDate declared?
Are the date fields in the table of data type Date?
What does the actual SQL look like?

/gustav
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40356487
> 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
 
LVL 11

Expert Comment

by:jkpieterse
ID: 40356515
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40356528
True. Then you need a string expression for the date value.

/gustav
0
 
LVL 84
ID: 40356602
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
 
LVL 24

Expert Comment

by:chaau
ID: 40356627
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
 

Author Comment

by:matt_m
ID: 40356663
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40356673
If you rename all six parameters uniqely and expand your code above for these, and that works, then you have the answer.

/gustav
0
 
LVL 24

Expert Comment

by:chaau
ID: 40358386
Show us the query, we will probably help you modify it so it refers to each of the parameters once
0
 

Accepted Solution

by:
matt_m earned 0 total points
ID: 40431868
Hi all, after much experimentation it looks like sub-queries can not be included ion these situations.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40432207
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
 

Author Closing Comment

by:matt_m
ID: 40442254
Solution found
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now