Thanks for your fast response, I've heard about Macro but never tried it before on foxpro.
Where can I find a tutorial for this method?
Main Topics
Browse All TopicsI would like to know how should I export my .dbf data to excel into different sheets after I done my sql query.
Below are my codes:
SELECT ENG2.PLU,ENG2.ITEM,ENG2.QT
FROM D:\REPORTS\DATA\Eng\ENG2, D:\Reports\T2000\Data\item
WHERE ENG2.PLU = ITEMM.PLU;
INTO TABLE D:\REPORTS\DATA\Eng\ENG3
CLOSE DATABASES
USE D:\REPORTS\Data\Eng\ENG3
COPY TO D:\REPORTS\Data\MenuEngOct
This question is in progress.
Our experts are working on an answer right now.
Sign up for immediate access to the solution once it becomes available.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
It is hard to know with the code you have posted, at what point you need to decide when you want to put the data on another sheet. Whats is the criteria? The record count perhaps?.
There aren't any tutorial (that I know of) on how to use Excel Macro in VFP. It is mostly a matter of a little translation. For example if you start Excel, and go to Tools-Macro-Record a new Macro, and add an extra sheet to your workbook. Stop the macro recording, and go back to the Macro and edit it. You will find :
>>Sheets.Add
Which is not enough to get you going, but knowing the rest of the code may help (Automate Excel, add a new workbook etc...), so here is how you would put that together in VFP.
oExcel = CREATEOBJECT('Excel.applic
oWb = oExcel.Workbooks.Add()
oSheet = owb.Sheets.Add && That is the Macro part.
oExcel.Visible = .t.
Hi Mikegagnon,
Below are the simplify code to give you an idea on how its going to be.
Here I've made a select statement on a selected field (PLU, ITEM, QTY and DEPT).
DEPT will content a department code which consist 1-9.
Here I've already filtered department 2 and inserted into table ENG3.
Then with this table ENG3 that has been filtered, is exported to Menu.xls.
Now for my question is, as I filtered out the other departments,
I would like to use the same Menu.xls file and put it into different sheets.
How should I program it to perform this action?
SELECT ENG2.PLU,ENG2.ITEM,ENG2.QT
FROM D:\ENG2,
WHERE ENG2.DEPT=2;
INTO TABLE D:\ENG3
CLOSE DATABASES
USE D:\ENG3
COPY TO D:\Menu TYPE XL5
Ok, now I understand. But copy to will not do it for you (Unless you want to create a separate workbook for each departments - where you would use an SQL statement filtering on a particular department and using copy to). I am affraid your solution lies in automating Excel as I have shown you above. The solution is more complex than one moght think. Here is the scenario.
1. Do an SQL statement as you have above.
2. Do a second DISTINCT sql statement to determine how many departments you have to deal with and loop through the second cursor to filter out the departments on the first cursor.
3. Once you have you third cursor, loop through each record, inserting line by line the records.
4. Once you have done the first department, add a new page to the workbook and start working on the second department on the new page.
5. Keep going on the same process for all individual departments.
The code for this is difficutl to post as each field of your table need to be formatted depending on the field type.
Here is a start if you wish to open a new workbook for each table, cut is and paste it on a separate sheet of the original workbook.
cFileName = SYS(5)+SYS(2003) + '\temp\test1.dbf'
oexcel = CREATEOBJECT("Excel.Applic
oexcel.Visible=.t.
oWBAll = oexcel.Workbooks.Add
oWBDBF = oexcel.Workbooks.Open(cFil
oWBDBF.Sheets(1).Cells.Cut
oSheet = oWBAll.Sheets.Add
oSheet.Paste
oWBDBF.Close(.F.)
oWBDBF = temporary workbook for the dbf
oWBAll contains all sheets
Make sure you have the tables in fox2x format as I mentioned earlier since excel would not open the latest dbf version.
Business Accounts
Answer for Membership
by: CaptainCyrilPosted on 2007-07-12 at 01:12:19ID: 19469630
You can write a macro that opens the Excel Workbook, creates a new sheet out of opening the DBF file. If the DBF file does not contain memo fields and is saved in an older format it does well.
COPY TO filename.dbf TYPE FOX2X
You can also create a new sheet in Excel and run automation to export the report. You have better control of formatting but this method takes time to develop and execute.
Which one do you prefer?