Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Import Data from Access via DAO

I am working with Office XP:

- Access database named Sales.mdb (at c:\data\) with a table tblSales
table tblSales has the following structure (about 70 fields):
RcdInd Customer Field1 Field2 Field3 Field4 Field5 Field6 ..

It contains data for 200 Customers and each Customer has 4 records
(RcdInd = 01, 02, 03 and 04)

- Excel workbook SalesD.xls (at c:\data\xlslib\)
This workbook is being used as a template i.e. it is formatted to
reflect data for each Customer.

I have started to write VBA in Excel using DAO to connect to Access

The following is an excerpt from my code:

' Open workbook SalesD.xls
ChDir "C:\DATA\XLSLIB\"
Workbooks.Open FileName:="SalesD.xls"

' Select the MS Access Database that houses the data
Dim DB As Database, RS As Recordset
' Open the Sales.mdb database
Set DB = OpenDatabase("c:\data\sales.mdb")

' Generate Recordset via an SQL query agains table tblSales
Set RS = DB.OpenRecordset(" SELECT Field1, Field2, Field3 " & _
" FROM tblSales " & _
" WHERE RcdInd = '01' AND Customer = '100047-A';")
' Copy recordset onto the worksheet, stating at cell C2
[C2].CopyFromRecordset RS

' Generate Recordset via an SQL query agains table tblSales
Set RS = DB.OpenRecordset(" SELECT Field6, Field7, Field8 " & _
" FROM tblSales " & _
" WHERE RcdInd = '02' AND Customer = '100047-A';")
' Copy recordset onto the worksheet, stating at cell F28
[F28].CopyFromRecordset RS
.................................................. ..........
.................................................. ..........
This code works fine; however, it is cumbersome as the Customer number
must be changed each time.

Is it possible to do the following? (if so, how?)

1. Perform the above for each Customer, save the output as
a new workbook.
2. Continue to process each Customer as above.

Something like:
For i = 1 to 200
code (SQL and CopyFromRecordset routines) .......
Next


Best Regards
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default Import Data from Access via DAO

(LHC01) wrote ...

- Access database named Sales.mdb (at c:\data\) with a table tblSales
table tblSales has the following structure (about 70 fields):
RcdInd Customer Field1 Field2 Field3 Field4 Field5 Field6 ..

It contains data for 200 Customers and each Customer has 4 records
(RcdInd = 01, 02, 03 and 04)

- Excel workbook SalesD.xls (at c:\data\xlslib\)
This workbook is being used as a template i.e. it is formatted to
reflect data for each Customer.

I have started to write VBA in Excel using DAO to connect to Access

The following is an excerpt from my code:

' Open workbook SalesD.xls
ChDir "C:\DATA\XLSLIB\"
Workbooks.Open FileName:="SalesD.xls"

' Select the MS Access Database that houses the data
Dim DB As Database, RS As Recordset
' Open the Sales.mdb database
Set DB = OpenDatabase("c:\data\sales.mdb")

' Generate Recordset via an SQL query agains table tblSales
Set RS = DB.OpenRecordset(" SELECT Field1, Field2, Field3 " & _
" FROM tblSales " & _
" WHERE RcdInd = '01' AND Customer = '100047-A';")
' Copy recordset onto the worksheet, stating at cell C2
[C2].CopyFromRecordset RS

' Generate Recordset via an SQL query agains table tblSales
Set RS = DB.OpenRecordset(" SELECT Field6, Field7, Field8 " & _
" FROM tblSales " & _
" WHERE RcdInd = '02' AND Customer = '100047-A';")
' Copy recordset onto the worksheet, stating at cell F28
[F28].CopyFromRecordset RS
.................................................. ..........
.................................................. ..........
This code works fine; however, it is cumbersome as the Customer number
must be changed each time.

Is it possible to do the following? (if so, how?)

1. Perform the above for each Customer, save the output as
a new workbook.
2. Continue to process each Customer as above.

Something like:
For i = 1 to 200
code (SQL and CopyFromRecordset routines) .......
Next


As an alternative to using CopyFromRecordset, consider using either
the INSERT INTO..SELECT syntax (where the Excel table already exisits)
or the SELECT..INTO syntax (creates the Excel table and even creates
the workbook if it doesn't already exist). Such Jet syntax operates on
closed workbooks and consequently executes much faster.

I am working with Office XP


As an alternative to using DAO, consider using the more modern ADO.
You'll find more people in these ngs use ADO and find more support
generally.

Jamie.

--
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Import Data from Access via DAO

(Jamie Collins) wrote in message . com...
(LHC01) wrote ...

- Access database named Sales.mdb (at c:\data\) with a table tblSales
table tblSales has the following structure (about 70 fields):
RcdInd Customer Field1 Field2 Field3 Field4 Field5 Field6 ..

It contains data for 200 Customers and each Customer has 4 records
(RcdInd = 01, 02, 03 and 04)

- Excel workbook SalesD.xls (at c:\data\xlslib\)
This workbook is being used as a template i.e. it is formatted to
reflect data for each Customer.

I have started to write VBA in Excel using DAO to connect to Access

The following is an excerpt from my code:

' Open workbook SalesD.xls
ChDir "C:\DATA\XLSLIB\"
Workbooks.Open FileName:="SalesD.xls"

' Select the MS Access Database that houses the data
Dim DB As Database, RS As Recordset
' Open the Sales.mdb database
Set DB = OpenDatabase("c:\data\sales.mdb")

' Generate Recordset via an SQL query agains table tblSales
Set RS = DB.OpenRecordset(" SELECT Field1, Field2, Field3 " & _
" FROM tblSales " & _
" WHERE RcdInd = '01' AND Customer = '100047-A';")
' Copy recordset onto the worksheet, stating at cell C2
[C2].CopyFromRecordset RS

' Generate Recordset via an SQL query agains table tblSales
Set RS = DB.OpenRecordset(" SELECT Field6, Field7, Field8 " & _
" FROM tblSales " & _
" WHERE RcdInd = '02' AND Customer = '100047-A';")
' Copy recordset onto the worksheet, stating at cell F28
[F28].CopyFromRecordset RS
.................................................. ..........
.................................................. ..........
This code works fine; however, it is cumbersome as the Customer number
must be changed each time.

Is it possible to do the following? (if so, how?)

1. Perform the above for each Customer, save the output as
a new workbook.
2. Continue to process each Customer as above.

Something like:
For i = 1 to 200
code (SQL and CopyFromRecordset routines) .......
Next


As an alternative to using CopyFromRecordset, consider using either
the INSERT INTO..SELECT syntax (where the Excel table already exisits)
or the SELECT..INTO syntax (creates the Excel table and even creates
the workbook if it doesn't already exist). Such Jet syntax operates on
closed workbooks and consequently executes much faster.

I am working with Office XP


As an alternative to using DAO, consider using the more modern ADO.
You'll find more people in these ngs use ADO and find more support
generally.

Jamie.

--


Thank you for the recommendations regarding the use of INSERT TO .. SELECT
and DAO.
Any suggestions regarding my main issue?
- Open workbook SalesD.xls
- Connect to Access
- Import data from table tblSales for the first Customer into workbook
SalesD.xls, save the workbook e.g. Book1.xls.
- Perform the same process with the next Customer.
- Repeat process up to 200 Customers.

Thanks again.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default Import Data from Access via DAO

(LHC01) wrote ...

Thank you for the recommendations regarding the use of INSERT TO .. SELECT
and DAO.


I recommended ADO!

Any suggestions regarding my main issue?


You seem to have more than one issue listed <g. They require separate
answers so should be in separate posts. In brief, some suggestions:

- Open workbook SalesD.xls


Record a macro, edit the code.

- Connect to Access


Using ADO or DAO? Google it e.g.

http://groups.google.com/groups?q=%2...22Excel+8.0%22

- Import data from table tblSales for the first Customer into workbook


You have already posted your code to do this.

- save the workbook e.g. Book1.xls.


Record a macro, edit the code.

- Perform the same process with the next Customer.
- Repeat process up to 200 Customers.


You are pretty much already there with your For..Next loop. On each
iteration, replace the 100047-A value with the next customer ID (or
whatever).

My suggestions aren't very specific because, as I said before, I'd
recommend a different approach. In full, it is a lot of code to write
for a ng post and anyhow I suspect you may be able to write most if it
yourself with a little help from the macro recorder <g.

Jamie.

--
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
Import Data from Access toby131 Excel Discussion (Misc queries) 3 October 27th 09 11:47 PM
Import Data from Access query Andy Excel Discussion (Misc queries) 8 April 23rd 09 09:15 AM
Can't import data to access Amin Excel Discussion (Misc queries) 3 September 26th 08 04:56 AM
import data from access to excel George Applegate[_2_] Excel Worksheet Functions 1 April 18th 08 02:13 PM
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 08:29 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"