?
Solved

Insert records into an sql database with vba macro from excel range

Posted on 2010-09-17
7
Medium Priority
?
755 Views
Last Modified: 2012-05-10
I have a excel spreadsheet that needs to be regularly imported in an sql database.

I would like the user to be able to run a personal macro to update this data into the database then I have some other macros to run.

I can loop through each on the lines and inset the records one by one but this takes quite a while - I would like to be able to insert a range from the spreadsheet into a table in an SQL database in one process. Is this possible.



0
Comment
Question by:donhannam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33706002
Should be doable but what version excel and what database are you using?

code would be something along these lines

create a database connection (can use ado or dao)
iterate thru your rows
create the sql statement to insert
run this sql statement
next row until no more
close database
0
 

Author Comment

by:donhannam
ID: 33706015
Thanks.

I am using excel 2003 and MSL database 2005.

I have tried to iterate through rows one at a time but a lot of info and takes a very long time.

Looking for a quicker way to import a range at once.
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 2000 total points
ID: 33706022
Use this SQL syntax for speedy bulk inserts:

INSERT INTO TableName(FieldName1,FieldName2)
UNION ALL
SELECT data, data
UNION ALL
SELECT data, data
...
GO

Kevin
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Expert Comment

by:rockiroads
ID: 33706043
found an easier way which might be helpful to you. let me know if this works else I will come up with the way I described


http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_23958162.html
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33706049
the adodb parameters should match the column you want to insert to in terms of datatype as well
0
 
LVL 34

Expert Comment

by:Norie
ID: 33709561
You shouldn't need to loop through each 'record' in Excel to append the data to a table in the database.

You should be able to write code that would treat the data in Excel in much the same way as a table in the database.

Then with the correct SQL you should be able to append the data in one go, just like you would in the database.

Here's some sample code which runs from VBA.

Please note this is pretty incomplete - I've even marked the file 'not perfect but it seems to be going somewhere.'.

Oh, forgot to mention this is for Excel to SQL Server.
    Dim cn As ADODB.Connection
    Dim strSQL As String
    Dim lngRecsAff As Long
    Set cn = New ADODB.Connection
    cn.Open "Provider=SQLOLEDB;Data Source=NORIE-TOSH;" & _
        "Initial Catalog=StaffLevel;Trusted_Connection=yes"

    'Import by using OPENDATASOURCE.
    strSQL = "SELECT * INTO XLImport1 FROM " & _
        "OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', " & _
        "'Data Source=C:\Users\Norie\Documents\Excel Stuff\Mock Data\AccountNos.xls;" & _
        "Extended Properties=Excel 8.0')...[tblAccounts$]"
    Debug.Print strSQL
    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
    Debug.Print "Records affected: " & lngRecsAff

    'Import by using OPENROWSET and object name.
    strSQL = "SELECT * INTO XLImport2 FROM " & _
        "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _
        "'Excel 8.0;Database=C:\Users\Norie\Documents\Excel Stuff\Mock Data\AccountNos.xls', " & _
        "[TblAccounts$])"
    Debug.Print strSQL
    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
    Debug.Print "Records affected: " & lngRecsAff

    'Import by using OPENROWSET and SELECT query.
    strSQL = "SELECT * INTO XLImport3 FROM " & _
        "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _
        "'Excel 8.0;Database=C:\Users\Norie\Documents\Excel Stuff\Mock Data\AccountNos.xls, " & _
        "'SELECT * FROM [tblAccounts$]')"
    Debug.Print strSQL
    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
    Debug.Print "Records affected: " & lngRecsAff

    cn.Close
    Set cn = Nothing

Open in new window

0
 

Author Closing Comment

by:donhannam
ID: 33729859
Thanks for all the comments - Solution by zorvek was real quick and easy to do.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question