Link to home
Start Free TrialLog in
Avatar of a3dvm
a3dvm

asked on

SaveDialog - trap errors - set options

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?)
So,
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.
Thanks.
procedure TFrameBar7.sBitBtn1Click(Sender: TObject);
   var
  fileSource, fileDest: string;
begin
  if sSavedialog1.execute then// only save if user actually selected a file
  begin
    fileSource := 'C:\No25Data\No25ContactData.mdb';
    filedest:=sSavedialog1.filename;
  CopyFile(PChar(fileSource), PChar(fileDest), False);
  sShowMessage('Backup Successfull','Backup was completed successfully. File saved to'+#13+#10' '+filedest+'.') ;
  end
  else
  sShowMessage('Backup Aborted','Backup cancelled at users request');
end;

Open in new window

Avatar of 2266180
2266180
Flag of United States of America image

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:
overwriteprompt
pathmustexist
filemustexist
noreadonlyreturn

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
else
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;
begin
  ok:=false;
while sSavedialog1.execute do // the if then becaomes while do
begin
  if sametext(extractfilepath(sSavedialog1.filename), 'C:\No25Data\') then
  begin
    showmessage('cannot save to that folder');
    continue;
  end;
    fileSource := 'C:\No25Data\No25ContactData.mdb';
    filedest:=sSavedialog1.filename;
  ok:=CopyFile(PChar(fileSource), PChar(fileDest), False) ;
  if ok then
    show ok
  else
    show error (syserrormessage(getlasterror))
  break;
end;
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 ;)
Avatar of a3dvm
a3dvm

ASKER

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)
Thanks
ciurly1.jpg
ciurly2.jpg
Avatar of a3dvm

ASKER

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...
Avatar of a3dvm

ASKER

After Googling the errors, I changed:
if ok then
    show ok
  else
    show error (syserrormessage(getlasterror))
  break;
end;

to:

if ok then
      show
      else
      ShowMessage(SysErrorMessage(GetLastError));
     break;
    end;

...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.
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of a3dvm

ASKER

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.  
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.
Avatar of a3dvm

ASKER

I think that's best.