I have a programme in Delphi 2005 which uses a TADOQuery
component, CalQuery
, in several places. The connection, CalCon
, connects to an Access *.mdb file (created in Access 2003).
It has been working fine until today when it suddenly stopped working. I get an Access Violation whenever the main unit tries to do anything with CalQuery
. An example of the code is below, but this happens throughout the same form. It does not seem to be happening with a copy of CalQuery
and CalCon
which connects to the same database in other units (I can't see anything that I've done differently between this unit and the others).
if (PatientGrid.RowCount = 2) and (PatientGrid.ColCount = 1) then
begin
if DayGrid.Cells[7, DayGrid.Row] = 'New' then
begin
if DayGrid.Cells[5, DayGrid.Row] = '' then
begin
CalCon.Open();
CalQuery.SQL.Clear;
CalQuery.SQL.Text := 'SELECT ClinicID FROM Clinic WHERE RoomNo = '+DayGrid.Cells[0, DayGrid.Row]+' '+
'AND [Date] = '+OldDateForSQL(DayViewDP.Date);
CalQuery.Open;
CalQuery.First;
ClinicID := CalQuery.FieldByName('ClinicID').Value;
CalQuery.Close;
CalQuery.SQL.Clear;
CalQuery.SQL.Text := 'SELECT SlotID FROM Slot WHERE ClinicID = '+IntToStr(ClinicID)+' '+
'AND AppTime = CStr('+QuotedStr(DayGrid.Cells[8, DayGrid.Row])+')';
CalQuery.Open;
CalQuery.First;
SlotID := CalQuery.FieldByName('SlotID').Value;
CalQuery.Close;
CalQuery.SQL.Clear;
CalQuery.SQL.Text := 'INSERT INTO Appointment (SlotID, ClinID, HospNo, Name) '+
'VALUES ('+IntToStr(SlotID)+', '+IntToStr(ClinicID)+', '+QuotedStr('XXXXXX')+
', '+QuotedStr(PatientGrid.Cells[0,1])+')';
CalQuery.ExecSQL;
CalCon.Close;
RefreshDayGrid(Sender);
end else ShowMessage('A patient is already booked into that slot.');
end else ShowMessage('You can only book new slots manually. Please book follow-ups from PiMS.');
end;
(All the Grids are StringGrids, not DBGrids.)
The Access Violation occurs at CalQuery.SQL.Clear;
. If I comment this line out, it crashes at the next. As I said, the code has been working until now and everything seems to be declared properly. The exact error is:
Access violation at address 004A91D4 in module 'PainCal.exe'. Read of address 00000260.
I had been working on the database to which CalCon
connects but only the tables (I had to clear all the data and wanted to the autonum field to reset, so I deleted and recreated a couple of tables). I tried rebuilding the connection string in CalCon
but that didn't help. The database file seems fine.
This isn't something I've come across before, nor can I see any similar questions elsewhere, so I'm not sure what to check next.