Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use cdlOFNOverwritePrompt as condition? 1

Status
Not open for further replies.

Svanholm

Programmer
Jul 11, 2007
31
0
0
DK
Is it possible to use cdlOFNOverwritePrompt as condition?

To be specific: I wish to let my program act in one way if cdlOFNOverwritePrompt dialog box appears during runtime and another way if it does not.

A simple coding example:

Code:
With CommonDialog1
   .Flags = cdlOFNOverwritePrompt 
   .ShowSave
End With

If cdlOFNOverWritePrompt Then 'Here is where it does not work
MsgBox Prompt:=("OverwritePrompt Dialog Box appeared")
End If

MsgBox Prompt:=("OverWritePrompt Dialog Box did not appear")

Problem is that the coding activates the if-statement whether or not the cdlOFNOverwritePrompt dialog box appears.

Is there a way around it?

What I am trying to do with this is to make one thing happen if the current file already exists and another if it does not.
 
If CommonDialog1.Flags = cdlOFNOverWritePrompt Then
MsgBox Prompt:=("OverwritePrompt Dialog Box appeared")
Else
MsgBox Prompt:=("OverWritePrompt Dialog Box did not appear")
end if
 
HughLerWill, thanks

That's what I thought at first (seems logical), but it does not work.

I get the MsgBox Prompt:=("OverwritePrompt Dialog Box appeared") every time.

If takes the if-statement either way.

I figured the logic is that the With statement actually says: CommonDialog1.Flags = OverwritePrompt

Which makes the if true every time.

In other words: The Flags-property just sort of load the CommonDialog1 with the possibility of OverwritePrompt(ing). But which function tricks the OverwritePrompt Dialog Box?
 
Erm, I think you slightly missed the point of cdlOFNOverWritePrompt: it is simply a configuration flag to CommonDialog instructing it to display an overwrite prompt if one is necessary (i.e we've picked an already existing filename). It is not a return value.

There is no (easy) way that I am aware of to determine whether that prompt appeared or not
 
Just what I figured after an day of testing.

I guess I just have to find another way of determining whether a filename already exists or not and make my program do one thing if it does and another if it does not.

But thanks strongm for saving more of my breath on this dead end.
 
>another way of determining whether a filename already exists or not

There are plenty of methods of doing this, for example VB's Dir command
 
You are right, strongm. There are plenty of methods.

And I have found a way, probably somewhat unorthodox and with a very long coding. But that is the way it is with languages: When you are a beginner, you use many words to describe simple issues.

Nevertheless, I thankfully bookmark every piece of advice given by all of you pros.

thx, pal.
 
dim FileExists as Boolean

FileExists = (Len(Dir$("C:\MyDir\MyFile.ext"))>0)
 
Interesting Hughlerwill.

Am gonna try it.

Wonder: Would it take all files in a directory if I change code to:

Dim Item As String

Item = Dir$("C:\MyDir\*.*")

List1.AddItem Item

Cause I need that later in my coding.
 
'paste this into a Form; you will need a List1 and Command1

Private Sub Command1_Click()
Dim a$

a$ = Dir$("*.*") 'all files in the CurDir$ folder
Do While Len(a$)
List1.AddItem a$
a$ = Dir$
Loop
End Sub
 
thx Hughlerwill

Got it.

Why a$ in the variable and not just a?

 
>$

Its just an old fashioned alternative to indicate a variable is a string type. In my previous I could have used Dim a As String (rather than Dim a$) at the top and then just used a in the code below. It's just a case of personal preference. I like the $ suffix on my string variables because it's shorter and it makes strings easy to spot in code.
Most people these days use the 'As String' convention when writing code but as you can see some VB functions like Dir$, Mid$ etc. carry on in the old tradition.
 
Thx for clearing that out (it will certainly help me read other threads) and thx for making me feel young again :)
 
>making me feel young again

If only.. Like a man once said to me;
<<
It's like watching a '57 Chevy drive down the road. It's rare, so you look. You may even feel compelled to point at it and remark to friends, "Hey... Did you see that? Cool huh?"
>>
 
I'm reminded of the evening I recently spent singing karaoke in front of the 20 something ink and metal set, and going outside to smoke my pipe. Someone said something to the effect of "hey, did you see that? Cool, huh?"

By the way, Svanholm, I had to take a second look at "do while Len(a$)", and why Hugh didn't say "do until len(a$) = 0" as I would have. His way is fine, of course, but the reason it works is a bit less obvious. "do while x" is short for "do while x = true". (conversely, "do while not x" is another way of saying "do while x = false.") So, "do while len(a$)" really means "do while len(a$) = true." Ok. Now, len(a$) is an integer. Integers translate to boolean as 0=false, everything else = true. So, as soon as len(a$) is 0, VB will interpret the while condition as false and exit the loop.

As I often have said when people lament the vagaries of VB syntax and usage, mastering these vagaries simply means that you can charge more money for your time than you could if they weren't there.

Bob
 
And now, Bob, perhaps you'd care to explain why Hugh uses

if len(a$) ...

rather than

if a$="" ...

:)
 
Interesting debate. And the beginner here is only grasping the thin surface of it. Bit like discussing black holes with Stephen Hawking; your explanations sound solid and clever, but it takes me an hour or so to understand what and why.

Anyway, if I got it right, the answer for strongm must be: They are both equally right. It is the old argument of whether the glass is half full or half empty. Or to be Booleanish: Full of something or full of nothing. Right? But I guess, you knew that before you posted your teaser.

Another puzzle for me is: You keep quoting "Hey did you see that? Cool huh?"
I am not familiar with the phrase, but it sounds a bit like Beavis and Butthead which I am only slightly familiar with. So I guess, it is an ironic statement. Otherwise: Feel free to educate me, a European ignorant. :)


 
Interesting debate. And the beginner here is only grasping the thin surface of it. Bit like discussing black holes with Stephen Hawking; your explanations sound solid and clever, but it takes me an hour or so to understand what and why.

Anyway, if I got it right, the answer for strongm must be: They are both equally right. It is the old argument of whether the glass is half full or half empty. Or to be Booleanish: Full of something or full of nothing. Right? But I guess, you knew that before you posted your teaser.

Another puzzle for me is: You keep quoting "Hey did you see that? Cool huh?"
I am not familiar with the phrase, but it sounds a bit like Beavis and Butthead which I am only slightly familiar with. So I guess, it is an ironic statement. Otherwise: Feel free to educate me, a European ignorant. :)


 
>"Hey did you see that? Cool huh?"

Told to me when I was being old fashioned in thread222-1290846

>why Hugh uses If len(a$) ...

it is considerably faster because it avoids a string comparison, and probably produces a smaller executable than If a$<>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top