VBA to export large tables from Excel to SQL Server

I have spreadsheets that I distribute to users.  I want Excel VBA to export 
large tables (2000+ rows) into a remote SQL Server 2005.  I am able to 
successfully do this with the following generic code, but it is too slow:

	Dim con As ADODB.Connection
	Set con = New ADODB.Connection
	con.Open "Driver={SQL 
	con.Execute "INSERT INTO ... SELECT * FROM ..."      'Looping this for each 
It takes over 10 minutes because it does it one line at a time.  

I've also tried looping  "ADODB.Recordset.addnew" ending with 
".UpdateBatch", but it also is too slow, seems to still only be able to 
transfer the table data one row at a time (unless I'm doing something wrong). 

Is it really true that VBA/ADO can only export one row at a time?  
Is there some other way to improve performance (send the whole data range at 
one time)?  For example, send the entire table to a SQL Server stored 
procedure or BCP (whatever that is).  If so, how would it work?  Is there a 
way to write the stored procedure generic enough to accept a variety of 
tables, for example by receiving parameters indicating what SQL table they go 
into and if it replaces or appends existing data?

Thank You!
4/26/2010 5:14:04 PM
excel.programming 6508 articles. 2 followers. Follow

1 Replies

Similar Articles

[PageSpeed] 33

I've uploaded a pretty large database with the following code and it
didn't take a long time.  See macro below

You could export your spreadshet as CSV and then import the data into
the SQL server in CSV format.

Sub Submit()
'filename of database is with MakeDatabase macro

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

Set sht = ThisWorkbook.Sheets("USA")

strdb = Folder & FName

If Dir(strdb) = "" Then
MsgBox ("Database Doesn't Exists, Create Database" & strdb)
MsgBox ("Exiting Macro")
Exit Sub
End If

ConnectStr = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Folder & FName & ";" & _
"Mode=Share Deny None;"

cn.Open (ConnectStr)
With rs
.Open Source:="USA", _
ActiveConnection:=cn, _
CursorType:=adOpenDynamic, _
LockType:=adLockOptimistic, _

If .EOF <> True Then
End If
End With

LastCol = sht.Cells(1, Columns.Count).End(xlToLeft).Column

LastRow = sht.Range("A" & Rows.Count).End(xlUp).Row
For RowCount = 2 To LastRow
With rs
!ID = sht.Cells(RowCount, "A")
For ColCount = 2 To LastCol
If Data <> "" Then
ColName = sht.Cells(1, ColCount)

rs(ColName) = sht.Cells(RowCount, ColCount)
End If
Next ColCount
End With
Next RowCount

Set appAccess = Nothing
End Sub

joel's Profile: http://www.thecodecage.com/forumz/member.php?u=229
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=198561


4/26/2010 9:28:56 PM

Similar Artilces:

Is it possible to add a base line in excel
I have a graph the tracks test scores. I want to establish in a bar chart a base line for acceptable test scores. I can do this by drawing a line on the chart but I want to be able to include this in the chart itself. Hi see: http://andypope.info/charts/averageline.htm http://www.tushar-mehta.com/excel/charts/straight_lines/index.html http://peltiertech.com/Excel/Charts/DummySeries.html#AddLine -- Regards Frank Kabel Frankfurt, Germany Tom King wrote: > I have a graph the tracks test scores. I want to establish in a bar > chart a base line for acceptable test scores. I can do th...

Importing Data into an Excel Pivot Table via Access
I have set up a query in Microsoft Access which is linked to our AS400 server. I have created pararmeters within Access which asks for certain fields which works. I then go into Excel and create a pivot table with the external data source that I have created in access. When I go to enter a pararmeter within Microsof Query I get a reply saying that "Parameters can not be used with this Query", what I want to do is setup a parameter on the Excel spreadsheet which then goes and gets the data i require from this parameter. I would be very grateful if someone could help me with thi...

VBA from another app: Suppressing Excel confirmation dialog?
After creating/formatting several worksheets from MS Access, I'd like to delete the "Sheetn" worksheets that got put there when I did a .WorkBooks.Add. I avoided using them because I'm not sure how/why they are created - i.e. maybe some user's defaults would only create 1 empty sheet or none. So, form MS Access's VBA I'd like to do: On Error Resume Next .Worksheets("Sheet1").Delete .Worksheets("Sheet2").Delete .Worksheets("Sheet3").Delete .Worksheets("Sheet4").Delete On Erro...

how do I add times in Excel and result in hours & mins
I want to insert a time when I start work and a time when I take a break, then a time when I leave work. Following that I want to be able to add up the amount of hours that I have worked. This will enable me to plan my week ahead and ensure I only allocate a specific amount of time to a project. http://www.cpearson.com/excel/datetime.htm#WorkHours -- Kind Regards, Niek Otten Microsoft MVP - Excel "Rty Shaw" <Rty Shaw@discussions.microsoft.com> wrote in message news:37D03D72-5525-4D6E-8ED7-2911B16248B0@microsoft.com... >I want to insert a time when I start work and...

Illegal operation error while printing EXCEL or WORD Files
Hi, I am facing an illegal operation error when i try to print any file from excel (any no. of pages), this happens in stand alone printer as well as a networked printer. When we press the print button, it flashes this message, but still prints, but once the printing is completed, i will have to restart the PC. Due to this error other applications PRINTING also will NOT HAPPEN and the only way out is, restart the PC. This happens not only in EXCEL, it happens in all the MS applications (outlook, access, front page, powerpoint also). When I check the print manager (before restart),...

Opening a new instance of Excel
I am using multiple monitors for work and it is great! Is there a setting that I can use so that it opens each new excel file in a new excel window so I can drag different ones to each monitor? Is there a similar setting for Word? I am using Excel 2002 and Word 2002. Thank you. Hi, Yes, you can check the Windows in Taskbar checkbox in Tools; Options. This is on the View tab for both Word and Excel. >-----Original Message----- >I am using multiple monitors for work and it is great! Is >there a setting that I can use so that it opens each new >excel file in a new excel ...

Emailing in excel 2003 02-26-10
If i type in the cell A34: neil.Holden@test.com and press a button is it possible to email to the address of what ever is in A34 is? The email body should say: this has been submitted for cell B34 and todays date. Thanks. Check out Ron De Bruins "Send-Mail" tips: http://www.rondebruin.nl/sendmail.htm Micky "Neil Holden" wrote: > If i type in the cell A34: neil.Holden@test.com and press a button is it > possible to email to the address of what ever is in A34 is? > > The email body should say: this has been submitted for cell B34 and...

Excel Crash
I use Excel and Word 2003 using Windows NT. I've kept some files on a jump drive so I can work on them at home. I attempted to work on a Word documents which had an Excel worksheet inserted in it. I tried double clicking on the worksheet to edit it and Word and Excel shut down. Now when I attempt to open Excel at home it asks for my Office XP Professional installation cd. (I have Office XP at home with Windows XP). I'm having a hard time locating my original discs. Does anyone have any suggestions or experience anything like this? ...

Does anyone have a dashboard gauge (speedometer style) for Excel?
I am trying to create dashboard charts from Excel data and would love other templates not available in Excel today - speedometer charts, multi-dimension comparitive charts, charts that build information overlays. I regularly create these in a manual way for executive and customer summaries but would appreciate the ability to automatically generate these types of charts allowing for real time viewing of "what if" scenarios. Steve, there are tons of these things out there to review, few better than this collection: http://www.andypope.info/charts.htm Andy Pope has put together...

Moving 5.5 server from one domain to another
Ex 5.5, sp4 on Win2000 sp4, Mcafee VScan8, Veritas 8.6 currently in NT4 domain. Have exactly the same config in another office except this Ex 5.5 server is the bridge end. Have gone through the process of changing service accounts and that is working fine and machine is using the new service accounts to run the services, (ie using the new service account in the Windows 2003 domain), when I drop out of "abc" domain and into "xyz" domain machine comes up fine, all services start but after about 60 secs the information store stops, if I try to start it manually I get er...

Excel Edit F2 button changed for Mac???
Switched to Microsofts version of Excel for Mac. Can anyone tell me what keystroke allows me to edit a cell? Before I switched to a Mac it was the F2 button. Please help. Thank you. See the answers in the m.p.mac.office.excel newsgroup. In article <1176582208.958694.269620@q75g2000hsh.googlegroups.com>, ssears@indy.tds.net wrote: > Switched to Microsofts version of Excel for Mac. Can anyone tell me > what keystroke allows me to edit a cell? Before I switched to a Mac > it was the F2 button. Please help. Thank you. ...

How do I create a pivot table if the pivot table icon or menu ite.
If rhe pivot table icon ...??? Please clarify in the body of the message. "Lynn@WS" <Lynn@WS@discussions.microsoft.com> wrote in message news:0E6B098C-551A-4389-9048-7F4F5A6E5EF8@microsoft.com... > ...

I have an excel file (2000 format), that after I made a number of changes is causing me problems when I re-open the file. Windows task manager goes to 100% CPU activity, and i cant do anything within the excel file. However, if I set recalculation to manual before I open the file, all seems fine. Obvioulsy I have a problem. But how do i find that problem ? Thanks in advance. I have had some experience running large spreadsheets lately. Above a certain size, the recalculation time seems to climb very fast. While Excel is recalculating, you can't do anything anyway. Best in my v...

Excel Cells Contents to HTML Pages
Hi, I have an excel sheet which has two columns. Column 1: PageName Column 2: HTMLCode PageName has information like: OurServices.htm which is the name of th HTML page. HTMLCode clumn has actual HTML Code like: <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta name="GENERATOR" content="Microsoft FrontPage 5.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <meta http-equiv="Content-Type" content="text/html charset=windows-1252"> ...

In Excel program shows a broken line and don't know how to get ri.
I am trying to put in entries in Excel program and when I go to Print Preview, it only shows part of the entries I put in. When I go to the last entry that it shows in Print Preview, there is a broken line between it and the next entry. I don't know what I did. I could use some help You probably have multiple areas selected for your print area. you can check this by <view><page break preview> One possibility to correct this would be <file><print area><clear print area> or specifically select the area you want printed and <file ><Print area>...

how to divide a numbers in a table of data by 1,000, all at once
I've noticed in some spreadsheets that numbers are displayed as rounded...i.e. so that one isn't looking at 1,403,333 + 2,333,999 etc. but rather 1403 + 2334, etc. with a note at the top that says "000's". In one step, how can I edit all the numbers in a table so that they show to the nearest 10,000 (i.e. 1403 +2334), without dividing them one by one by 1,000? Or, to go at it from another angle, how do I divide each # by 1,000? The ROUND feature requires that I create a new table. Not exactly what I'm looking for. Enter 1000 in an unused cell. ...

Exchange Server 2003 Fails to Start after reboot
Since rebooting our Exchange 2003 server running on Windows 2003 server it fails to start the System Attendant and any further exchange services. The only way to logon to the server is boot in safe mode with networking. The server is a member server and is reporting the following errors EVENT 2114 MSExchange DS Access Process INETINFO.EXE (PID=1720). Topology Discovery failed, error 0x80040a02. EVENT 2102 Process MAD.EXE (PID=2176). All Domain Controller Servers in use are not responding: crstgs003.CRSTG.LOCAL crstgs004.CRSTG.LOCAL EVENT 2114 Process MAD.EXE (PID=2176). Topology Discovery ...

connect a form to excel database
I have a database in excel which lists our stock and prices etc. I would like to be able to enter a stock number on an invoice that will retrieve the details and price from my excel database. Can this be done? Excel has built in option--try data|form. If that doesn't quite work, but it's close, John Walkenbach has an enhanced dataform at: http://j-walk.com/ss/dataform/index.htm And if you want to design your own, Debra Dalgleish has a get started with userforms: http://contextures.com/xlUserForm01.html Kathy wrote: > > I have a database in excel which lists our stock and p...

Does anyone know where I can download EXCEL.MSI?
I am missing my Excel 2002 CD, and can't install Office Update Service Pack 3 without this file. ...

Excel ignores boot-time regional settings when interpreting a date
I have a third party DDE app that exports dates as strings, correctly using the short date format in the regional and language settings, which, in my case is MM/DD/YYYY (reset at every boot). Unfortunately excel insists in interpreting that date as DD/MM/YYYY [Application.International(xlDateOrder)=1, it should be 0], disregarding my regional settings. The result is that all the dates are wrong or #VALUES! If this is not wierd enough, listen to this: it only happens after I reboot (when the date format is reset to MM/DD/YYYY). If subsequently I change the short date format in the regional se...

Excel to check for regular expression?
Hi, How do you use Regular expression in excel? For eg: Check a column of data to see whether are all of them valid email addresses? Please advise THanks By valid, do you mean that the email is in proper syntax or that it is an actual email address. Text formulas can be used for proper syntax. Perhaps a Visual Basic proceedure. I doubt you can check that the email address actually exists, as many companies and ISPs will simply disregard and drop mail to invalid email addresses to eliminate spam "user" wrote: > Hi, > > How do you use Regular expression ...

Exporting-importing email accout (.iaf) doesn't work
Hi, i have an .iaf file that i get exporting my email account at work. Now i have imported that file at home, but when i click on "Send and recieve" i get this message: [quote title=3DQuote:] Fall=F3 la conexi=F3n con el servidor. Cuenta: 'pop3.xxx.es', Servidor: 'pop3.xxx.es', Protocolo: POP3, Puerto: 110, Seguridad (SSL): No, Error de socket: 10060, N=FAmero de error: 0x800CCC0E[/quote] I'm using OL Express 6 in both computers (Windows XP). Any idea? Bye Javi Disable email scanning by your anti-virus application. It provides no addit...

opening Excel and then closing it
I have a file that is passed to me in XLS format, but I would much prefer to work with it in CSV. So I did this... ' open the downloaded file in Excel and copy it over to L in csv format Workbooks.Open "O:\Downloads\D00100001554_0.xls" Workbooks("D00100001554_0.xls").ConflictResolution = xlLocalSessionChanges Workbooks("D00100001554_0.xls").SaveAs "L:\Activity.csv", xlCSV Workbooks("Activity.csv").Close False This works fine except it leaves Excel open in the background. The only thing to do is fine it in Processes a...

Problems getting to server?
I have a customer who is saying that if he tries to get to a email server I am running, he can't get to it on the 1st attempt using Outlook Express. He has to browse to a web page that I am hosting, view that page. Then open OE, and his email will work fine. Has anyone heard of this, and have any solutions? Thanks, Steve Ask in an Outlook Express forum. This is an Microsoft Office Outlook forum. Outlook Express is family of Internet Explorer and Outlook of the Office family. Here is the link for the right forum http://communities.microsoft.com/newsgroups/default.asp?icp=Internet...

filling a field in a continuous form with a field from a table
I have a form with the fields: date, client, atty, time, billable and description. All these fields update the Time Sheet Table. I want the atty field to automatically take the attorney's name from the table Attorney which contains one field called Atty. So for I can only figure out how to use a combo box to chose the Attorney table field. You could set the default value of the control using DLookup(). =DLookup("Atty","tblAttorney") This could be a locked combo box that displays whatever value you want or can derive from tblAttorney. -- Duane H...