[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

Access violation

Hi

Can somebody please help me with an access violation i get after i close a report?  ive looked through my code a hundred times and cant figure it out.  The code is below:

c := StrToFloat(EditVat5.text);
       
        if (DBComboBox1.Text = 'Cash') OR (DBComboBox1.text = 'EFT') then
        begin
                if DBLookupCombobox1.text = '' then
                        ShowMessage('Please select a bank account')
                else
                begin
                        a := StrToFloat(EditTotal5.text);
                        b := StrToFloat(EditDepositAmount.text);
                        DBEdit1.text := FloatToStr((a * b) / 100);

                        DataModule1.tblBooking.FieldByName('DepositAmount').value := DBEdit1.text;
                        DataModule1.tblBooking.FieldByName('DateSent').AsString := DateToStr(Date());
                        DataModule1.tblBooking.FieldByName('Deposit').AsString := 'No';

                        directory := 'C:\Quotes\'+DBEdit8.text;

                        if DirectoryExists(directory) = false then
                        begin
                                MkDir('C:\Quotes\'+DBEdit8.text);

                                SetCurrentDir('C:\Quotes\'+DBEdit8.text);
                                GetCurrentDir;
                                AttachmentPath := 'C:\Quotes\'+DBEdit8.text+'\';
                                FileType := '.pdf';

                                AttFileName := AttachmentPath + DBEdit8.text + '-B' + '-' + DBEdit9.text + '-' + DBEdit10.text + '-' + DBEdit11.text + '-' +DBEdit12.text + Filetype;
                        end
                        else
                        begin
                               SetCurrentDir('C:\Quotes\'+DBEdit8.text);
                               GetCurrentDir;
                               AttachmentPath := 'C:\Quotes\'+DBEdit8.text+'\';
                               FileType := '.pdf';

                               AttFileName := AttachmentPath + DBEdit8.text + '-B' + '-' + DBEdit9.text + '-' + DBEdit10.text + '-' + DBEdit11.text + '-' +DBEdit12.text + Filetype;
                        end;
                  
                  //this is the filename the attachment will be saved as.  user copies and paste it in the save as dialog
                        InputBox('Customer Number', 'Customer Number', DBEdit8.text + '-B' + '-' + DBEdit9.text + '-' + DBEdit10.text + '-' + DBEdit11.text + '-' + DBEdit12.text);
                       
                  DataModule1.tblBooking.post;

                        with DataModule1.ADOQuery5 do begin
                        close;
                                With SQL do begin
                                clear;
                                Add('Select * from Customer, Quote, Function, Booking');
                                Add('Where Booking.FunctionID = Function.FunctionID');
                                Add('AND Function.QuoteID = Quote.QuoteID');
                                Add('AND Quote.CustomerID = Customer.CustomerID');
                                Add('AND Booking.FunctionID = :DBEdit5');
                                Parameters.ParamByName('DBEdit5').value := DBEdit5.text;
                                end;
                        open;
                        end;  

                  //the 4 different booking reports
                        if DBEdit11.text = 'Corporate' then
                        begin
                                try
                                        QRCorporateBooking := TQRCorporateBooking.create(application);
                                        QRCorporateBooking.preview;
                                finally
                                        QRCorporateBooking.free;
                                end;
                        end

                        else if DBEdit11.text = 'Birthday' then
                        begin
                                try
                                        QRBirthdayBooking := TQRBirthdayBooking.create(application);
                                        QRBirthdayBooking.preview;
                                finally
                                        QRBirthdayBooking.free;
                                end;
                        end

                        else if DBEdit11.text = 'Wedding' then
                        begin
                                try
                                        QRWeddingBooking := TQRWeddingBooking.create(application);
                                        QRWeddingBooking.preview;
                                finally
                                        QRWeddingBooking.free;
                                end;
                        end
                        else
                        begin
                                try
                                        QRBookingReport := TQRBookingReport.create(application);
                                        QRBookingReport.preview;
                                finally
                                        QRBookingReport.free;
                                end;
                        end;

                        if c > 0 then  //creates a tax invoice
                        begin
                        try
                                directory := 'C:\Quotes\'+DBEdit8.text;

                                if DirectoryExists(directory) = false then
                                begin
                                        MkDir('C:\Quotes\'+DBEdit8.text);

                                        SetCurrentDir('C:\Quotes\'+DBEdit8.text);
                                        GetCurrentDir;
                                        AttachmentPath := 'C:\Quotes\'+DBEdit8.text+'\';
                                        FileType := '.pdf';

                                        AttFileName2 := AttachmentPath + DBEdit8.text + '-I1' + '-' + DBEdit9.text + '-' + DBEdit10.text + '-' + DBEdit11.text + '-' +DBEdit12.text + Filetype;
                                end
                                else
                                begin
                                        SetCurrentDir('C:\Quotes\'+DBEdit8.text);
                                        GetCurrentDir;
                                        AttachmentPath := 'C:\Quotes\'+DBEdit8.text+'\';
                                        FileType := '.pdf';

                                        AttFileName2 := AttachmentPath + DBEdit8.text + '-I1' + '-' + DBEdit9.text + '-' + DBEdit10.text + '-' + DBEdit11.text + '-' +DBEdit12.text + Filetype;
                                end;

                                    //this is the filename the attachment will be saved as
                                        InputBox('Customer Number', 'Customer Number', DBEdit8.text + '-I1' + ' ' + DBEdit9.text + ' ' + DBEdit10.text + ' ' + DBEdit11.text + ' ' + DBEdit12.text);

                                        QRInvoiceReportYes := TQRInvoiceReportYes.create(application);
                                        QRInvoiceReportYes.preview;
                        finally
                                        QRInvoiceReportYes.free;
                        end;
                        end
                        else if c = 0 then  //creates a non-tax invoice
                        begin
                        try
                                directory := 'C:\Quotes\'+DBEdit8.text;

                                if DirectoryExists(directory) = false then
                                begin
                                        MkDir('C:\Quotes\'+DBEdit8.text);

                                        SetCurrentDir('C:\Quotes\'+DBEdit8.text);
                                        GetCurrentDir;
                                        AttachmentPath := 'C:\Quotes\'+DBEdit8.text+'\';
                                        FileType := '.pdf';
                              
                              //creates the filename for attachment in email
                                        AttFileName2 := AttachmentPath + DBEdit8.text + '-I1' + '-' + DBEdit9.text + '-' + DBEdit10.text + '-' + DBEdit11.text + '-' +DBEdit12.text + Filetype;
                                end
                                else
                                begin
                                        SetCurrentDir('C:\Quotes\'+DBEdit8.text);
                                        GetCurrentDir;
                                        AttachmentPath := 'C:\Quotes\'+DBEdit8.text+'\';
                                        FileType := '.pdf';

                              //creates the filename for attachment in email
                                        AttFileName2 := AttachmentPath + DBEdit8.text + '-I1' + '-' + DBEdit9.text + '-' + DBEdit10.text + '-' + DBEdit11.text + '-' +DBEdit12.text + Filetype;
                                end;

                        //this is the filename the attachment will be saved as.  user copies and paste it in the save as dialog
                                InputBox('Customer Number', 'Customer Number', DBEdit8.text + '-I1' + '-' + DBEdit9.text + '-' + DBEdit10.text + '-' + DBEdit11.text + '-' + DBEdit12.text);

                                QRInvoiceReport := TQRInvoiceReport.create(application);
                                QRInvoiceReport.preview;
                        finally
                              QRInvoiceReport.free;
                        end;
                end;

        if DBEdit21.text = 'Email' then  //if its fax, then user just prints the report
                begin
                //Booking Report
                SendMail('Thank you for choosing Cream Cheese.',
                   'Hi ' + DBEdit14.text + ' ' + CRLF + CRLF + DataModule1.tblCompany.FieldByName('InvoiceBookingWording').AsString + CRLF + CRLF + 'Regards,' + CRLF + CRLF +
                   'MornĂ© Rheeder' + CRLF + 'Director' + CRLF + 'Cream Cheese' + CRLF + CRLF + 'Share Call 08600-DISCO (08600-34726)' + CRLF + 'Direct + 27 (0)11-462-1103' +
                   CRLF + 'Cell +27 (0)83-413-0140' + CRLF + 'Fax + 27 (0)86-675-0608' + CRLF + 'E-mail morne@creamcheese.co.za' + CRLF + 'www.creamcheese.co.za' + CRLF + CRLF +
                   'If you cannot open PDF attachments, please download Adobe Reader' + CRLF + CRLF + 'Confidentiality:' + CRLF + 'Any e-mail messages from Cream Cheese may contain confidential information and is intended for a specific addresses and purpose.  if you are not the addresse you may not disclose, copy, distribute or take any action' +
                   ' based on the contents thereof.  Kindly inform the sender immediately and destroy all copies thereof.' + CRLF + CRLF +
                   'All attached information is Cream Cheese confidential.  Any duplication or unauthorised distribution of this message is prohibited.  Cream Cheese reserves the right for legal action should this occur.',
                   AttFileName,
                   DataModule1.tblCompany.FieldByName('OwnerFirstName').AsString, DataModule1.tblCompany.FieldByName('CompanyEmail').AsString,
                   DBEdit14.text, DBEdit15.text);

                //Invoice Report
                   SendMail('Thank you for choosing Cream Cheese.',
                   'Hi ' + DBEdit14.text + ' ' + CRLF + CRLF + DataModule1.tblCompany.FieldByName('InvoiceBookingWording').AsString + CRLF + CRLF + 'Regards,' + CRLF + CRLF +
                   'MornĂ© Rheeder' + CRLF + 'Director' + CRLF + 'Cream Cheese' + CRLF + CRLF + 'Share Call 08600-DISCO (08600-34726)' + CRLF + 'Direct + 27 (0)11-462-1103' +
                   CRLF + 'Cell +27 (0)83-413-0140' + CRLF + 'Fax + 27 (0)86-675-0608' + CRLF + 'E-mail morne@creamcheese.co.za' + CRLF + 'www.creamcheese.co.za' + CRLF + CRLF +
                   'If you cannot open PDF attachments, please download Adobe Reader' + CRLF + CRLF + 'Confidentiality:' + CRLF + 'Any e-mail messages from Cream Cheese may contain confidential information and is intended for a specific addresses and purpose.  if you are not the addresse you may not disclose, copy, distribute or take any action' +
                   ' based on the contents thereof.  Kindly inform the sender immediately and destroy all copies thereof.' + CRLF + CRLF +
                   'All attached information is Cream Cheese confidential.  Any duplication or unauthorised distribution of this message is prohibited.  Cream Cheese reserves the right for legal action should this occur.',
                   AttFileName2,
                   DataModule1.tblCompany.FieldByName('OwnerFirstName').AsString, DataModule1.tblCompany.FieldByName('CompanyEmail').AsString,
                   DBEdit14.text, DBEdit15.text);
                end;
        end;
end;

If anything is unclear, just ask....ive put in comments on so its made clear what some things are.
Basically it is a booking page, with a booking report, and invoice report that will be saved and sent as an attachment.  The code is for onclick button.  Ive used the same procedure on other forms, and there it works fine.  Only difference is that this code creates two reports after each other, where on other forms, i only create one report.

Any help would be appreciated.

Torment
0
whythetorment
Asked:
whythetorment
1 Solution
 
whythetormentAuthor Commented:
PS:  Im using Delphi 5 with ADO queries.  And an Access database
0
 
VoodoomanCommented:

Hi

I was in a similar situation myself the other day.  It took me a few hours to figure.  In my case it was redundant code in the calling form that I had overlooked.  I was getting an error 'unable to focus an invisible form or control.  On the form I was using all the controls were visible.

Instead of boggling your brain trying to figure it out, put message boxes in your code around different blocks with relevant messages ('block 1 O.K.', 'Block 2 O.K' etc.  This will enable you to quickly isolate where the error is occurring.  Once you have found where, post the code so others can help.

Regarding Access Violations in General.  I have had a problem where a Variable has been globally declared in a Unit with the name of I:Integer. This is a pretty common name. The Unit was part of a control I purchased.  When I declared a variable of I:Integer locally in a procedure, I got an Access Violation.  This is apparently a problem with D7 (and others) of memory allocation.  To get around this add FastSharemem.pas to your project . Get it at http://cc.borland.com/ccweb.exe/listing?id=19414 .

It is also 8/10 times faster that Borlands memshare - a must have for any project


Voodooman

Best Regards

Voodooman
0
 
whythetormentAuthor Commented:
thing that is strange, i can click on the button one moment, and after the reports are closed, it does nothing, then next time i go do another booking, then it gives the violation.  it doesnt happen every time.  all the variables ive used are declared locally.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BigRatCommented:
I have absolutely no experience with these components but I am always suspicious when I see things like :-

try
        QRBookingReport := TQRBookingReport.create(application);
        .......
finally
       QRBookingReport.free;
end;

what exactly does the Create do with Application? Does it bind the created object into some structure? Because seconds later the Free simply calls the destructor providing it is not nil and the destructor usually knows nothing about any bindings.
0
 
vadim_tiCommented:
I had such a problem
add Application.ProcessMessages
after every Preview calling.
0
 
VoodoomanCommented:

Whether the variables are declared locally or not is irrelevant.  Its to do with how the memory manager works.

In my case, the locally declared variables were the same as globally declared variables in a component.  It was Borland's memory manager that fouled up.

When you declare a variable, it's put on the heap (simplistically put).  When another variable is declared in another scope it should be put on the heap at another address.  If the memory manager fouls up and it atempts to use the memory occupied by another variable (by using the same pointer) you get an Exception.

Have yout tired my suggestions - it would only take a few minutes.....

Your scenario ' it doesnt happen every time' is typical of memory allocation errors.......


Voodooman
0
 
VoodoomanCommented:

Hi

I guess Application.ProcessMessages is the same as  VB Doevents?

Nice to know.....

Voodooman

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now