LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Using SQL to query Excel Data


Antonio A. Oliv Wrote:
Yes you could "query" a SQL in Excel Sheet.

First of all you should "name" the "range" of "Excel Table" in your
SOURCE of DATA

Tip #1:
- Open a ODBC connect and use code LIKE "Teste2"

Tip #2:
- Use EXAMPLE like "Teste3"


Sub Teste2()
Dim dbs As Database, rst As Recordset, nTotReg As Double, p As Object
Set dbs = Workspaces(0).OpenDatabase("DataBase_Excel",
dbDriverNoPrompt, True, _
"ODBC;DATABASE=;UID=;PWD=;DSN=DataBase_Excel")
Sheets("Plan2").Select
Set rst = dbs.OpenRecordset("SELECT * FROM Sales")
Cells(1, 1).Select
For Each p In rst.Fields
Cells(ActiveCell.Row, ActiveCell.Column).Value = p.Name
Cells(ActiveCell.Row, ActiveCell.Column + 1).Select
Next
nTotReg = ActiveSheet.Cells(2, 1).CopyFromRecordset(rst)
dbs.Close
End Sub


Sub Teste3()
Dim dbs As Database, tdf As TableDef, rst As Recordset, i As Integer,
nTotFields As Integer, nTotReg As Double
Set dbs = Workspaces(0).CreateDatabase("C:\TEMP\NewDB2.mdb",
dbLangGeneral)
Set tdf = dbs.CreateTableDef("ExcelTable")
tdf.Connect = "Excel
5.0;DATABASE=D:\LIB's\Excel\!_Rotinas_Solicitadas\ DataBase_Excel.xls"
tdf.SourceTableName = "Sales"
dbs.TableDefs.Append tdf

Sheets("Plan3").Select
Set rst = dbs.OpenRecordset("SELECT * FROM ExcelTable")
nTotFields = rst.Fields.Count
For i = 0 To nTotFields - 1
Cells(ActiveCell.Row, i + 1).Value = rst.Fields(i).Name
Next

Cells(2, 1).Select
rst.MoveFirst
Do While Not rst.EOF
For i = 0 To nTotFields - 1
Cells(ActiveCell.Row, i + 1).Value = rst.Fields(i).Value
Next
rst.MoveNext
Cells(ActiveCell.Row + 1, 1).Select
Loop
dbs.Close
End Sub


how can i set the "name" and "range" in the "Excel table"??
can somebody assist??


--
lsy
------------------------------------------------------------------------
lsy's Profile: http://www.excelforum.com/member.php...o&userid=23917
View this thread: http://www.excelforum.com/showthread...hreadid=347252

 
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
Use MS Query to query data within the current work book Steve Kesler Excel Discussion (Misc queries) 0 August 6th 09 05:22 PM
Importing Data via Web Query - Can values be passed to query? [email protected] Excel Discussion (Misc queries) 5 May 9th 06 06:21 PM
Save data retreived from query without saving query Anthony Excel Discussion (Misc queries) 0 January 25th 06 07:17 PM
How to Process Query Data after Query is Refreshed rajpuneyani Excel Programming 2 July 30th 04 02:13 PM
Query returning more data than will fit on worksheet with VBA DB query... ChrisSmith Excel Programming 0 June 8th 04 12:07 PM


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