Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.office.developer.vba,microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default accessing SQL server from Excel/VBA (instead of ODBC)

I added some VBA code into an excel Spreadsheet to get some data from a SQL
server using QueryTables and ODBC (I defined a DSN, etc.).

Is there a faster method to access SQL Server (ADO)?

Note: I am a beginner in VBA.

Thanks!
Danut


  #2   Report Post  
Posted to microsoft.public.office.developer.vba,microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default accessing SQL server from Excel/VBA (instead of ODBC)

Probably not a faster way.

Here's something that might help you. It was built for Excel and Access but
is easily changed for Sql Server.

It includes examples of using variables in SQL queries.
http://www.bygsoftware.com/examples/sql.html

Or you can get there from the "Excel with Access Databases" section on page:
http://www.bygsoftware.com/examples/examples.htm

It demonstrates how to use SQL in Excel's VBA to:

* create a database,
* create a table and add data to it,
* select data from a table,
* delete a table,
* delete a database.

You can also download the demonstration file called "excelsql.zip".

The code is open and commented.


--

Regards
Andy Wiggins
www.BygSoftware.com
Home of "Save and BackUp",
"The Excel Auditor" and "Byg Tools for VBA"


"Danut" wrote in message
...
I added some VBA code into an excel Spreadsheet to get some data from a

SQL
server using QueryTables and ODBC (I defined a DSN, etc.).

Is there a faster method to access SQL Server (ADO)?

Note: I am a beginner in VBA.

Thanks!
Danut




  #3   Report Post  
Posted to microsoft.public.office.developer.vba,microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default accessing SQL server from Excel/VBA (instead of ODBC)

Thanks Ender!!! ;-)

"Andy Wiggins" <contact me @ my web site wrote in message
...
Probably not a faster way.

Here's something that might help you. It was built for Excel and Access

but
is easily changed for Sql Server.

It includes examples of using variables in SQL queries.
http://www.bygsoftware.com/examples/sql.html

Or you can get there from the "Excel with Access Databases" section on

page:
http://www.bygsoftware.com/examples/examples.htm

It demonstrates how to use SQL in Excel's VBA to:

* create a database,
* create a table and add data to it,
* select data from a table,
* delete a table,
* delete a database.

You can also download the demonstration file called "excelsql.zip".

The code is open and commented.


--

Regards
Andy Wiggins
www.BygSoftware.com
Home of "Save and BackUp",
"The Excel Auditor" and "Byg Tools for VBA"


"Danut" wrote in message
...
I added some VBA code into an excel Spreadsheet to get some data from a

SQL
server using QueryTables and ODBC (I defined a DSN, etc.).

Is there a faster method to access SQL Server (ADO)?

Note: I am a beginner in VBA.

Thanks!
Danut






  #4   Report Post  
Posted to microsoft.public.excel.programming
A J A J is offline
external usenet poster
 
Posts: 2
Default accessing SQL server from Excel/VBA (instead of ODBC)

If you are using Excel2000 or higher you can do this:

dim cn as new adodb.connection, rs as new adodb.recordset
cn.Open "Provider=SQLOLEDB;Data Source=FRKfwd03;" _
& "Initial Catalog=ORS2K;UID=SA;PWD=;"
rs.Open "tbl1", conn, adOpenDynamic, adLockPessimistic

Sheets("Sheet1").Range("A1").CopyFromRecordset

If you have Excel97 then you can still use the above but
the copyfromrecordset thing won't work with ADO (wasn't
invented yet). So you need to create a DAO recordset in
Addition to the ADODB recordset (make a reference to DAO
3.51, also need to make a reference to Microsoft ActiveX
Data Objets 2.5 or higher (MDac2.5or higher)) and copy row
by row the data from rsADo to rsDAO and then you can use
CopyFromRecordset. Way faster than ODBC even for a few
thousand records - (note: if you have more than a few
thousand records to plant into excel for sql Server, then
you need to do more crunching on the sql server side).
You should be able to transfer a few thousand records this
way (with Excel2000 or higher) in less than one second,
especially if you use the ADODB command object and get the
data from a stored procedure on Sql Server using
copyfromrecordset.

A J

-----Original Message-----
I added some VBA code into an excel Spreadsheet to get

some data from a SQL
server using QueryTables and ODBC (I defined a DSN, etc.).

Is there a faster method to access SQL Server (ADO)?

Note: I am a beginner in VBA.

Thanks!
Danut


.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default accessing SQL server from Excel/VBA (instead of ODBC)

I get "Compile error: User-defined type no defined" at

dim cn as new adodb.connection

Where is the "adodb" class defined for VBA? I looked in the Object Browser
and I did not find it.

