BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Use of Parameter problem

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Bob Dalton
Guest





PostPosted: Sat Oct 16, 2004 7:05 pm    Post subject: Use of Parameter problem Reply with quote



As Brian from Team B suggested I used a parameter in my SQL syntax statement
to make the SQL statement acceptable to both MS SQL Server and MS Access Jet
when dealing with dates. This worked find for the first iteration of the
statement but generated an EOleException "Data Type mismatch in crieria
expression" error if the date was changed on a second try. Probably
something obvious I am missing but not having used parameters before it is
new to me, and not terribly well documented. Here is the code I am using:

procedure TPurgeForm1.Button2Click(Sender: TObject);
begin
try
FiltCrit := '';
if MonthCalendar2.Date < MonthCalendar1.Date then MonthCalendar2.Date :=
MonthCalendar1.Date;

ScheduleDB.Parameters.ParambyName('dateParam1').value :=
MonthCalendar1.Date;
ScheduleDB.Parameters.ParambyName('dateParam2').value :=
MonthCalendar2.Date;
FiltCrit := 'select * from Schedule where SCH_STARTTIME >= :dateParam1 '
+
' AND SCH_ENDTIME <= :dateParam2 ';
FiltCrit := FiltCrit + ' order by SCH_STARTTIME';
ScheduleDB.CommandText := FiltCrit;
ScheduleDB.Active := True;
ScheduleDB.Requery;
ScheduleDB.First;

OrganizationDB.Active := True;
FormsDB.Active := True;
if FormsDB.Locate('FRM_Number', 31, []) = False then
begin
FormsDB.Insert;
FormsDBFRM_Number.AsInteger := 31;
FormsDBFRM_HeaderInfo.AsString := 'Equipment Schedule Report';
FormsDBFRM_ModuleName.AsString := 'Dispatch';
FormsDB.Post;
FormsDB.Refresh;
end;
if FormsDB.Locate('FRM_Number', 31, []) then
begin
ppReport1.Reset;
ppReport1.Print;
end;
finally
Application.ProcessMessages;
FormsDB.Active := False;
ScheduleDB.Active := False;
OrganizationDB.Active := False;
end;

Note: I made sure to include the parameters using the names above in the
TADODatSet parameters property and set the values accepted to DateTime.

Regards;

Bob Dalton


Back to top
Henry
Guest





PostPosted: Sat Oct 16, 2004 7:44 pm    Post subject: Re: Use of Parameter problem Reply with quote



"Bob Dalton" <bob.dalton (AT) removeme_digitallogistics (DOT) com> skrev i en
meddelelse news:41717105$1 (AT) newsgroups (DOT) borland.com...
Quote:
As Brian from Team B suggested I used a parameter in my SQL syntax
statement
to make the SQL statement acceptable to both MS SQL Server and MS Access
Jet
when dealing with dates. This worked find for the first iteration of the
statement but generated an EOleException "Data Type mismatch in crieria
expression" error if the date was changed on a second try. Probably
something obvious I am missing but not having used parameters before it is
new to me, and not terribly well documented. Here is the code I am using:

On which line do you get the exception?


I would do it the other way around

FiltCrit := '';
FiltCrit := 'select * from Schedule where SCH_STARTTIME >= :dateParam1 ' +
' AND SCH_ENDTIME <= :dateParam2 ';
FiltCrit := FiltCrit + ' order by SCH_STARTTIME';
ScheduleDB.CommandText := FiltCrit;

if MonthCalendar2.Date < MonthCalendar1.Date then MonthCalendar2.Date :=
MonthCalendar1.Date;
ScheduleDB.Parameters.ParambyName('dateParam1').value :=
MonthCalendar1.Date;
ScheduleDB.Parameters.ParambyName('dateParam2').value :=
MonthCalendar2.Date;
ScheduleDB.Active := True;



ScheduleDB.Requery;
< Why, what is this?
ScheduleDB.First;
< Why?

I would also use BETWEEN for convinience ;o)


regards
Henry




Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Sun Oct 17, 2004 3:05 pm    Post subject: Re: Use of Parameter problem Reply with quote



Bob Dalton wrote:
Quote:

ScheduleDB.Parameters.ParambyName('dateParam1').value :=
MonthCalendar1.Date;
ScheduleDB.Parameters.ParambyName('dateParam2').value :=
MonthCalendar2.Date;
FiltCrit := 'select * from Schedule where SCH_STARTTIME >=
:dateParam1 ' +
' AND SCH_ENDTIME <= :dateParam2 ';
FiltCrit := FiltCrit + ' order by SCH_STARTTIME';
ScheduleDB.CommandText := FiltCrit;

