Need clarification on save button code

santoshbhat
10-07-2009, 09:10 AM
Hello...
I have the following code for save button. The code is written in VB 6.0.. In the below code, when the save button is clicked, a window will be popped up and asks the location to save the file. But if I click the cancel button in the popped window it is giving a runtime error. Please go through the code and help me.. I want cancel button working fine...

Private Sub btnSave_Click()
With CommonDialog1
.CancelError=False
.InitDir="C:\"
.Filter="text files(*.txt)|*.txt|"
.ShowSave
End With
Open CommonDialog1.Filename For Append As #1
Print #1, Text1.Text ' here text present in Text1 is saved on to file
Close #1
End Sub

Thanx in advance

EmptyVessel
10-07-2009, 10:37 AM
Your error is happening because there is no filename to open. (Because you pressed Cancel)

You want: CancelError = True
Not false.

This makes the Common Dialog box generate an error when you press cancel

Then use an 'if' statement to check the Err object before trying to open your file.

Private Sub btnSave_Click()
With CommonDialog1
.CancelError=True
.InitDir="C:\"
.Filter="text files(*.txt)|*.txt|"
.ShowSave
End With

If Err.Number = 0 then
' Cancel button was not pressed
Open CommonDialog1.Filename for Append As #1
Print #1, Text1.Text
Close #1
Else
' Cancel Button was pressed
' Clean up the Error so your program does not fail.
Err.Clear
End If
End Sub


Cheers!

santoshbhat
10-07-2009, 08:26 PM
Thank you very much for your help... It really worked.. But very sorry for late acknowledgment

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum