Discussion Locked

Access 2000 SendObject to Excel

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm using MS Access 2000

I'm using the following command:
"DoCmd.SendObject acSendQuery, sQDName, acFormatXLS, sEmailAddr"
to create an Excel spreadsheet and e-mail it to a specified address.

My question is this:
How can I have a formula ina certain cell?

My query is pulling Gross Weight and Tare Weight from a database and including those as 2 columns in the spreadsheet. I want a third column to be the Net Weight (gross - tare). I need to use a formula because once the user receives the spreadsheet via e-mail, they have the option of modifying the gross and/or tare weights. Thus, I would
like the "Net Weight" to recalculate as the user changes the gross and/or tare weight columns.

Thanks in advance,
Mike T.

5 replies

by peyison

There's no easy way to do this. I have two options.

You can enter the text for a formula as a separate field in the query, like:

Select Gross, Tare, "=A2-B2" as Net From tblWeight;

But there are two problems with this. The first is Excel will treat the formula sort of like a string. initially, the contents of the cell will be the actual text of the formula, but if you bring the cell into edit mode by double clicking or pressing F2, then press Enter, it will resolve the formula.

The second problem is getting the right row numbers in the text. If there is just one record, this will be fine. You have to use row 2 in the formula becuase the titles will be in row 1. If you have multiple records, you'll have to devise a way to get the correct row number into each resulting record's formula. So your query might look something like:

Select Gross, Tare, "=A" & Row & "-B" & Row as Net From tblWeight;

and you'll have to increment row, or pull it from the table (e.g. each record in the table will have a row field).

The other option, (better, but more complex) is to use OLE to open or create an Excel workbook, and enter the values and formulas via code. This is not too big of a deal to do, plus you have control over other aspects of the workbook as well, like formatting.

Let me know if you're interested in this route and I'll send you or post some source code.

Hope this helps.

by shanghai sam

Thanks very much for your answer, it validates what I was trying to do and failing at. :-)

You and Peter Ritchie were both great helps. Thanks again.

by peter ritchie

You need to be able to access the excel spreadsheet after creation and before the send. Of course, the SendObject method does this all at once so you can't make any modifications to the sheet.

One method is to create the xls file seperately thenfill in the cells you want and their values.

To create the xls file:
DoCmd.OutputTo acOutputQuery, "queryname", acFormatXLS, "c:\test.xls"

To Modify the spreadsheet:
Dim App, Workbooks, book
Set App = CreateObject("Excel.Application")
Set Workbooks = App.Workbooks
set book = Workbooks.Open("C:\test.xls")
book.sheets(1).Range("d5").Select
App.ActiveCell = "=A2+A1"
book.close true

And then that xls file could be sent via e-mail. (See KB Article Q161088 If using outlook 97, for example:

Dim objOutlook
Set objOutlook = CreateObject("outlook.application")
Dim objMail
set objMail = objOutlook.CreateItem(olMailItem)
With objMail
.To = "me@domain.com"
.Subject = "subject"
.Body = "bleah bleah bleah"
.Attachments.add("c:\test.xls")
.Send
End With

by shanghai sam

Thanks for your answer. Especially thanks for the code snippets, saves me the trouble of figuring it out. You and Peyison were both great helps.

This discussion is closed to new replies.