You need to assign the SQL statement *first* before assigning parameter
values. If the SQL has already been assigned (e.g. at design time) then you
do not need to keep reassigning it here, just set the parameters.

Also, avoid the .Value property like the plague, *especially* when dealing
with dates. Use AsDateTime:

ScheduleDB.Parameters.ParambyName('dateParam1').AsDateTime :=
MonthCalendar1.Date;

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
Those who disdain wealth as a worthy goal for an individual or a
society seem not to realize that wealth is the only thing that can
prevent poverty. - Thomas Sowell



Back to top
Bob Dalton
Guest





PostPosted: Sun Oct 17, 2004 6:18 pm    Post subject: Re: Use of Parameter problem Reply with quote

Thanks to both of you for the tips. BTW the requery is because the
TADODataSet component is used in two different ways on the same for. I only
included the code for the first routine.

One of the problems I am finding with the ADO components when used with SQL
is the general lack of documentation for them in Delphi 7. Yes there is some
spotty and generall brief information, but not all that helpful.

I purchased the "Advanced Delphi Developers Guide to ADO (for Delphi 5)
which I found useful in explaining the various ADO properties, methods, and
events. While helpful there was little code showing ADO in actual use or
demonstrating the various things in use. A good examples is the parameters
property of the TADODataSet. The book and Delphi documentation explain it in
a roundabout fashion but do not actually *show* you the proper way to use
them and how or the sequence of events needed to make them work correctly,
nor the pitfalls to avoid. This leaves most of us, including yours truly,
using trial and error most of the time to come up with how best to use these
properties, methods and events.

I do like ADO but just wish it was better documented....

Regards;

Bob Dalton

"Wayne Niddery [TeamB]" <wniddery (AT) chaffaci (DOT) on.ca> wrote

Quote:
Bob Dalton wrote:

ScheduleDB.Parameters.ParambyName('dateParam1').value :=
MonthCalendar1.Date;
ScheduleDB.Parameters.ParambyName('dateParam2').value :=
MonthCalendar2.Date;
FiltCrit := 'select * from Schedule where SCH_STARTTIME >=
:dateParam1 ' +
' AND SCH_ENDTIME <= :dateParam2 ';
FiltCrit := FiltCrit + ' order by SCH_STARTTIME';
ScheduleDB.CommandText := FiltCrit;

You need to assign the SQL statement *first* before assigning parameter
values. If the SQL has already been assigned (e.g. at design time) then
you
do not need to keep reassigning it here, just set the parameters.

Also, avoid the .Value property like the plague, *especially* when dealing
with dates. Use AsDateTime:

ScheduleDB.Parameters.ParambyName('dateParam1').AsDateTime :=
MonthCalendar1.Date;

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
Those who disdain wealth as a worthy goal for an individual or a
society seem not to realize that wealth is the only thing that can
prevent poverty. - Thomas Sowell





Back to top
Bob Dalton
Guest





PostPosted: Sun Oct 17, 2004 6:31 pm    Post subject: Re: Use of Parameter problem Reply with quote

Tried as you suggested
ScheduleDB.Parameters.ParambyName('dateParam1').AsDateTime := Date and got
an Error: "Undeclared identifier: AsDateTime".

BTW The TParameter[0].DataType is set to ftDateTime, the TParameter[0].Name
is dateParam1 and everything else is left at default.

Something I am doing wrong or missing?

BTW fuill code is now:

procedure TPurgeForm1.Button2Click(Sender: TObject);
begin
try
FiltCrit := '';
if MonthCalendar2.Date < MonthCalendar1.Date then MonthCalendar2.Date :=
MonthCalendar1.Date;

FiltCrit := 'select * from Schedule where SCH_STARTTIME >= :dateParam1 '
+
' AND SCH_ENDTIME <= :dateParam2 ';
FiltCrit := FiltCrit + ' order by SCH_STARTTIME';
ScheduleDB.CommandText := FiltCrit;
ScheduleDB.Parameters.ParambyName('dateParam1').AsDateTime :=
MonthCalendar1.Date;
ScheduleDB.Parameters.ParambyName('dateParam2').AsDateTime :=
MonthCalendar2.Date;
ScheduleDB.Active := True;
ScheduleDB.Requery;
ScheduleDB.First;

