Export The Data from Excel to Sequel Database
Public Con As New ADODB.Connection
'Import the Library
'Create a Database in Sequel
Sub Export_The_Data_From_Excel_To_Sequel_Database()
'Define a variable to save Data File path
Dim DataFile As String
DataFile = Application.GetOpenFilename
'Open the DataFile Workbook
Workbooks.Open (DataFile)
'Define Object for DataFile Workbook
Dim InputWkb As Workbook
Set InputWkb = ActiveWorkbook
'Define the Datasheet
Dim InputSh As Worksheet
Set InputSh = InputWkb.Sheets("DataSheet")
'Establish the connection with Sequel Database
Establish_Connection
'Create a Table
Con.Execute "Create table Sales (Item varchar (15),Quantity int, Price int, Zone Varchar(11))"
'Using for Loop to export the data from excel to Sequel Database
For R = 2 To InputSh.Range("A" & Rows.Count).End(xlUp).Row
Item = InputSh.Cells(R, 1).Value
Quantity = InputSh.Cells(R, 2).Value
Price = InputSh.Cells(R, 3).Value
Zone = InputSh.Cells(R, 4).Value
'Inserting values into table
Con.Execute "insert into Sales values ('" & Item & "', " & Quantity & ", " & Price & ", '" & Zone & "')"
Next
MsgBox ("Hi Exported the data from Excel to Sequel Data base")
Close_the_Connection
InputWkb.Close
Set InputSh = Nothing
Set InputWkb = Nothing
End Sub
Public Function Establish_Connection()
Dim ConnectionString As String
ConnectionString = "Provider=SQLOLEDB;Data Source=PAVANKUMAR-PC;Initial Catalog=ExcelToSQL;Integrated Security=SSPI"
Con.Open ConnectionString
End Function
Public Function Close_the_Connection()
Con.Close
Set Con = Nothing
End Function