tags:

views:

31

answers:

1

I'm creating an SQL file using Application.Path & SQL_statements.After that I write SQL statements into this file through vba.Now I want to save the file after all SQL statements are generated in an different directory.This directory depends upon the user.Hence I want to give him an Save as Dialog .

Thus, I need to save the file that I created through vba, and save it using save as dialog in a location specified by the user and delete the copy of file created by me.

+2  A: 

I think you want Application.GetSaveAsFilename. Here's an example.

Sub Test()

    Dim sFile As String
    Dim lFile As Long
    Dim sSql As String

    sFile = Application.GetSaveAsFilename(, "*.txt,*.txt", , "Save SQL File")

    If sFile <> "False" Then
        sSql = "Select * from abc"
        lFile = FreeFile
        Open sFile For Append As lFile
        Print #lFile, sSql
        Close lFile
    End If

End Sub

sFile will be a string that is the full path and name. I don't know what a "SQL file" is, so I guessed it was a file with a sql extension. sFile will be "False" if the user clicks Cancel on the dialog.

To copy the file after it's been created

Sub TestCopy()

    Dim sNewFile As String, sOldFile As String
    Dim lFile As Long
    Dim sSql As String

    sNewFile = Application.GetSaveAsFilename(, "*.txt,*.txt", , "Save SQL File")

    If sNewFile <> "False" Then
        sSql = "Select * from abc"
        sOldFile = "C:\Book1.txt"
        lFile = FreeFile
        Open sOldFile For Append As lFile
        Print #lFile, sSql
        Close lFile
        FileCopy sOldFile, sNewFile
    End If

End Sub

You'll end up with two files - one at C:\Book1.txt and one where the user chooses.

Dick Kusleika
How to save my file.I'm a newbie to vba.Don't know a lot of things in it which might be ordinary
gizgok
What kind of file is it? Excel file? Text file? How are you creating it?
Dick Kusleika
It's textfile.I'm creating it like this.
gizgok
gizgok
after this I want to save this file through a dialog
gizgok
I've edited the code to show one way to do it. See if that does what you need.
Dick Kusleika
Ur code works fine.Just a sidetrack question.What if I already created my file and now want to copy this file where the user wants.
gizgok