Re: ADOQuery in thread hangs



I think I have localiced where the problem begins...

In my main program (not the thread) i have the following code. The
EditSchedulesForm is a maintenance form and it adds a procedure hook for all
insert events. When an insert triggers it sets some default values including
an ID which i get through another functioncall to GetNextScheduleId. This is
where it all goes wrong! As soon as this GetNextScheduleId is executed the
thread!! stops working with the error i mentioned earlier. If I in
GetNextScheduleId just put a "Result := 100; Exit;" then it works.
The really strange thing is that my main program works all the time, also
after the code below s run.

-- From maintenance form

Procedure TEditSchedulesForm.FormShow(Sender: TObject);
DBForm.ADOQuery.AfterInsert := InsertRecord;

Procedure TEditSchedulesForm.FormDestroy(Sender: TObject);
DBForm.ADOQuery.AfterInsert := Nil;

Procedure TEditSchedulesForm.InsertRecord(DataSet: TDataSet);
Begin
DBForm.ADOQuery.FieldByName('ScheduleID').AsInteger :=
DBForm.GetNextScheduleId;
DBForm.ADOQuery.FieldByName('ScheduleCreationDate').AsDateTime := Now;
DBForm.ADOQuery.FieldByName('ScheduleChangeDate').AsDateTime := Now;
End;

--- From DBForm

Function TDBForm.GetNextScheduleId: Integer;
Begin
Result := 0;
Try
ADOQuery3.Close;
ADOQuery3.SQL.Clear;
ADOQuery3.SQL.Add('Select Max(ScheduleId) As MaxId');
ADOQuery3.SQL.Add('From Schedules');
ADOQuery3.Open;
If ADOQuery3.RecordCount = 1 Then
Result := Max(1, ADOQuery3.FieldByName('MaxId').AsInteger + 1)
Else
Result := 1;
LogForm.Log(1, 'Retreiving next free Schedule ID as ' +
IntToStr(Result), 0);
Except
On E: Exception do
Begin
LogForm.Log(4, 'Failed retreiving next free schedule Id!', 0);
LogForm.LogSQL(0, ADOQuery3.SQL, 0);
LogForm.Log(0, 'Error: ' + E.Message, 0);
End;
End;
ADOQuery3.Close;
End;


"Mikael Lenfors" <mikael@xxxxxxxxxx> skrev i meddelandet
news:4563ec72$1@xxxxxxxxxxxxxxxxxxxxxxxxx
Yes I create a TADOQuery local to the thread. In the thread I don't have
any ADOConnection, I just use a connectionstring directly in the ADOQuery.

Mikael


"Brian Bushay TeamB" <BBushay@xxxxxxxxx> skrev i meddelandet
news:n897m2hao5o1ff76lq199v79aav5oqtvtu@xxxxxxxxxx
Hello!

Having a real nightmare problem at the moment. In my main program I have
a
ADOConnection and several associated ADOQuerys. They are all connected to
an
Access file.
Then I have a TThread containing it's own ADOQuery, connecting to the
same
Access file doing some selects. I can run the thread as many times as I
want
without any problem. If I then do an Insert statement in the main program
a
strange thing happenes. The next time I'm running the thread it DIES at
the
first line where I do ADOQuery.SQL.Add...

ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Add('Select * From Units Where UnitID = ' +
IntToStr(UnitRecord.UnitID));
ADOQuery.Open;

The above is sample code from the thread, if I trace it and press F8 at
the
Add line it just dies there, no exceptions what so ever.

Running Win XP Pro and Delphi Studio 2006

Please any ideas?


When you use a thread you need to create the TadoQuery as well as its
connection
in the thread. You didn't indicate if that is what you are doing.
--
Brian Bushay (TeamB)
Bbushay@xxxxxxxxx








.



Relevant Pages

  • Re: ADOQuery in thread hangs
    ... ADOConnection, I just use a connectionstring directly in the ADOQuery. ... Add line it just dies there, ...
    (borland.public.delphi.database.ado)
  • Re: ADOQuery in thread hangs
    ... I just use a connectionstring directly in the ... Having a real nightmare problem at the moment. ... Then I have a TThread containing it's own ADOQuery, ... Access file doing some selects. ...
    (borland.public.delphi.database.ado)