Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 4th, 2004, 12:23 PM
keeko keeko is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 7 keeko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Passing date Parameters between Queries in Access

Can someone please tell me how I amend this code to accept date parameters?

Dim strSQL As String
Dim stDate As Date

DoCmd.RunSQL strSQL
strSQL = "SELECT Interface.RecType, Count(Interface.RecType) AS CountOfRecType," & _
"Sum(Interface.Amount) AS SumOfAmount, Interface.CreatedDate INTO test2 FROM Interface " & _
"GROUP BY Interface.RecType, Interface.CreatedDate, Interface.CreatedDate HAVING ((Interface.CreatedDate)= '#' &stDate& '#');"
DoCmd.RunSQL strSQL

I get Datatype mismatch when I try to run. I've a feeling my & # and variable definition combinations are wrong!

this works fine:

strSQL = "SELECT Interface.RecType, Count(Interface.RecType) AS CountOfRecType," & _
"Sum(Interface.Amount) AS SumOfAmount, Interface.CreatedDate INTO test FROM Interface " & _
"GROUP BY Interface.RecType, Interface.CreatedDate, Interface.CreatedDate HAVING ((Interface.CreatedDate)=#07/29/2004#);"

but I am trying to parameterise query so I can run multiple queries for the same date.

Thanks for your help.

Reply With Quote
  #2  
Old August 4th, 2004, 06:26 PM
ineuw ineuw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 82 ineuw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 30 m 28 sec
Reputation Power: 5
Date is a Variant type, so you should implement the following:

Dim strSQL As String, varDate As Variant

varDate = Me!Date (... or whatever)

strSQL = "SELECT Interface.RecType, Count(Interface.RecType) AS CountOfRecType," & _
"Sum(Interface.Amount) AS SumOfAmount, Interface.CreatedDate INTO test FROM Interface " & _
"GROUP BY Interface.RecType, Interface.CreatedDate, Interface.CreatedDate HAVING (((Interface.CreatedDate)=#" & varDate & "#));"

Note the triple/double bracketing and the positions of the double quotes. If you want to play it safe, avoid using single quotes for cross platform compatibility and where a single quote in a data string will cause an error as in
'O'Malley'.

Also, it seems that you are using "Interface.CreatedDate" twice in your grouping.

Reply With Quote
  #3  
Old August 5th, 2004, 10:00 AM
keeko keeko is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 7 keeko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you

Thank you - that works great.

Does anyone know how I would pass such a variable into a query that isn't defined within the VBA code itself?

I have tried to edit the SQL of a query to accept the Date variant as detailed above and call the query from the VBA but the query falls over.

ie:

This (myQuery):

SELECT Interface.RecType, Count(Interface.RecType) AS CountOfRecType, Sum(Interface.Amount) AS SumOfAmount, Interface.CreatedDate
FROM Interface
GROUP BY Interface.RecType, Interface.CreatedDate, Interface.CreatedDate
HAVING (((Interface.CreatedDate)=#" & varDate & "#));

falls over if I call the query by means of:

stDocName = "myQuery"
DoCmd.OpenQuery stDocName, acNormal, acEdit

because it doesn't like the syntax of the HAVING clause. Can anyone tell me how to achieve this?

Thank you

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > Passing date Parameters between Queries in Access


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT