Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Limiting Data Imported from Access to Excel

The code below pulls in a query from Access to my Excel spreadsheet.
It works great, but I'd like to limit the amount of data it brings in.
I've used parametric queries before - but they limit you to reading
data from one fixed file. In this case the Access file is read from a
cell in an Excel spreadsheet and used in the code below.

For example, one column in the Access query is named SALE_LOT.
Is there any way to modify the Access query so it could limit records
to SALE_LOT = "189:002"?

Here's the code. Thanks in advance! Mark

Private Sub CommandButton1_Click()
Worksheets("data").Range("A1:HH50000").Clear
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim xlApp As Object
Dim xlWb As Object
Dim xlWs As Object
Dim recArray As Variant
Dim strDB As String
Dim fldCount As Integer
Dim recCount As Long
Dim iCol As Integer
Dim iRow As Integer
strDB = Worksheets("input").Range("C4") '<<<<this is the Access
path/file
Set xlApp = Application
Set ap = CreateObject("Access.Application")
ap.OpenCurrentDatabase (strDB)
cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDB & ";"
'begin import from "master oneline detail 2"
xlApp.Sheets("data").Select
rst.Open "Select * From [master oneline detail 2]", cnt
Set xlWb = ActiveWorkbook
Set xlWs = xlWb.Worksheets("Data")
fldCount = rst.Fields.Count
For iCol = 1 To fldCount
xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
Next
xlWs.Cells(2, 1).CopyFromRecordset rst
rst.Close
' end import
cnt.Close
Set ap = Nothing
Set rst = Nothing
Set cnt = Nothing
Set xlWs = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 599
Default Limiting Data Imported from Access to Excel

Mark

This line

rst.Open "Select * From [master oneline detail 2]", cnt


should be changed so that the SQL argument contains the criteria. Set up a
query in Access returning the records you want and look at the SQL view to
see the proper string.

--
Dick Kusleika
MVP - Excel
www.dicks-clicks.com
Post all replies to the newsgroup.

"Mark" wrote in message
om...
The code below pulls in a query from Access to my Excel spreadsheet.
It works great, but I'd like to limit the amount of data it brings in.
I've used parametric queries before - but they limit you to reading
data from one fixed file. In this case the Access file is read from a
cell in an Excel spreadsheet and used in the code below.

For example, one column in the Access query is named SALE_LOT.
Is there any way to modify the Access query so it could limit records
to SALE_LOT = "189:002"?

Here's the code. Thanks in advance! Mark

Private Sub CommandButton1_Click()
Worksheets("data").Range("A1:HH50000").Clear
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim xlApp As Object
Dim xlWb As Object
Dim xlWs As Object
Dim recArray As Variant
Dim strDB As String
Dim fldCount As Integer
Dim recCount As Long
Dim iCol As Integer
Dim iRow As Integer
strDB = Worksheets("input").Range("C4") '<<<<this is the Access
path/file
Set xlApp = Application
Set ap = CreateObject("Access.Application")
ap.OpenCurrentDatabase (strDB)
cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDB & ";"
'begin import from "master oneline detail 2"
xlApp.Sheets("data").Select
rst.Open "Select * From [master oneline detail 2]", cnt
Set xlWb = ActiveWorkbook
Set xlWs = xlWb.Worksheets("Data")
fldCount = rst.Fields.Count
For iCol = 1 To fldCount
xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
Next
xlWs.Cells(2, 1).CopyFromRecordset rst
rst.Close
' end import
cnt.Close
Set ap = Nothing
Set rst = Nothing
Set cnt = Nothing
Set xlWs = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Limiting Data Imported from Access to Excel

Thanks, Dick. Great suggestion. It worked wonderfully.

Mark

Mark Roach
Vice President Technical Resources
The Oil & Gas Asset Clearinghouse
Houston, Texas

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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
How do I set up an Excel file to be imported into Access debsews2002 Excel Discussion (Misc queries) 1 February 9th 10 10:17 PM
Excel 2007 - security / limiting access Paul Excel Discussion (Misc queries) 0 September 2nd 09 02:54 PM
How to convert imported Access Hyperlinks to Excel and KEEP the li Rudeseal Excel Worksheet Functions 0 August 28th 09 10:06 PM
Question about limiting access to functions in Excel Brian Smith Excel Discussion (Misc queries) 1 February 1st 07 12:48 PM
Limiting access to excel worksheets Chris Excel Discussion (Misc queries) 2 January 8th 07 10:51 AM


All times are GMT +1. The time now is 05:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"