473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Date criteria for query in VBA

72 64KB
Hi,
have a crosstab query that works ok so far

Need a dynamic date criteria. I use a tempVar from a Form. If I write in vba

TempVars!SelectedReportDate = "05/06/2020" this is ok

TempVars!SelectedReportDate = "Between 8/05/2020 and 08/06/2020"

this does not work. when I requery the subform I get error 3420 Object invalid or no longer set and the subform gives me 1 records with all the 9 fields shown as #Name?
Tried many combinations with apostrophe but could not get it to work. the two date will come eventually form 2 textboxes on the form. Any ideas? thanks
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM CDbl(Nz(Sum([PaymentAmount]),0)) AS AmountTotal
  2. SELECT tblInvoices.DateProcessed AS RepDate, Count(tblPayments.PaymentID) AS CountOfPaymentID, Sum([AmountTotal]) AS GrandTotal
  3. FROM tblInvoices INNER JOIN (tblPaymentType INNER JOIN tblPayments ON tblPaymentType.PaymentTypeID = tblPayments.fkPaymentTypeID) ON tblInvoices.InvoiceID = tblPayments.fkInvoiceID
  4. WHERE (((tblInvoices.DateProcessed)=[TempVars]![SelectedReportDate]))
  5. GROUP BY tblInvoices.DateProcessed
  6. PIVOT tblPaymentType.PaymentType In ("Card","Cash","Other","Voucher","Discount","Refund");
Jun 8 '20 #1

✓ answered by NeoPa

Hi Neruda.

It seems you're confusing a value, on the one hand, with code on the other. When you set it to a single date then TempVars!SelectedReportDate stores the text value as a string. When you refer to that variable within you SQL it recognizes that the value can be interpreted as a date and so interprets it thus and manages to execute the SQL normally.

When you store the longer string then this is clearly not able to be interpreted as a date value. At no point does the SQL engine try to take the value of TempVars!SelectedReportDate and interpret it as part of the SQL instruction code as you seem to be trying to do. If it did it would still fail as the syntax is also wrong. If you open out the value as below you'll see it isn't valid SQL.
Expand|Select|Wrap|Line Numbers
  1. WHERE (((tblInvoices.DateProcessed)=[TempVars]![SelectedReportDate]))
becomes
Expand|Select|Wrap|Line Numbers
  1. WHERE (((tblInvoices.DateProcessed)=Between 8/05/2020 and 08/06/2020]))
Not that you should worry too much about that. It doesn't support what you're trying to do.

There are various solutions but the one most consistent with how you do things already would be to use two TempVars. You may also want to ensure you save them as Date values rather than strings too, but Jet/ACE will actually handle strings for you if you do it that way as you've already found out.

4 2683
NeoPa
32,556 Expert Mod 16PB
Hi Neruda.

It seems you're confusing a value, on the one hand, with code on the other. When you set it to a single date then TempVars!SelectedReportDate stores the text value as a string. When you refer to that variable within you SQL it recognizes that the value can be interpreted as a date and so interprets it thus and manages to execute the SQL normally.

When you store the longer string then this is clearly not able to be interpreted as a date value. At no point does the SQL engine try to take the value of TempVars!SelectedReportDate and interpret it as part of the SQL instruction code as you seem to be trying to do. If it did it would still fail as the syntax is also wrong. If you open out the value as below you'll see it isn't valid SQL.
Expand|Select|Wrap|Line Numbers
  1. WHERE (((tblInvoices.DateProcessed)=[TempVars]![SelectedReportDate]))
becomes
Expand|Select|Wrap|Line Numbers
  1. WHERE (((tblInvoices.DateProcessed)=Between 8/05/2020 and 08/06/2020]))
Not that you should worry too much about that. It doesn't support what you're trying to do.

There are various solutions but the one most consistent with how you do things already would be to use two TempVars. You may also want to ensure you save them as Date values rather than strings too, but Jet/ACE will actually handle strings for you if you do it that way as you've already found out.
Jun 8 '20 #2
Neruda
72 64KB
so I wrote

