Solved

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

Posted on 2010-09-17
7
739 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
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 500 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 33

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now