OrganizationDB.Active := True;
FormsDB.Active := True;
if FormsDB.Locate('FRM_Number', 31, []) = False then
begin
FormsDB.Insert;
FormsDBFRM_Number.AsInteger := 31;
FormsDBFRM_HeaderInfo.AsString := 'Equipment Schedule Report';
FormsDBFRM_ModuleName.AsString := 'Dispatch';
FormsDB.Post;
FormsDB.Refresh;
end;
if FormsDB.Locate('FRM_Number', 31, []) then
begin
ppReport1.Reset;
ppReport1.Print;
end;
finally
Application.ProcessMessages;
FormsDB.Active := False;
ScheduleDB.Active := False;
OrganizationDB.Active := False;
end;
end;

Regards;

Bob Dalton

Quote:
Also, avoid the .Value property like the plague, *especially* when dealing
with dates. Use AsDateTime:

ScheduleDB.Parameters.ParambyName('dateParam1').AsDateTime :=
MonthCalendar1.Date;



Back to top
Bob Dalton
Guest





PostPosted: Sun Oct 17, 2004 7:10 pm    Post subject: Re: Use of Parameter problem Reply with quote

Found the solution. The problem was the TMonthCalendar components. They
return a DATE and not a DateTime which is what the paraemeters ftDateTime
value expects. Here is the code which will work with eitther mS SQL Serevr
OR MS Access for dates and will hopefully save someone else the "head
bashing against brick wall" problems I experienced with this issue:

procedure TPurgeForm1.Button2Click(Sender: TObject);
begin
try
FiltCrit := '';
if MonthCalendar2.Date < MonthCalendar1.Date then MonthCalendar2.Date :=
MonthCalendar1.Date;

FiltCrit := 'select * from Schedule where SCH_STARTTIME >= :dateParam1 '
+
' AND SCH_ENDTIME <= :dateParam2 ';
FiltCrit := FiltCrit + ' order by SCH_STARTTIME';
ScheduleDB.CommandText := FiltCrit;
ScheduleDB.Parameters.ParambyName('dateParam1').DataType := ftDate;
ScheduleDB.Parameters.ParambyName('dateParam2').DataType := ftDate;
ScheduleDB.Parameters.ParambyName('dateParam1').value :=
MonthCalendar1.Date;
ScheduleDB.Parameters.ParambyName('dateParam2').value :=
MonthCalendar2.Date;
ScheduleDB.Active := True;
ScheduleDB.Requery;
ScheduleDB.First;

OrganizationDB.Active := True;
FormsDB.Active := True;
if FormsDB.Locate('FRM_Number', 31, []) = False then
begin
FormsDB.Insert;
FormsDBFRM_Number.AsInteger := 31;
FormsDBFRM_HeaderInfo.AsString := 'Equipment Schedule Report';
FormsDBFRM_ModuleName.AsString := 'Dispatch';
FormsDB.Post;
FormsDB.Refresh;
end;
if FormsDB.Locate('FRM_Number', 31, []) then
begin
ppReport1.Reset;
ppReport1.Print;
end;
finally
Application.ProcessMessages;
FormsDB.Active := False;
ScheduleDB.Active := False;
OrganizationDB.Active := False;
end;
end;

Thanks to all who tried to help me! It is you all who make the forums work
for everyone else!

Regards;

Bob Dalton


Back to top
Bob Dalton
Guest





PostPosted: Sun Oct 17, 2004 7:19 pm    Post subject: Re: Use of Parameter problem Reply with quote

The first example assumes values of Date and not DateTime. Here is an
example using a DateTime value:

procedure TPurgeForm1.Button1Click(Sender: TObject);
begin
if MessageDlg('Are you sure you want to clear all scheduled items which
have an end date prior to today?',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
try
OldCursor := Screen.Cursor;
Screen.Cursor := crHourglass;
FiltCrit := '';
FiltCrit := 'select * from Schedule where SCH_ENDTIME < :dateParam1';
FiltCrit := FiltCrit + ' order by SCH_STARTTIME';
ScheduleDB.CommandText := FiltCrit;
ScheduleDB.Parameters.ParambyName('dateParam1').DataType :=
ftDateTime;
ScheduleDB.Parameters.ParambyName('dateParam1').value := Date;
ScheduleDB.Active := True;
ScheduleDB.Requery;
ScheduleDB.First;
while not ScheduleDB.eof do
begin
if ScheduleDBSCH_ENDTIME.AsDateTime < Date then
begin
ScheduleDB.Delete
end
else
begin
ScheduleDB.Next;
end;
end;
finally
Screen.Cursor := OldCursor;
ScheduleDB.Active := False;
ShowMessage('All done. You may now close this form.');
end;
end;

Regards;

Bob Dalton


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.