LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Junior Member
 
Posts: 29
Default Import multiple Access Queries

Hi, I have some vba code that pulls in a MS Access db query. It works well, but my question is how can I alter the code to allow for multiple queries to be imported from the same macro? Ideally I'd like to have another couple of queries (from the same db) pulled into separate worksheets but WHERE statement will always be the same (i.e. SL ='" & Sheets("Start").Range("K41").Value & "'")

Here's the code:
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim i As Long
Dim ShDest As Worksheet
Dim sSQL As String

Set ShDest = Sheets("FTEGet")

sSQL = "SELECT * FROM qryFTE WHERE SL ='" & Sheets("Start").Range("K41").Value & "'"

Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB

With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With

Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, _
Options:=adCmdText

'clear existing data on the sheet
ShDest.Activate
Range("A1").CurrentRegion.Clear

'transfer data to Excel
Range("A1").CopyFromRecordset rst

'Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing

Thanks in advance. Cheers.
 
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
automatically import multiple named-ranges from Excel to Access [email protected] Excel Discussion (Misc queries) 2 December 20th 08 02:58 AM
Importing data from multiple Access queries JoeA2006 Excel Programming 1 August 29th 06 02:17 PM
How to import multiple sheet of excel into Access database to a diffrent tables?? baka Excel Discussion (Misc queries) 0 July 6th 06 05:48 AM
info in Excel import into Access to combine multiple records then. farm dog dad Excel Programming 1 July 1st 05 12:38 AM
I can import Access Tables. But, I can't import Access queries nickg420[_8_] Excel Programming 0 August 5th 04 07:46 PM


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