jasminlois jasminlois -4 years ago 149
SQL Question

How do you save the contents of a textbox as an .sql file using Excel-VBA?

I have created an SQL Script Generator where in when I input data in comboboxes, a script will be generated in the Textbox. (See sample picture below.)

SQL Script Generator

What I want to happen is upon clicking a button, I will be prompted for a file name and location for saving, and then it will take the value of the textbox and it will be saved as an .sql File. How can I do this?

Answer Source

Use Application.GetSaveAsFilename and then write text to file as per code sample

Dim FileName As String
FileName = Application.GetSaveAsFilename(, "*.sql,*.sql", , "Save SQL File")
Open FileName For Output As #1
Print #1, "SQL Query here"
Close #1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download