Hi Team,

I'm stuck with my code and I found out this snippet to save(Export) an Excel file to D:. However, I would like to give a User an option to save it according to his/her necessity.

Please find the Code Snippet below:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If ComboBox1.Text = "Select a Month to Proceed" Then
                MsgBox("Bad Selection. Make a choice by Selecting a Month first")
            Else
                'Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\msv\MSVDB.accdb;Persist Security Info=True;Jet OLEDB:Database Password=hdAI##^1939")
                If cn.State = ConnectionState.Open Then
                    cn.Close()
                End If
                cn.Open()


                cmd.Connection = cn
                cmd.CommandText = "SELECT (EmpID)as [Employee ID],(Answer1) as [Answer 1],(Answer2)as [Answer 2],(Answer3) as [Answer 3],(Answer4) as [Answer 4],(Answer5) as [Answer 5],(Answer6) as [Answer 6],(Answer7) as [Answer 7],(Answer8) as [Answer 8],(Answer9) as [Answer 9],(Answer10) as [Answer 10] INTO [Excel 12.0 Xml;HDR=YES;DATABASE=D:\zzzNew.xlsx].[Sheet1] FROM " & Label2.Text & ""
                If cmd.ExecuteNonQuery() Then
                    MsgBox("File Saved Successfully. Check your D:\zzzNew.xlsx for Exported Data")
                End If
            End If
        Catch ex As Exception
            MsgBox("Bad Selection. Your file is already Exported OR You haven't selected an appropriate Month", MsgBoxStyle.Critical, "Error")
        End Try
    End Sub

You will need to add the ability for the user to specify a location before reaching that code section. A file dialog will work. They can then locate the location they want to save and, on clicking the export button, the directory location becomes a variable that is inserted into the command text.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.