I'm using Excel 2002 (10.4524.4219) SP-2, the one that comes with Office XP.


"A J" wrote in message
...
If you are using Excel2000 or higher you can do this:

dim cn as new adodb.connection, rs as new adodb.recordset
cn.Open "Provider=SQLOLEDB;Data Source=FRKfwd03;" _
& "Initial Catalog=ORS2K;UID=SA;PWD=;"
rs.Open "tbl1", conn, adOpenDynamic, adLockPessimistic

Sheets("Sheet1").Range("A1").CopyFromRecordset

If you have Excel97 then you can still use the above but
the copyfromrecordset thing won't work with ADO (wasn't
invented yet). So you need to create a DAO recordset in
Addition to the ADODB recordset (make a reference to DAO
3.51, also need to make a reference to Microsoft ActiveX
Data Objets 2.5 or higher (MDac2.5or higher)) and copy row
by row the data from rsADo to rsDAO and then you can use
CopyFromRecordset. Way faster than ODBC even for a few
thousand records - (note: if you have more than a few
thousand records to plant into excel for sql Server, then
you need to do more crunching on the sql server side).
You should be able to transfer a few thousand records this
way (with Excel2000 or higher) in less than one second,
especially if you use the ADODB command object and get the
data from a stored procedure on Sql Server using
copyfromrecordset.

A J

-----Original Message-----
I added some VBA code into an excel Spreadsheet to get

some data from a SQL
server using QueryTables and ODBC (I defined a DSN, etc.).

Is there a faster method to access SQL Server (ADO)?

Note: I am a beginner in VBA.

Thanks!
Danut


.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default accessing SQL server from Excel/VBA (instead of ODBC)

Thanks A.J.!
Thanks Andy!

I got it:
http://www.erlandsendata.no/english/.../exportado.php
"The macro example assumes that your VBA project has added a reference to
the ADO object library.
You can do this from within the VBE by selecting the menu Tools, References
and selecting Microsoft ActiveX Data Objects x.x Object Library.
Use ADO if you can choose between ADO and DAO for data import or export. "


Now the macro is abotu 3-4 times fatser due to this change + other changes I
have made not related to the database access.


"Danut" wrote in message
...
I get "Compile error: User-defined type no defined" at

dim cn as new adodb.connection

Where is the "adodb" class defined for VBA? I looked in the Object Browser
and I did not find it.

I'm using Excel 2002 (10.4524.4219) SP-2, the one that comes with Office

XP.


"A J" wrote in message
...
If you are using Excel2000 or higher you can do this:

dim cn as new adodb.connection, rs as new adodb.recordset
cn.Open "Provider=SQLOLEDB;Data Source=FRKfwd03;" _
& "Initial Catalog=ORS2K;UID=SA;PWD=;"
rs.Open "tbl1", conn, adOpenDynamic, adLockPessimistic

Sheets("Sheet1").Range("A1").CopyFromRecordset

If you have Excel97 then you can still use the above but
the copyfromrecordset thing won't work with ADO (wasn't
invented yet). So you need to create a DAO recordset in
Addition to the ADODB recordset (make a reference to DAO
3.51, also need to make a reference to Microsoft ActiveX
Data Objets 2.5 or higher (MDac2.5or higher)) and copy row
by row the data from rsADo to rsDAO and then you can use
CopyFromRecordset. Way faster than ODBC even for a few
thousand records - (note: if you have more than a few
thousand records to plant into excel for sql Server, then
you need to do more crunching on the sql server side).
You should be able to transfer a few thousand records this
way (with Excel2000 or higher) in less than one second,
especially if you use the ADODB command object and get the
data from a stored procedure on Sql Server using
copyfromrecordset.

A J

-----Original Message-----
I added some VBA code into an excel Spreadsheet to get

some data from a SQL
server using QueryTables and ODBC (I defined a DSN, etc.).

Is there a faster method to access SQL Server (ADO)?

Note: I am a beginner in VBA.

Thanks!
Danut


.





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
64 bit ODBC driver for Excel (Server 2008) NicAC Excel Discussion (Misc queries) 0 October 28th 09 11:34 AM
Changing ODBC server (MySQL) Groj Excel Worksheet Functions 0 November 23rd 07 05:09 AM
How easy is it to change the server odbc details for excel Bryan Hepworth Excel Discussion (Misc queries) 0 September 26th 06 11:45 PM
Multiple people accessing an Excel file from a server at once. UABCSA Excel Discussion (Misc queries) 1 May 2nd 05 05:41 PM
SQL Server ODBC Error Art Saffran Excel Discussion (Misc queries) 0 March 24th 05 02:37 PM


All times are GMT +1. The time now is 01:44 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"