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.
 
Thanks Hugh, of course that's so. However, if speed is the consideration, doesn't "if a$ <> 0" avoid a cast to boolean that "if a$" requires, and therefore isn't it more efficient?

<They are both equally right.
Well, even if they aren't equally "right", any difference they may have is unlikely to be a practical one. My other answer to strongm's question is "because he thinks it's prettier," which is of course a valid reason to adopt one syntax over another when the differences in efficiency are purely academic.

Bob
 
>However...

Copy this into a Form; you'll need a Command1 and a Command2

Private Sub Command1_Click()

Dim i&
Dim t!
t = Timer
For i = 1 To 100000000
If Len(a$) Then

End If
Next
Print Timer - t

End Sub

Private Sub Command2_Click()

Dim i&
Dim t!
t = Timer
For i = 1 To 100000000
If a$ = "" Then

End If
Next
Print Timer - t

End Sub

Notwithstanding precision timers I figure it's nearly twice as quick and just use it.
 
Hmmm?

On my pc, they both take almost exactly 10 sec to calculate.

What is being timed? counting to a billion or what?
 
>What is being timed?

One loop is using If Len(a$), the other uses If a$ = "".

In the IDE I get about 2 seconds and 4 seconds respectively. Compiled about .4 seconds and 2 seconds.
 
Interestingly, when I used your code, Hugh, and then added another button to evaluate len(a$) = 0 I was surprised to find that the boolean cast was a bit faster than the integer evaluation. So I tried this:
Code:
Option Explicit

Dim a$

Private Sub Command1_Click()

    Dim i&
    Dim t!
    t = Timer
    For i = 1 To 100000000
        If Len(a$) Then
        
        End If
    Next
    Debug.Print Timer - t

End Sub

Private Sub Command2_Click()
    
    Dim i&
    Dim t!
    t = Timer
    For i = 1 To 100000000
        If a$ = "" Then
        
        End If
    Next
    Debug.Print Timer - t

End Sub

Private Sub Command3_Click()
    
    Dim i&
    Dim t!
    t = Timer
    For i = 1 To 100000000
        If Len(a$) = 0 Then
        
        End If
    Next
    Debug.Print Timer - t

End Sub


Private Sub Form_Load()
a$ = "Hello world"
End Sub
I found that Command1 averaged about 3.5 seconds, Command2 about 5.5 seconds, and Command3 about 3 seconds.

Bob
 
Bob, Your use of Debug.Print tells me you have not tried it compiled, in this case it makes quite a difference. It's compiled that counts...

Compiled with straight Print (to Form) and ramping it up to 100,000,000 (another 10x) loops I get;

Command1 3.06 100% ref
Command2 18.30 590% this is significant
Command3 3.03 99% I take note it's better but we are spitting hairs now

When we and the computers were young stuff like this made a real difference; nowdays it is'nt so critical but take a few such techniques, use them routinely, and the exe will still be faster and smaller.
 
Good point, Hugh, and of course I should have done that. Anyway, yes, we are splitting hairs, and we always have been. As I mentioned before, any difference is unlikely to be a practical one. Furthermore, on my machine, they ran at the same speed exactly once I compiled them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top