Between tempvars!SelectedReportDate And tempvars!SelectedReportDate1
in the query criteria and it works.
was wondering:if use between keyword, can a date be left blank? (guess it dos not make sense though)
Can the criteria be constructed in vba at all?
Jun 8 '20 #3
Neruda
72 64KB
ok, think I got it now, have 2 textboxes instead and do the job as expected, Thanks!
Jun 8 '20 #4
NeoPa
32,556 Expert Mod 16PB
Neruda:
Can the criteria be constructed in vba at all?
That's a very important question. Hopefully my answer will help you to understand why.

SQL, as we know, is formed of commands held in Text format. One would expect then, that one could use VBA to create/modify a text string with SQL code in to be used. The truth is that it can - sometimes.

There are many places SQL can be used, and by extension, parts of SQL can be. SQL strings can be found in various Access objects - QueryDefs being the most obvious, but also in the RecordSource properties of Forms & Reports as well as the RowSources of various Controls. Forms & reports, as well as some of the Controls, also have properties that allow filtering to be specified independently of the RecordSource etc. The Filter property of Forms & Reports can be set explicitly as well as automatically when the object is opened.

All of these allow you to specify your SQL but some only allow you to set the SQL by updating the design of the object itself. To be avoided where possible. That does leave setting filters at the point of opening and, something I haven't yet mentioned, Action Queries can also be executed as SQL passed directly as a String, as well as from within a saved QueryDef of course.

So, there are many ways of creating and using SQL that has been created or modified using VBA. Unfortunately though, this flexibility & power comes with certain restrictions.

One of the things I use a lot is having the basic part of the SQL in the Tag property of an object with place-holders for the items I know will change. When the object is opened it will take one or more values as passed and put them into the SQL string before using the result as the RecordSource.

EG. (Simplified) strClientID passed as 'X000365'. Tag contains :
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM [tblClient] WHERE ([ClientID]='%C')
The Form_Open() procedure would include :
Expand|Select|Wrap|Line Numbers
  1. With Me
  2.     .RecordSource = Replace(.Tag, "%C", strClientID)
  3. End With
I hope that gives a bit of an idea of what can be done using VBA, as well as why one can't simply use it in all scenarios.
Jun 8 '20 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: David B | last post by:
I have 2 querys with the following in the date criteria Between DateAdd("m",-6,(!!)) And (!!) Between DateAdd("m",-24,(!!)) And DateAdd("m",-6,(!!)) Problem is they both look at the same...
3
by: Vivian Dunn | last post by:
Can anyone please help with what I am sure is actually a very simple question. In the charity, that I work for, I use a database which records the support that we give to our clients. One of the...
0
by: zeusspandex | last post by:
Im creating a cross tab query which sorts via and sums the for each type of . I want to be able to add a date criteria, so the user can specify that the query processes data between two dates. ...
1
by: zeusspandex | last post by:
Hi, i have the following SQL query and would like to add date criteria to it. the field being from the table and the criteria being Between !! And !! query: SELECT , ...
3
by: shawnmiller77 | last post by:
Need Help ASAP! History: Installed new SBS 2003 server on Monday. Migrated IIS, website and current Access database over to new server. I did not develop the website or Access database. Former...
5
by: sh55555 | last post by:
I have built a query in Access using the DateSerial function to convert dates such as 20100401 to 04/01/2010. The query results work fine and the date is displayed correctly. I am now trying to...
9
by: Raza Zahur | last post by:
Hi all, I've googled and googled but I can google no more, so I am here to ask for help on an issue I am having. I have a macro that runs a series of queries. Before the macro can be run, the...
1
yosiro
by: yosiro | last post by:
I want to seperate date on query field. Eg. in the field date show 12/3/2012 so i want to seperate just year in a new field in excel we known MID LEFT RIGHT function
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.