0

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.

Matthew
  • 164
  • 1
  • 2
  • 10
  • 1
    You are using `CalQuery` either before it is created or after it is destroyed. Read of address 00000260 is clearly an offset from a null pointer. – J... Dec 21 '15 at 11:08
  • @J... but it works the first time and nothing in the code destroys it. The connection gets closed but is reopened immediately before trying to use the query. Also, it was working until now and I haven't changed the code. – Matthew Dec 21 '15 at 11:12
  • If the instruction pointer stops on the line you say (and not the exception), then it is `CalCon` that is nil rather than `CalQuery`. In any case, we can't see the important code here so there's not much more to say. Find everywhere you create those components, then find everywhere you free (and likely nil) them. Put breakpoints on them and wait for them to stop. When you find the unexpected one, check your callstack and start backtracking. – J... Dec 21 '15 at 11:18
  • Also, really, you ought to be [using parameters](http://docwiki.embarcadero.com/RADStudio/en/Using_Parameters_in_Queries) in your queries. This has nothing to do with your problem, it's just good practice. – J... Dec 21 '15 at 11:22
  • To begin with, set a breakpoint on the first `CalCon.Open();` and check that both `CalCon` and `CalQuery` are not nil; if you are lucky it may be enough to detect the cause of your problem. – kludg Dec 21 '15 at 11:35
  • @user246408 Got it, thanks. Will accept J... 's answer and look into parameters – Matthew Dec 21 '15 at 13:00
  • You might want to learn how to use FastMM Full Debug Mode. It might help you find where you went wrong. – Warren P Dec 21 '15 at 17:13

0 Answers0