Solved

SaveDialog - trap errors - set options

Posted on 2008-10-31
8
811 Views
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?)
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

0
Comment
Question by:a3dvm
  • 5
  • 3
8 Comments
 
LVL 28

Expert Comment

by:ciuly
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:
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 ;)
0
 

Author Comment

by:a3dvm
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)
Thanks
ciurly1.jpg
ciurly2.jpg
0
 

Author Comment

by:a3dvm
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...
0
 

Author Comment

by:a3dvm
ID: 22855180
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 28

Accepted Solution

by:
ciuly 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
 with
showmessage('ok')

dammit.

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.
0
 

Author Comment

by:a3dvm
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.  
0
 
LVL 28

Expert Comment

by:ciuly
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.
0
 

Author Comment

by:a3dvm
ID: 22859060
I think that's best.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
git pull --rebase :always complain unstaged files 11 186
selectallvieditor 6 88
Intellij adding new line in xml 3 59
C#  WebForms or MVC 4 89
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
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 connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now