Access Violation spørgsmål
Jeg en procedure, som tjekker en sql db igennem for når en medarbejder stopper. Udfra hvad personen chef vælger, skal der udføres forskellige ting. Jeg har en person, som er den sidste i db'en. De ting, som skal gøres i forbindelse med at personen stopper er blevet udført, men når jeg skal afslutte min query får jeg en Access Violation fejl:Access Violation at 0x0192f91f: write of address 0x020f8947.
Hvis den person ikke er i min db får jeg ikke sådanne en fejl.
Hvad kan det være?
Bruger Delphi 2006.
Koden. Nu ikke noget med at grine. :) Jeg er sikker på at den kan optimeres.
procedure TMyThread.CheckForUserToDelete;
var Query, Query1 : TADOQuery; Table : TADOTable; UpdateMail : boolean; BooTest, str, strTest : string; Disp : IDispatch; User : IAdsUser; Container : IADsContainer; HR : HREsult;
begin
try
Query1 := TADOQuery.Create(nil);
Query1.Connection := MainForm.ADOConnection1;
Query1.SQL.Clear;
Query1.SQL.Add('SELECT * FROM STAFFLEFTCOMPANY WHERE DONE = :Done');
Query1.Parameters.ParamByName('Done').Value := false;
Query1.Open;
Updatemail := false;
MainForm.IdSMTP1.Host := 'SMTP';
MainForm.IdMessage1.From.Address := 'FromAddress';
while not Query1.eof do
begin
if Query1.FieldByName('DATEOFBOSSUPDATE').AsString = '' then
begin
case comparedate(Query1.FieldByName('DATEOFRETIRE').AsDateTime,Date) of
1 : begin
case daysbetween(Query1.FieldByName('DATEOFRETIRE').AsDateTime,DATE) of
0..6 : begin
ShowMessage(IntToStr(daysbetween(strtodate(Query1.FieldByName('DATEOFRETIRE').AsString), DATE)) + Query1.FieldByName('EMPLOYEE').AsString);
end;
7..31 : begin
case daysbetween(Date, StrToDate(Query1.FieldByName('DATEOFITUPDATE').AsString)) of
0..6 : begin
if Query1.FieldByName('Reminder').AsInteger = 0 then
begin
MainForm.IdMessage1.Subject := 'In connection with the termination of ' + Query1.FieldByName('EMPLOYEE').AsString;
UpdateMail := true;
end;
end;
7,14,21 : begin
case Query1.FieldByName('Reminder').AsInteger of
1 : begin
MainForm.IdMessage1.Subject := '1st reminder: In connection with the termination of ' + Query1.FieldByName('EMPLOYEE').AsString;
UpdateMail := true;
end;
2 : begin
MainForm.IdMessage1.Subject := '2nd reminder: In connection with the termination of ' + Query1.FieldByName('EMPLOYEE').AsString;
UpdateMail := true;
end;
3 : begin
MainForm.IdMessage1.Subject := '3rd reminder: In connection with the termination of ' + Query1.FieldByName('EMPLOYEE').AsString;
UpdateMail := true;
end;
end;
end;
end;
end;
end;
end;
-1 : begin
case daysbetween(Date, Query1.FieldByName('DateOfITUpdate').AsDateTime) of
1 : begin
if Query1.FieldByName('Reminder').AsInteger = 0 then
begin
MainForm.IdMessage1.Subject := 'In connection with the termination of ' + Query1.FieldByName('EMPLOYEE').AsString;
UpdateMail := true;
end;
end;
end;
// IdMessage1.Subject := 'Days past: ' + intToStr(daysbetween(strtodate(ADOQuery1.FieldByName('DATEOFRETIRE').AsString), DATE)) + ' ' + ADOQuery1.FieldByName('EMPLOYEE').AsString;
// ShowMessage(intToStr(daysbetween(strtodate(ADOQuery1.FieldByName('DATEOFRETIRE').AsString), DATE))+ ADOQuery1.FieldByName('EMPLOYEE').AsString);
// SendMail;
end;
end;
if UpdateMail then
begin
With MainForm.IdMessage1 do
begin
Recipients.Clear;
Body.Clear;
if MainForm.checkbox2.Checked then
Recipients.Add.Address := 'Recipient'
else
Recipients.Add.Address := Query1.FieldByName('EMAILOFBOSS').AsString;
Body.Add('');
Body.Add('In connection with the termination of ' + Query1.FieldByName('EMPLOYEE').AsString + ', please');
Body.Add('fill retirement form, by pressing the link below.');
Body.Add('');
Body.Add('http://dksodt0470/default.aspx?task=delete&id=' + Query1.FieldByName('SECURITYCODE').AsString);
Body.Add('');
Body.Add('Br');
Body.Add('ITService Desk');
Body.Add('');
end;
with MainForm.IdSMTP1 do
begin
try
Connect;
Send(MainForm.IdMessage1);
Disconnect;
except on E: Exception do
begin
if connected then try disconnect; except end;
ShowMessage(E.Message);
end;
end;
end;
if not MainForm.Checkbox2.Checked then
begin
try
Query := TADOQuery.Create(Nil);
Query.Connection := MainForm.ADOConnection1;
with Query do
begin
SQL.Clear;
SQL.Add('Update StaffLeftCompany');
SQL.Add('Set DateOfITUpdate = :DateOfITUpdate,');
SQL.Add(' reminder = reminder + 1 ');
SQL.Add('Where SecurityCode = :SecurityCode');
Parameters.ParamByName('DateOfITUpdate').Value := DateOf(now);
Parameters.ParamByName('SecurityCode').Value := Query1.FieldByName('SECURITYCODE').AsString;
Prepared := true;
ExecSQL;
end;
finally
Query.Free;
end;
UpdateMail := false;
end;
end;
end
else
begin
if CompareDate(Query1.fieldByName('DateOfRetire').AsDateTime,Date) = -1 then
begin
case daysbetween(Date,query1.FieldByName('DateOfRetire').AsDateTime) of
1..31 : begin
if CompareDate(Query1.fieldByName('DateOfOperator').AsDateTime,Date) = -1 then
begin
CoInitializeEx(0,COINIT_MULTITHREADED);
try
if SUCCEEDED(ADsGetObject('LDAP://cn=' + Query1.FieldByName('Employee').AsString + ',OU=USERS,OU=SO,OU=DK,OU=ALFALAVAL,DC=AD,DC=ALFALAVAL,DC=ORG',IADsUser, User)) then
begin
if not User.AccountDisabled then
begin
User.AccountDisabled := true;
User.Description := 'Stoppet den ' + DateTostr(Query1.FieldByName('DATEOFRETIRE').AsDateTime);
end;
User.SetInfo;
User._Release;
end;
except on E: Exception do
ShowMessage(E.Message);
end;
CoUnInitialize;
if not Query1.FieldByName('BPCSStatus').AsBoolean then
begin
with MainForm.IdMessage1 do
begin
Recipients.Add.Address := 'Recipient';
Subject := 'Opfølgning på ' + Query1.FieldByName('Employee').AsString + ' med ID: ' + IntToStr(Query1.FieldByName('ID').AsInteger) + ' hos ' + Query1.FieldByName('Boss').AsString;
Body.Clear;
Body.Add('');
Body.Add('');
if (trimRight(Query1.FieldByName('AS400').AsString) <> 'NO') then
Body.Add(Query1.FieldByName('AS400').AsString + ' skal have objekterne.');
Body.Add('');
if not Query1.FieldByName('PCDisabled').AsBoolean then
begin
//
end;
Body.Add('');
Body.Add('Br Mig Selv');
end;
with MainForm.IdSMTP1 do
begin
try
Connect;
Send(MainForm.IdMessage1);
Disconnect;
except on E: Exception do
begin
if connected then try disconnect; except end;
ShowMessage(E.Message);
end;
end;
end;
end;
try
Query := TADOQuery.Create(Nil);
Query.Connection := MainForm.ADOConnection1;
with Query do
begin
SQL.Clear;
SQL.Add('Update StaffLeftCompany');
SQL.Add('Set DateOfOperator = :DateOfOperator');
SQL.Add('Where SecurityCode = :SecurityCode');
Parameters.ParamByName('DateOfOperator').Value := DateOf(now);
Parameters.ParamByName('SecurityCode').Value := Query1.FieldByName('SECURITYCODE').AsString;
Prepared := true;
ExecSQL;
end;
finally
Query.Free;
end;
end;
end;
32..62 : begin
if CompareDate(Query1.fieldByName('DateOfOperator').AsDateTime,Date) = -1 then
begin
CoInitializeEx(0,COINIT_MULTITHREADED);
if SUCCEEDED(ADsGetObject('LDAP://cn=' + Query1.FieldByName('Employee').AsString + ',OU=USERS,OU=SO,OU=DK,OU=COMPANY,DC=AD,DC=COMPANY,DC=ORG',IADsUser, User)) then
begin
if User.AccountDisabled then
begin
if SUCCEEDED(ADsGetObject('LDAP://OU=USERS,OU=SO,OU=DK,OU=ALFALAVAL,DC=AD,DC=COMPANY,DC=ORG', IADsContainer, Container)) then
begin
Container.Delete('User','cn=' + Query1.FieldByName('Employee').AsString);
Container._Release;
end;
User._Release;
end;
CoUnInitialize;
end;
with MainForm.IdMessage1 do
begin
Recipients.Add.Address := 'Recipient';
Subject := 'Opfølgning på ' + Query1.FieldByName('Employee').AsString + ' med ID: ' + IntToStr(Query1.FieldByName('ID').AsInteger) + ' hos ' + Query1.FieldByName('Boss').AsString;
if not Query1.FieldByName('MailCopyStatus').AsBoolean then
begin
if (trimRight(Query1.FieldByName('MakeCopyMailDB').AsString) <> 'NO') then
begin
Body.Clear;
Body.Add('Kopi af mail ønskes. Skal sendes til ' + Query1.FieldByName('Boss').AsString);
Body.Add('');
Body.Add('Category: GBL APPLICATIONS');
Body.Add('Subcategory: LOTUS NOTES DATABASES');
Body.Add('Product Type: NONE');
Body.Add('Problem: ACCESS');
end;
end;
if not Query1.FieldByName('ForwardStatus').AsBoolean then
begin
if (trimRight(Query1.FieldByName('ForwardMail').AsString) <> 'NO') then
Body.Add('Mail ønskes videresendt til: ' + trimRight(Query1.FieldByName('ForwardMail').AsString));
end;
if not Query1.FieldByName('MailDeleteStatus').AsBoolean then
begin
Body.Add('Mail skal slettes.');
end;
Body.Add('');
Body.Add('Br Mig Selv');
with MainForm.IdSMTP1 do
begin
try
Connect;
Send(MainForm.IdMessage1);
Disconnect;
except on E: Exception do
begin
if connected then try disconnect; except end;
ShowMessage(E.Message);
end;
end;
end;
try
Query := TADOQuery.Create(Nil);
Query.Connection := MainForm.ADOConnection1;
with Query do
begin
SQL.Clear;
SQL.Add('Update StaffLeftCompany');
SQL.Add('Set DateOfOperator = :DateOfOperator');
SQL.Add('Where SecurityCode = :SecurityCode');
Parameters.ParamByName('DateOfOperator').Value := DateOf(now);
Parameters.ParamByName('SecurityCode').Value := Query1.FieldByName('SECURITYCODE').AsString;
Prepared := true;
ExecSQL;
end;
finally
Query.Free;
end;
end;
end;
end;
else
if CompareDate(Query1.fieldByName('DateOfOperator').AsDateTime,Date) = -1 then
begin
if (not Query1.FieldByName('BPCSStatus').AsBoolean) or (not Query1.FieldByName('PCDisabled').AsBoolean)
or (not Query1.FieldByName('MailCopyStatus').AsBoolean) or (not Query1.FieldByName('MailDeleteStatus').AsBoolean) then
begin
with MainForm.IdMessage1 do
begin
Recipients.Clear;
Body.Clear;
Recipients.Add.Address := 'Recipient';
Subject := 'Opfølgning på ' + Query1.FieldByName('Employee').AsString + ' med ID: ' + IntToStr(Query1.FieldByName('ID').AsInteger) + ' hos ' + Query1.FieldByName('Boss').AsString;
if not Query1.FieldByName('BPCSStatus').AsBoolean then
begin
//
end;
if not Query1.FieldByName('PCDisabled').AsBoolean then
begin
Body.Add('');
Body.Add('Husk at disable ' + Query1.FieldByName('ComputerName').AsString);
end;
if not Query1.FieldByName('MailCopyStatus').AsBoolean then
begin
if (trimRight(Query1.FieldByName('MakeCopyMailDB').AsString) <> 'NO') then
begin
Body.Add('Kopi af mail ønskes. Skal sendes til ' + Query1.FieldByName('Boss').AsString);
Body.Add('');
Body.Add('Category: GBL APPLICATIONS');
Body.Add('Subcategory: LOTUS NOTES DATABASES');
Body.Add('Product Type: NONE');
Body.Add('Problem: ACCESS');
end;
end;
if not Query1.FieldByName('ForwardStatus').AsBoolean then
begin
if (trimRight(Query1.FieldByName('ForwardMail').AsString) <> 'NO') then
Body.Add('Mail ønskes videresendt til: ' + trimRight(Query1.FieldByName('ForwardMail').AsString));
end;
if not Query1.FieldByName('MailDeleteStatus').AsBoolean then
begin
Body.Add('Mail skal slettes.');
end;
Body.Add('');
Body.Add('Br Mig Selv');
end;
with MainForm.IdSMTP1 do
begin
try
Connect;
Send(MainForm.IdMessage1);
Disconnect;
except on E: Exception do
begin
if connected then try disconnect; except end;
ShowMessage(E.Message);
end;
end;
end;
try
Query := TADOQuery.Create(Nil);
Query.Connection := MainForm.ADOConnection1;
with Query do
begin
SQL.Clear;
SQL.Add('Update StaffLeftCompany');
SQL.Add('Set DateOfOperator = :DateOfOperator');
SQL.Add('Where SecurityCode = :SecurityCode');
Parameters.ParamByName('DateOfOperator').Value := DateOf(now);
Parameters.ParamByName('SecurityCode').Value := Query1.FieldByName('SECURITYCODE').AsString;
Prepared := true;
ExecSQL;
end;
finally
Query.Free;
end;
end;
end;
end;
end;
// Mails sendes til InternService
if (TrimRight(Query1.FieldByName('NewEmployee').AsString) = 'NO') and (not Query1.FieldByName('PCReturnStatus').AsBoolean) then
begin
case comparedate(Query1.FieldByName('DATEOFRETIRE').AsDateTime,Date) of
-1 : begin
case DaysBetween(Query1.FieldByName('DateOfIntern').AsDateTime, Date) of
0 : begin
if Query1.FieldByName('Reminder').AsInteger = 0 then
begin
UpdateMail := true;
end;
end;
7 : begin
UpdateMail := true;
end;
end;
if UpdateMail then
begin
with MainForm.IdMessage1 do
begin
Recipients.Clear;
Body.Clear;
if MainForm.Checkbox2.Checked then
Recipients.Add.Address := 'Recipient'
else
Recipients.Add.Address := 'Recipient';
Subject := 'Afhenting af ' + Query1.FieldByName('Employee').AsString + 's ' + Query1.FieldByName('COMPUTERNAME').AsString + ' hos ' + Query1.FieldByName('Boss').AsString;
Body.Add('Br');
Body.Add('ITService Desk');
end;
if not MainForm.Checkbox2.Checked then
begin
with MainForm.IdSMTP1 do
begin
try
Connect;
Send(MainForm.IdMessage1);
Disconnect;
except on E: Exception do
begin
if connected then try disconnect; except end;
ShowMessage(E.Message);
end;
end;
end;
try
Query := TADOQuery.Create(Nil);
Query.Connection := MainForm.ADOConnection1;
with Query do
begin
SQL.Clear;
SQL.Add('Update StaffLeftCompany');
SQL.Add('Set DateOfIntern = :DateOfIntern,');
SQL.Add(' reminder = reminder + 1 ');
SQL.Add('Where SecurityCode = :SecurityCode');
Parameters.ParamByName('DateOfIntern').Value := DateOf(now);
Parameters.ParamByName('SecurityCode').Value := Query1.FieldByName('SECURITYCODE').AsString;
Prepared := true;
ExecSQL;
end;
finally
Query.Free;
end;
UpdateMail := false;
end;
end;
end;
end;
end;
// Confirm mail sendes til Boss
if (not Query1.FieldByName('MailConfirmToBoss').AsBoolean) then
begin
with MainForm.IdMessage1 do
begin
Recipients.Clear;
Body.Clear;
Subject := 'Confirm mail for updating ' + Query1.FieldByName('Employee').AsString;
if MainForm.Checkbox2.Checked then
Recipients.Add.Address := 'Recipient'
else
Recipients.Add.Address := Query1.FieldByName('EMAILOFBOSS').AsString;
Body.Add('Based on your choice of termination form following will happen.');
Body.Add('');
Body.Add('ForwardMail : ' + Query1.FieldByName('ForwardMail').AsString);
Body.Add('Make a copy : ' + Query1.FieldByName('MakecopyMailDB').AsString);
Body.Add('Delete AS400 : ' + Query1.FieldByName('AS400').AsString);
if (TrimRight(Query1.FieldByName('NewEmployee').AsString) = 'NO') then
begin
Body.Add('');
Body.Add('Please, you must have ' + Query1.FieldByName('COMPUTERNAME').AsString + ' and SecureID in your possession when the Internal Service Department comes to retrieve it.');
end
else
Body.Add('New Employee : ' + Query1.FieldByName('NewEmployee').AsString);
Body.Add('');
Body.Add('Br');
Body.Add('ITService Desk');
end;
with MainForm.IdSMTP1 do
begin
try
Connect;
Send(MainForm.IdMessage1);
Disconnect;
except on E: Exception do
begin
if connected then try disconnect; except end;
ShowMessage(E.Message);
end;
end;
end;
if not MainForm.Checkbox2.Checked then
begin
try
Query := TADOQuery.Create(Nil);
Query.Connection := MainForm.ADOConnection1;
with Query do
begin
SQL.Clear;
SQL.Add('Update StaffLeftCompany');
SQL.Add('Set DateOfITUpdate = :DateOfITUpdate,');
SQL.Add(' MailConfirmToBoss = :MailConfirmToBoss ');
SQL.Add('Where SecurityCode = :SecurityCode');
Parameters.ParamByName('DateOfITUpdate').Value := DateOf(now);
Parameters.ParamByName('MailConfirmToBoss').Value := True;
Parameters.ParamByName('SecurityCode').Value := Query1.FieldByName('SECURITYCODE').AsString;
Prepared := true;
ExecSQL;
end;
finally
Query.Free;
end;
end;
end;
end;
Query1.Next;
end;
finally
Query1.Close;
end;
end;
