?
Solved

Passing Date to Access Query with ADO within Excel VBA

Posted on 2014-10-01
17
Medium Priority
?
915 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +2
17 Comments
 
LVL 25

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 51

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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

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 51

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 51

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
 
LVL 51

Expert Comment

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

/gustav
0
 
LVL 85
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 25

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 51

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 25

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 51

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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