Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default VBA Excel 2007 SQL query

I'm trying to query a SQL database and bring the results to a spreadsheet.
Be gentle on my, I have not coded in VBA for many years.

I'm thinking I can open a connection lke this
Set MyConnection = new ADODB.connectio0n
MyConnection.Open "Driver=SQL
Server;Server=MyServerName;Database=MyDatabase; (not sure I need a
password)

So question is how do I run a SQL query? I have only used this to add stuff
to a database before?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default VBA Excel 2007 SQL query

Turn on the macro recorder, then tab to DATA and set up your connection
'From other sources', and query options. Turn off recorder and you will have
a skeleton code that you can adapt for general use.

--

Regards,
Nigel




"Striker" wrote in message
...
I'm trying to query a SQL database and bring the results to a spreadsheet.
Be gentle on my, I have not coded in VBA for many years.

I'm thinking I can open a connection lke this
Set MyConnection = new ADODB.connectio0n
MyConnection.Open "Driver=SQL
Server;Server=MyServerName;Database=MyDatabase; (not sure I need a
password)

So question is how do I run a SQL query? I have only used this to add
stuff to a database before?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default VBA Excel 2007 SQL query

I can see the fframework, but not sure where to put a query in there?


"Nigel" wrote in message
...
Turn on the macro recorder, then tab to DATA and set up your connection
'From other sources', and query options. Turn off recorder and you will
have a skeleton code that you can adapt for general use.

--

Regards,
Nigel




"Striker" wrote in message
...
I'm trying to query a SQL database and bring the results to a
spreadsheet. Be gentle on my, I have not coded in VBA for many years.

I'm thinking I can open a connection lke this
Set MyConnection = new ADODB.connectio0n
MyConnection.Open "Driver=SQL
Server;Server=MyServerName;Database=MyDatabase; (not sure I need a
password)

So question is how do I run a SQL query? I have only used this to add
stuff to a database before?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default VBA Excel 2007 SQL query

On Tue, 3 Mar 2009 07:08:25 -0700, "Striker" wrote:

I'm trying to query a SQL database and bring the results to a spreadsheet.
Be gentle on my, I have not coded in VBA for many years.

I'm thinking I can open a connection lke this
Set MyConnection = new ADODB.connectio0n
MyConnection.Open "Driver=SQL
Server;Server=MyServerName;Database=MyDatabase; (not sure I need a
password)

So question is how do I run a SQL query? I have only used this to add stuff
to a database before?


Dim rs As ADODB.Recordset
Dim sSQL As String

sSQL = "SELECT * FROM MyTable WHERE ID=1"

Set rs = MyConnection.Execute(sSQL)

More info here
http://www.dailydoseofexcel.com/arch...set-basics/%5C
--
Dick Kusleika
Microsoft MVP-Excel
http://www.dailydoseofexcel.com
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default VBA Excel 2007 SQL query

VBA does not seem to like the line continuation _. If I put my query on one
line 600 spaces long it works, but the query is hard to read that way.



"Dick Kusleika" wrote in message
...
On Tue, 3 Mar 2009 07:08:25 -0700, "Striker"
wrote:

I'm trying to query a SQL database and bring the results to a spreadsheet.
Be gentle on my, I have not coded in VBA for many years.

I'm thinking I can open a connection lke this
Set MyConnection = new ADODB.connectio0n
MyConnection.Open "Driver=SQL
Server;Server=MyServerName;Database=MyDatabase ; (not sure I need a
password)

So question is how do I run a SQL query? I have only used this to add
stuff
to a database before?


Dim rs As ADODB.Recordset
Dim sSQL As String

sSQL = "SELECT * FROM MyTable WHERE ID=1"

Set rs = MyConnection.Execute(sSQL)

More info here
http://www.dailydoseofexcel.com/arch...set-basics/%5C
--
Dick Kusleika
Microsoft MVP-Excel
http://www.dailydoseofexcel.com


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
SQL query in Excel 2007 narp[_2_] Excel Discussion (Misc queries) 0 January 14th 09 04:36 PM
Excel 2007 Query Wizard problem with Access 2007 extensions Showdad Excel Discussion (Misc queries) 0 December 4th 08 05:57 PM
Web Query in Excel 2007 dhstein Excel Discussion (Misc queries) 3 October 7th 08 02:58 PM
Excel 2007 / MS Query - editing existing query to another sheet Hotpepperz Excel Discussion (Misc queries) 0 June 13th 08 06:53 PM
Query from microsoft query- Excel 2007 טבלאות אקסל 2007 Excel Discussion (Misc queries) 0 December 24th 07 10:47 PM


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