SaveDialog - trap errors - set options

Posted on 2008-10-31
Last Modified: 2013-11-23
Hi Experts
I got this code from Ciurly a while back, and thought it worked fine.
But unfortunately, my ignorance of setting up the SaveDialog options, led to a near disaster.
I was struggling with a problem of why the Access database fell over corrupted with 'unrecognised database' error (unrecoverble too) - when fortunately the user found another bug that sent this code in to a loop.
Unravelling it I found that because I had set the 'InitialDir' to the app folder (as I didn't know what letter the USB drive would be) when the user tested it (without a USB key present) she  still clicked 'Save' instead of Cancel to exit. The code then attempted to save the .mdb file to the one that was in use. This caused the loop with 'bookmark error' etc. and also the next time opening - a corrupt database! Lucky she found the second error...or I'd still be looking.

So now I've changed the SaveDialog InitialDir to M: (arbitrary - it shows as blank folder in the Save dialog) to force the user to choose a suitable location. (Filename.ext is supplied)
However, this code still shows the 'Backup Successfull' message and gives the appearance of working - even when a folder / Drive destintion is not provided, and I need to trap that.
(the 'if sSavedialog1.execute' then line doesn't appear to prevent saving if the file name is provided?)
1. Is setting the correct filename.ext as filename, but the M: drive accepatble prcatice?
(I didn't want to set it as E:\ as unpredictable if a printer with USB or such, is  connected and taking drive E:\ and might error)
2. How do I trap the blank drive / no drive selected error with a message?
3. Is there any way of prohibiting the file being saved to C:\No25Data? (this seems to cause the corruption)
(the only savedialog options I've changed are FileName=No25ContactData.mdb - Filter MDB Files (*.mdb) |*. mdb - InitialDir=M: .
I would beg your indulgence for answers in code format if poss please - I'm a novice and don't understand all the routines you guys sometimes offer and accept as given.
procedure TFrameBar7.sBitBtn1Click(Sender: TObject);
  fileSource, fileDest: string;
  if sSavedialog1.execute then// only save if user actually selected a file
    fileSource := 'C:\No25Data\No25ContactData.mdb';
  CopyFile(PChar(fileSource), PChar(fileDest), False);
  sShowMessage('Backup Successfull','Backup was completed successfully. File saved to'+#13+#10' '+filedest+'.') ;
  sShowMessage('Backup Aborted','Backup cancelled at users request');

Open in new window

Question by:a3dvm
  • 5
  • 3
LVL 28

Expert Comment

ID: 22853665
you need to set up your dialog first, corrrectly. in the object inspector, go to options proeprty and make sure the following proeprties are set to true:

if your application allows saving to a file whicih does not exist (in which case it creates it), then do not set filemustexist to true.

also, change your code so that

if CopyFile(PChar(fileSource), PChar(fileDest), False) then
  show ok
show error (syserrormessage(getlasterror))


if you do not want to save to a specific folder, then you need some more changes like this:

var ok:boolean; fileSource, fileDest: string;
while sSavedialog1.execute do // the if then becaomes while do
  if sametext(extractfilepath(sSavedialog1.filename), 'C:\No25Data\') then
    showmessage('cannot save to that folder');
    fileSource := 'C:\No25Data\No25ContactData.mdb';
  ok:=CopyFile(PChar(fileSource), PChar(fileDest), False) ;
  if ok then
    show ok
    show error (syserrormessage(getlasterror))
if not ok then
sShowMessage('Backup Aborted','Backup cancelled at users request');

-- everything was written directly ion browser so it might not compile. but I figure you can figure out thigs, right? if not, just ask ;)

ps: I wouldn't be able to compile anyway since I don't have the entire code. like sshowmessage ;)

Author Comment

ID: 22854026
Hi again, ciurly
Thanks for quick response.
All goes well up to "show ok" line, then barfs at missing operator or semicolon. But if I add a semicolon before an Else, it barfs again.
Three others that might go away if the first ones fixed? (see attachments)
I've hit this 'missing semicolon before Else' with dialog boxes before - and it's driven me mad.
I've fiddled with it - but really don't know what to do - figured it would be better to ask.
Can you give me a further hand?
(the sShowMessage is AlphaSkin component to make things look pretty. Same as ShowMessage in operation)

Author Comment

ID: 22854133
Hi Ciurly
Just to confirm, I did add an ' end; ' after your sShowMessage line.
Also tried a Begin & End; to enclose the ' show ok ' and several other trys - just added more errors...
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 22855180
After Googling the errors, I changed:
if ok then
    show ok
    show error (syserrormessage(getlasterror))


if ok then

...and it seems to work. Would you agree with this?

I also found that initially with an intentionally blank destination (M:) - it saved to thin air twice apparently - then on the third attempt the system error 'Path Not Found' kicked in.
I tried so many Filename / Extension combinations to cure it, I can't remember what changed it now. As far as I can see, the settings are back pretty much as they were, so I'm none the wiser.
LVL 28

Accepted Solution

2266180 earned 125 total points
ID: 22857874
COME ON. you ar acting like this is the first day when you are coding delphi/pascal. what is this? must I give you everythgin 100% correct, can';t you see whn I am using PSEUDO CODE? jesus christ.

"show ok". it's english. show an ok message. geez man.
replace show ok


I told you what dialog options to make true. if M: does nto exist the dialopg will not allow you to save to that path. it's that simple. if it allows, it measnn you haven't set the options correctly.

try to THINK. use your logic. that is why you are human and have a brain. If I feed improper code to a machine, it will error. a human brain should be able to logically detect when a given statement is not ok.

if you just cannot do it yourself, say it, and I will make time one of the following days and I'll make a demo for you.  programming is using your brain. not "testing to see if it works". you must understand what you do around there and do it correctly. the fact that you keep testing a lot of stupid options only underlines that you are far from being a programmer. a programmer thinks. a tester tests. see the difference? you don't want to become a tester, you want to become a programmer. use the delphi help and read on all options and understand what they do. THEN, use them. testing different things will not get you anywhere. you're approach is wrong. even though you might think that you learn something, you actually don't. you're just making a big mess in your brain.
imagine that programming is like taking a math quis. teacher asks how much is 3 plus 5. you must say 8. not , 4? 7? 9? 11? etc. programming i snot guessing.

Author Comment

ID: 22858207
Thanks for the lecture.
Re: your "Can't you see when I'm using pseudo code? jesus christ" etc.
Frankly, No. "show ok" (when there's an "ok" variable declared) might well be legitimate to me, even though I might not understand it - I kinda figured you did.

That's why I wrote in the original - "I would beg your indulgencge ~ I'm a novice and don't understand all the routines you guys sometimes offer and accept as given."
Given that statement and your seemingly high expectations of authors - perhaps you shouldn't have responded?

Perhaps I'm in the wrong place.
I am a complete novice without any formal training, not a professional programmer, and have done my best (by weeks/months of books, help files and Googling) to produce my first database from scratch.

It was/is an ambitious project for a lone beginner without any mentor support - and I probably should have stuck with "Hello World" type projects for a few years longer. but I'm over 60 now, and don't have forever.

And no - I don't understand yet excatly what I've managed to do, or how - exactly - it all does what it does. I know by experience though that an ounce of explanation is worth a ton of books and help files.

But piece by piece over the last few months - I've come from knowing exactly zero to having produced an aesthetically pleasing, funtional (with obvious exceptions) project for practical 'in house' use (not fully commercial) - but mostly to try and expand my horizons in a new area.

Then I stumbled over the Experts Exchange and noted they had a rank for Beginners as well as Masters like yourself, which encouraged me.
My first few contacts were patient with a beginner and extremely helpful, and did as much to encourage me as you have now done to deflate me.
Maybe I was wrong - perhaps Experts Exchange is meant to be just that - one Expert talking to another Expert about things they know all about.
In which case, I'll award your points, and bid you adieu.
p.s. As the corrupt database was holding up a label run, I did my best to understand it and got the code working as I wanted it to. Thanks for your pointers and pseudo code.
Though I doubt you would approve my method - trial and error. Time didn't permit extensive research and digestion of logic.  
LVL 28

Expert Comment

ID: 22858965
yeah, sure, and time didn't permit you to read the grading tips either. oh well, since you do thing your way, I'll do it my way and I won't help you again. that way you don't have to worry about my pseudo code and stuff.

Author Comment

ID: 22859060
I think that's best.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
importing  maven Project 5 312
Eclipse insufficient memory error 3 113
Netbeans Open project initial dir 5 46
Visual Studio C# project complains it can't run a dll 5 106
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question