?
Solved

overwrite existing file

Posted on 2000-04-10
3
Medium Priority
?
229 Views
Last Modified: 2010-05-02
How can I set the default filename in the save dialog box to a varible name, such as strTitle, and ask to overwrite if the file already exists. Her is the code.
Private Sub cmdSave_Click()

'Declare variable
Dim strFilename As String
'Do the error handler
cdbOpenSave.CancelError = True
On Error GoTo ErrHandler

'Set the properties of the text control
cdbOpenSave.Filter = _
"Text Files|*.txt*"

'Show the save-as dialog box
cdbOpenSave.ShowSave

'retrieve the filename
strFilename = cdbOpenSave.FileName & ".txt"

'save the file
Open strFilename For Output As #1
Print #1, Text1
Close #1

Exit Sub
ErrHandler:
End
0
Comment
Question by:Ginger16
3 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2702712
Do you mean you already want to have something in the box there like a filename ? If so, then this would work
just assign a string with file name to a property FileName.


Dim sFile as string
Dim strFilename As String
'Do the error handler
cdbOpenSave.CancelError = True
On Error GoTo ErrHandler

'Set the properties of the text control
cdbOpenSave.Filter = _
"Text Files|*.txt*"
sFile = "SomeFile"
cdb.FileName = sFile
'Show the save-as dialog box
cdbOpenSave.ShowSave

'retrieve the filename
strFilename = cdbOpenSave.FileName & ".txt"

'save the file
Open strFilename For Output As #1
Print #1, Text1
Close #1

Exit Sub
ErrHandler

0
 
LVL 32

Accepted Solution

by:
Erick37 earned 200 total points
ID: 2702718
Option Explicit

Private Sub Command1_Click()
'Declare variable
Dim strFilename As String
Dim strTitle As String
'Do the error handler
cdbOpenSave.CancelError = True
On Error GoTo ErrHandler

'Set the properties of the text control
cdbOpenSave.Filter = "Text Files *.txt|*.txt*"
'Set default extetion
cdbOpenSave.DefaultExt = "txt"
'Set default filename
strTitle = "c:\windows\test.txt"
cdbOpenSave.filename = strTitle
'Prompt on overwrite
cdbOpenSave.Flags = cdlOFNOverwritePrompt
'Show the save-as dialog box
cdbOpenSave.ShowSave

'Get the filename
strFilename = cdbOpenSave.filename

'save the file
Open strFilename For Output As #1
Print #1, Text1.Text
Close #1

Exit Sub
ErrHandler:

End Sub
0
 

Author Comment

by:Ginger16
ID: 2702861
Answered perfectly. Thanks.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 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