+ Reply to Thread
Results 1 to 6 of 6

Insert an Excel range to SQL Server using ADO (push from Excel)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-12-2007
    Posts
    144

    Insert an Excel range to SQL Server using ADO (push from Excel)

    Hi,
    I have a macro that uses ADO to insert data to my SQL Server.
    It works well inserting one row at a time, but I really want to insert a whole range at once.

    I believe this is possible but it doesn't like the way I am writing the range which I got from the Microsoft website.
    What am I missing here?

    The error says "Invalid object name 'Forecast$A2:AS10'.

    Sub Insert2()
    
    Dim con As New ADODB.Connection, rs As New ADODB.Recordset, strPath As String, row1 As Integer, server As String, table As String, database As String
         
    server = "BKBAS\SQLEXPRESS"
    table = "dbo.ahr1304_FORECAST"
    database = "SimpleSQL"
                 
    con.Open "Provider=SQLOLEDB;Data Source=" & server & ";Initial Catalog=" & database & ";Integrated Security=SSPI;"
    Set rs.ActiveConnection = con
    
     'this works
     con.Execute "INSERT INTO " & table & "  VALUES ('" & Sheets("Forecast").Cells(2, "A") & "', '" & Sheets("Forecast").Cells(2, "B") & "')"
     
     'this doesn't work
     con.Execute "INSERT INTO " & table & " SELECT * FROM [Forecast$A2:AS10]"
    
     con.Close
     Set con = Nothing
         
     MsgBox "Import Complete!", vbInformation
    
    End Sub
    Last edited by treva26; 08-05-2014 at 12:44 AM.

  2. #2
    Forum Contributor
    Join Date
    06-12-2007
    Posts
    144

    Re: Insert an Excel range to SQL Server using ADO

    Is it that I have set my SQL Server as an ADO connection but not Excel??

    Or can I read the range into an array and put the array as the data source in the INSERT statement?

  3. #3
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Insert an Excel range to SQL Server using ADO

    I think you need a dollar sign after the sheet name.

    3 Export data from Excel to existing SQL Server table

    Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;Database=D:\testing.xls;HDR=YES',
    'SELECT * FROM [SheetName$]')
    There seems to be a lot on the net about it.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  4. #4
    Forum Contributor
    Join Date
    06-12-2007
    Posts
    144

    Re: Insert an Excel range to SQL Server using ADO

    I do have a dollar sign after the sheet name:

    [Forecast$A2:AS10]
    I think the issue is that form of code is for running on the SQL server and referencing an Excel sheet.

    I am running vba code in Excel. So I probably need something quite different.
    An array object or something?
    Last edited by treva26; 08-04-2014 at 11:15 PM.

  5. #5
    Forum Contributor
    Join Date
    06-12-2007
    Posts
    144

    Re: Insert an Excel range to SQL Server using ADO

    Well I am just going to do it by inserting rows of data in a loop, not an entire range.

    Although I have made a loop to insert 10 rows with each INSERT INTO statement.
    INSERT INTO table VALUES (row1value1,row1value2),(row2value1,row2value2),(row3value1,row3value2)
    This enters my 3,000 rows of data in 2 seconds rather than 5 seconds doing each row separately.
    Interestingly inserting 100 rows at a time takes 10 seconds!
    For whatever reason 10 at a time seems to work fastest and most consistently.

    My somewhat inelegant final code:
    Sub InsertIn10s()
    'Under Tools > References, set a reference to 'Microsoft ActiveX Data Objects 2.8 Library'.
    
    Dim con As New ADODB.Connection, rs As New ADODB.Recordset, row1 As Integer, cols1 As Integer, shtName As String, server As String, table As String, database As String
         
    server = "BKBAS-DATA\SQLEXPRESS"
    table = "dbo.ahr1304_FORECAST"
    database = "SimpleSQL"
    shtName = "Forecast"
    row1 = 2   'set first row with records to import
    cols1 = 45 'number of columns to import
                 
       'make the connection to the SQL Server
       con.Open "Provider=SQLOLEDB;Data Source=" & server & ";Initial Catalog=" & database & ";Integrated Security=SSPI;"
       Set rs.ActiveConnection = con
                
       con.Execute "DELETE FROM " & table 'delete all records first
    
       Do Until Sheets(shtName).Cells(row1, 1) = ""       'process all rows until you get to a blank one
            
            SQL1 = "INSERT INTO " & table & " VALUES ("   'create the SQL statement
            
            For x = 1 To 10                               'add 10 rows
             If Sheets(shtName).Cells(row1, 1) <> "" Then 'except blank ones
            
               For c = 1 To cols1                         'add all of the columns
                  SQL1 = SQL1 & "'" & Sheets(shtName).Cells(row1, c) & "', "
               Next c
               SQL1 = Left(SQL1, Len(SQL1) - 2) & "), ("  'remove the last comma and add brackets
               row1 = row1 + 1                            'increase row counter
            
             End If
            Next x
            
            SQL1 = Left(SQL1, Len(SQL1) - 3) 'remove the last comma and open bracket
            con.Execute SQL1                 'insert row into database
            
       Loop
                
         con.Close         'close the connection
         Set con = Nothing 'release the memory
         
         MsgBox "Import Complete!", vbInformation
    
    End Sub

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Insert an Excel range to SQL Server using ADO (push from Excel)

    To specify a worksheet as your recordsource, use the worksheet name followed by a dollar sign and surrounded by square brackets. For example:

    strQuery = "SELECT * FROM [Sheet1$]"
    You can also delimit the worksheet name with the slanted single quote character (`) found on the keyboard under the tilde (~). For example:

    strQuery = "SELECT * FROM `Sheet1$`"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Populate Server Function based on Server Name for Server Inventory
    By mvcp007 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-13-2013, 11:29 AM
  2. Replies: 1
    Last Post: 04-26-2007, 10:36 PM
  3. Replies: 1
    Last Post: 04-26-2007, 10:24 PM
  4. [SOLVED] Excel 2003 Error Cannot Locate the Internet Server or Proxy Server
    By Seabee in forum Excel General
    Replies: 0
    Last Post: 11-19-2005, 08:10 PM
  5. Replies: 1
    Last Post: 07-16-2005, 08:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1