Solved

Date query issue based on parameters

Posted on 2013-05-31
6
368 Views
Last Modified: 2013-06-01
Hi fellas,

It's been a long time, happy to be here again.

I have a little issue with retrieving info based on a date range:

procedure TReportsForm.FilterBtnClick(Sender: TObject);
begin
  MainForm.Query.Close;   Elder
  MainForm.Query.SQL.Clear;
  MainForm.Query.SQL.Text := 'SELECT sum(AdultMale), sum(AdultFemale), sum(ChildMale), sum(ChildFemale), sum(ElderMale), sum(ElderFemale), ' +
'sum(PermEx), sum(DecEx), sum(SurgEx), sum(TempFill), sum(AmalFill), sum(CompFill), sum(InstRCT), ' +
'sum(DecRCT), sum(PermRCT), sum(PerPerio), sum(ScallPerio), sum(LesionPerio), sum(OLAMS), ' +
'sum(OGAMS), sum(DresMS), sum(PARay), sum(OcclRay), sum(ExoRay), sum(FullSet), sum(PartialSet), ' +
'sum(SetRepair), sum(PMSet), sum(FixedCrown), sum(FixedBridge), sum(OHI), sum(FS), sum(FSO) ' +
'FROM Statistics WHERE TransDate BETWEEN :FromDate AND :ToDate';
  MainForm.Query.Parameters.ParamByName('FromDate').Value := DateOf(FromDateEdit.Date);
  MainForm.Query.Parameters.ParamByName('ToDate').Value := DateOf(ToDateEdit.Date); //DateUtils
  MainForm.Query.Open;

  MainForm.frxReport.ShowReport();
  Close;
end;

Open in new window



The above code works flawlessly when a basic query is written such as "Select * from"

But when it came to this complicated/boring query, the following error popped up:

No value given for one or more required parameters.

I don't know why Access is not detecting the parameters even though I checked the property ParamCheck in the used TADOQuery.

I doubled checked the columns names also and they are totally fine.

Any ideas?

Appreciated in advance,

Phil.
0
Comment
Question by:gxs
6 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 39211252
I see in the query the parameters have a : infront of them.  Do you need to use the : when you assign the value?

mlmcc
0
 
LVL 1

Author Comment

by:gxs
ID: 39211374
Thanks for your participation mlmcc.

The colon is used or called a bind variable in other words.

It gives the query the ability to use different values which is the case of selecting different dates through the datetimepicker.

Hope it is clear to you.
0
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 39211572
try to use thus notation:
...
'FROM Statistics WHERE TransDate BETWEEN :pFromDate AND :pToDate';
MainForm.Query.Parameters.ParamByName('pFromDate').Value := DateOf(FromDateEdit.Date);
  MainForm.Query.Parameters.ParamByName('pToDate').Value := DateOf(ToDateEdit.Date); //DateUtils
  MainForm.Query.Open;

Open in new window


..and on Query -> Parameters - set DataType as DateTime for each parameters.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:gxs
ID: 39211781
Sinisav, your suggestion didn't work but it doesn't mean that it is wrong. :p

I mean damn it man sometimes little things do drive you crazy. :@

The fix was the following:

procedure TReportsForm.FilterBtnClick(Sender: TObject);
begin
  MainForm.Query.Close;   Elder
  MainForm.Query.SQL.Clear;                                               //[Child-Male]
  MainForm.Query.SQL.Text := 'SELECT sum(AdultMale), sum(AdultFemale), sum([Child-Male]), sum(ChildFemale), sum(ElderMale), sum(ElderFemale), ' +
'sum(PermEx), sum(DecEx), sum(SurgEx), sum(TempFill), sum(AmalFill), sum(CompFill), sum(InstRCT), ' +
'sum(DecRCT), sum(PermRCT), sum(PerPerio), sum(ScallPerio), sum(LesionPerio), sum(OLAMS), ' +
'sum(OGAMS), sum(DresMS), sum(PARay), sum(OcclRay), sum(ExoRay), sum(FullSet), sum(PartialSet), ' +
'sum(SetRepair), sum(PMSet), sum(FixedCrown), sum(FixedBridge), sum(OHI), sum(FS), sum(FSO) ' +
'FROM Statistics WHERE TransDate BETWEEN :FromDate AND :ToDate';
  MainForm.Query.Parameters.ParamByName('FromDate').Value := DateOf(FromDateEdit.Date);
  MainForm.Query.Parameters.ParamByName('ToDate').Value := DateOf(ToDateEdit.Date); //DateUtils
  MainForm.Query.Open;

  MainForm.frxReport.ShowReport();
  Close;
end;

Open in new window


The code in my older comment is an old one. The above one has a lot of dashes. By restricting them into square brackets, the problem is solved.

It is the same thing that I discussed with you and the other guys in my other subject when I said square brackets do solve many Access compatibility issues such as queries that include Arabic or Chinese Alphabet. But to be honest, I have never imagined that Access would be fooled by a dash :O, :@.

Appreciated.
0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 39211857
To debug problems like this...put the actual SQL into the TQuery component...
instead of parameters...put the actual values in the query...
set the query to active in the Object Inspector...see if you get any errors...then replace one of the parameters...and repeat...adding the parameters until you find the offending problem...this way you can find if its a problem with the SQL or the parameters...
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39211861
I always use English non-separated words for field names like UserName, DateOfRun, ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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