M
M
Maxim2018-06-13 18:47:55
Delphi
Maxim, 2018-06-13 18:47:55

After clicking the button, an access violation crashes, what should I do?

When adding a contract (SpeedButton3Click), a form should open with editing the contract data, in my case, Acces Violation crashes.
5b213c01c2a14844916499.jpeg
The debugger points to End in the first procedure. Help me to understand. thanks!)
Code:

procedure TF_MAIN.SpeedButton3Click(Sender: TObject);
Var TextSQL, MaxId: String;
    Year1, MaxIDPrev: integer;
    KeyResult: Boolean;
    D1, D2: TDateTime;

begin
 //KeyResult:=False;
 InsertKey:=True;
 RB_ALL.SetFocus;

 FlagDoc:='Добавить';

 Year1:=StrToInt(StaticText1.Caption);

 MaxIDPrev:=F_DM.Q_PRIKAZ.FieldByName('PRIKAZ_ID').AsInteger;
 MaxId:=IntToStr(GetMaxFieldValueADO(F_DM.ADOConnection, 'RDP_PRIKAZ', 'PRIKAZ_ID')+1);
 //Определяем новый номер договора
 //определяем дату создания договора
 If YearOf(now-2)=Year1 Then D1:=Now
                        Else D1:=EncodeDate(StrToInt(StaticText1.Caption),12,31);

 D2:=D1+10;

 If not DirectoryExists(Path_To_Files+'DocUchet\'+MaxID) then
   MkDir(pchar(Path_To_Files+'DocUchet\'+MaxID));

 TextSQL:='insert into RDP_PRIKAZ '+
            '(PRIKAZ_ID, DP_ID, PRZ_DATACREAT, PRZ_TEMA, PRZ_DATAPOD,'+
            ' PRZ_DATAVIPDOC, PRZ_DATAFAKT, PRZ_STATUSDOC, PRZ_DIRECTOR, '+
            ' TYPEDOC_ID, PRZ_NOMERDOC, PRZ_IniPodgotovki)'+
            'VALUES ('''+MaxID+''', '+IntToStr(USER_ID)+','+IntToStr(trunc(D1)-2)+
            ', null, null,'+IntToStr(trunc(D2)-2)+', null, 0, null, 1, null, null)';
 RunSQL(TextSQL, F_DM.Q_PRIKAZ);

 With TADOCommand.create(Nil) do
     Begin
       Connection:=F_DM.ADOConnection;
       CommandText:='delete from RDP_ISPOLPRIKAZ where PRIKAZ_ID='+MaxID;
       Execute;
       Free;
     End;


  KeyResult:=F_DM.Q_PRIKAZ.Locate('PRIKAZ_ID', MaxId, []);
  if not KeyResult then
    begin
      BitBtn2Click(BitBtn2);
      KeyResult:=F_DM.Q_PRIKAZ.Locate('PRIKAZ_ID', MaxId, []);
    end;
 //Открываем редактирование нового документа
If KeyResult Then SpeedButton5Click(Sender);
end;

procedure TF_MAIN.SpeedButton5Click(Sender: TObject);
Var
  Id: String;
begin
  If F_DM.Q_PRIKAZ.IsEmpty then
    exit;
  //редактируем документ
  If not F_DM.Q_Prikaz.IsEmpty Then
    Begin
      Try
        Application.CreateForm(TF_FormUchetDoc, F_FormUchetDoc);

        Id:=F_DM.Q_Prikaz.FieldByName('Prikaz_ID').AsString;

        if F_DM.Q_PRIKAZ.FieldByName('PRZ_DATAVIPDOC').AsString='' then
          F_FormUchetDoc.BitBtn3.Enabled:=False
        else F_FormUchetDoc.BitBtn3.Enabled:=True;

        if not DirectoryExists(Path_To_Files+'DocUchet\'+Id) then
          MkDir(pchar(Path_To_Files+'DocUchet\'+Id));
        F_FormUchetDoc.ShellListView1.Root:=Path_To_Files+'DocUchet\'+Id;

          If F_FormUchetDoc.ShowModal<>mrOk Then
            Begin
              If InsertKey Then
                Begin
                  RunSimpleSQL(F_DM.ADOConnection, 'delete from RDP_ISPOLPRIKAZ where prikaz_id='+ID);
                  RunSQL('delete from RDP_PRIKAZ where prikaz_id='+ID, F_DM.Q_PRIKAZ);
                  F_DM.Q_PRIKAZ.Locate('prikaz_id', MaxIDPrev, []);
                End;
            End;

      Finally
        F_FormUchetDoc.Free;
      End;
    End;
      InsertKey:=False;
end;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kalapanga, 2018-06-13
@kalapanga

Go through the debugger step by step, you will see what exactly crashes.
Autocreation of TF_FormUchetDoc form removed?
Application.CreateForm(TF_FormUchetDoc, F_FormUchetDoc); must be before try.
Not relevant, but why is F_DM.Q_Prikaz.IsEmpty checked twice in a row?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question