LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Running a SQL query off an excel table of data

Is this what you are looking for? Not sure if I got the columns correct.

Sub test()

Dim vConnection As New ADODB.Connection
Dim rsPubs As ADODB.Recordset
Dim db As String

Worksheets("All pipeline") _
.Range("b7:iv65536").ClearContents


Set rsPubs = New ADODB.Recordset
Set vConnection = New ADODB.Connection
db = "data source=J:\Pipeline.mdb;"

vConnection.ConnectionString = _
db & "Provider=Microsoft.Jet.OLEDB.4.0;"
vConnection.Open

With Sheets("Sheet1")
First = True
RowCount = 1
Do While .Range("B" & RowCount) < ""

MyItem = .Range("B" & RowCount)

Sql1 = "SELECT x "
Sql2 = "FROM y "
Sql3 = "WHERE " & MyItem & "; "

Sql = Sql1 + Sql2 + Sql3

Debug.Print (Sql)
' vRecordSet.Open sql
With rsPubs
' Assign the Connection object.
.ActiveConnection = vConnection
' Extract the required records.
.Open Sql
With Worksheets("All pipeline")
If First = True Then
NewRow = 7
First = False
Else
LastRow = _
.Range("B" & Rows.Count).End(xlUp).Row
NewRow = LastRow + 1
End If
.Range("B" & NewRow).CopyFromRecordset rsPubs
End With

.Close

End With
RowCount = RowCount + 1
Loop
End With
vConnection.Close
Set rsPubs = Nothing
Set vConnection = Nothing
End Sub


"macroapa" wrote:

Hi, I have the vba code below to run a sql query off an Access
database.... what I would like to do is change this code so that it
can run off a table of data held in an excel file. Is this possible?

Essentially, due to our fantastic network, I need to make one call to
the database to get the full pipeline data and then run 4 SQL queries
off the retrieved data. Each call to the data base takes about 5mins
(due to network performance), so I was hoping to do one call and then
manipulate in excel.

Thanks

Dim vConnection As New ADODB.Connection
Dim rsPubs As ADODB.Recordset
Dim db As String
Set rsPubs = New ADODB.Recordset
Set vConnection = New ADODB.Connection
db = "data source=J:\Pipeline.mdb;"

vConnection.ConnectionString = db & "Provider=Microsoft.Jet.OLEDB.
4.0;"
vConnection.Open

Sql1 = "SELECT x "
Sql2 = "FROM y "
Sql3 = "WHERE z; "

Sql = Sql1 + Sql2 + Sql3

Debug.Print (Sql)
' vRecordSet.Open sql
With rsPubs
' Assign the Connection object.
.ActiveConnection = vConnection
' Extract the required records.
.Open Sql
Worksheets("All pipeline").Range("b7:iv65536").ClearContents
Worksheets("All pipeline").Range("b7").CopyFromRecordset
rsPubs
.Close

End With

vConnection.Close
Set rsPubs = Nothing
Set vConnection = Nothing

 
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
QUERY EXCEL TABLE DATA ibo4lyf Excel Programming 1 February 26th 08 07:19 PM
Running a SQL Update Query in Excel VBA KC Excel Programming 2 July 27th 06 11:30 PM
Running Data Table using an input that triggers DDE linked data [email protected] Excel Discussion (Misc queries) 1 December 16th 04 11:56 AM
Web Query running in backg. -how to know when data retrieval compl Matt Lawson[_4_] Excel Programming 1 December 10th 04 08:34 PM
Excel 2000 Web query not returning all Table Data [email protected] Excel Programming 5 December 21st 03 04:14 AM


All times are GMT +1. The time now is 08:30 AM.

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"