Tuesday, May 22, 2018

Excel data load in SQL Server table using OLEDB ,VBA

Sub test()

Dim cn As ADODB.Connection
    Dim strSQL As String
    Dim lngRecsAff As Long
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & Application.ActiveWorkbook.FullName & ";" & _
        "Extended Properties=Excel 8.0"
 
    'Import by using Jet Provider.
           
    strSQL = "" & _
    "" & _
    " Insert into [odbc;Driver={SQL Server};" & _
             "Server=SEVERSQL1;Database=Mydb;" & _
             "UID=budget;PWD=Password].[ForecastUpdate1111] " & _
             "SELECT * FROM [UpdateData$]"
           
           
'    Debug.Print strSQL
    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
    'debug.print "Records affected: " & lngRecsAff
     
    cn.Close
    Set cn = Nothing


End Sub

No comments: