Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Export table to SQL

I want to write some VBA code to export a worksheet in excel to a table in
SQL. Is that possible? Thanks a lot!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Export table to SQL

Sure it is possible. You need to understand ADO, SQL (enough to create a
query) and have the ODBC drivers.

I can give you some code that I Use to update a table in Access. The only
difference is the Connection...

If you want the code let me know...

"winter" wrote:

I want to write some VBA code to export a worksheet in excel to a table in
SQL. Is that possible? Thanks a lot!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Export table to SQL

Thanks a lot, I almost thought it was possible. I know how to run a query in
sql and return the results to excel, but don't know the opposite. If you
could show me, it will be great.

"Jim Thomlinson" wrote:

Sure it is possible. You need to understand ADO, SQL (enough to create a
query) and have the ODBC drivers.

I can give you some code that I Use to update a table in Access. The only
difference is the Connection...

If you want the code let me know...

"winter" wrote:

I want to write some VBA code to export a worksheet in excel to a table in
SQL. Is that possible? Thanks a lot!

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default Export table to SQL


winter wrote:
You need to understand ADO, SQL (enough to create a
query) and have the ODBC drivers.


I know how to run a query in
sql and return the results to excel, but don't know the opposite.


Are you using 'SQL' as a contraction for Microsoft SQL Server?

I think by mentioning ODBC drivers, Jim probably meant something like
this:

Sub just_four_lines()
Dim con As Object
Set con = CreateObject("ADODB.Connection")
con.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\MyWorkbook.xls;" & _
"Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"
con.Execute _
"INSERT INTO [ODBC;Driver={SQL Server};" & _
"SERVER=MYSERVER;DATABASE=MyDatabase;" & _
"UID=;Pwd=;].MyTable (data_col)" & _
" SELECT MyMemoCol AS data_col FROM [MySheet$];"
End Sub

However, it can be done with OLE DB alone e.g.

Sub four_lines_more()
Dim con As Object
Set con = CreateObject("ADODB.Connection")
con.Open _
"Provider=SQLOLEDB.1;Data Source=MYSERVER;" & _
"Initial Catalog=MyDatabase;User Id=;password=;"
con.Execute _
"INSERT INTO MyTable (data_col) " & _
" SELECT data_col FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0'," & _
"'Excel 8.0;HDR=YES;IMEX=1;Database=C:\MyWorkbook.xls'," & _
"'SELECT MyMemoCol AS data_col FROM [MySheet$]');"
End Sub

Jamie.

--

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export table from AutoCAD into Excel ! [email protected] Excel Discussion (Misc queries) 2 May 15th 23 03:42 AM
Export Pivot Table Data to Excel Table Milind Keer[_2_] Excel Discussion (Misc queries) 0 October 8th 08 04:53 PM
Is there any way to export a .xls table to Access .mdb without Acc Mr. Analogy Excel Discussion (Misc queries) 0 February 1st 07 12:05 AM
Export data to SQL table Mark Excel Programming 1 April 6th 04 10:31 PM
How to export pivot table to a plain table? RADO[_3_] Excel Programming 4 December 3rd 03 09:01 AM


All times are GMT +1. The time now is 05:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"