Solved

loading and editing values in spreadsheet

Posted on 2001-07-25
2
237 Views
Last Modified: 2008-03-06
I would like to build a function in MS Access '97 that automatically generates a report in Excel format.  Here are the basic steps:
1. copy list over to specific preformatted spreadsheet (headers are item# with month/year from current month out to 10 years)
2. for each item in MS Access table, read date value and level value
3. Locate the proper cell location based on date (month/year) and item#, then write the date and change the cell color based on the level value (low=green, mod=yellow, high=red).  The cell color to the right should be colored the same until it hits another level on the same item.  For example, item#1 has level=low for Jan 2001 and level=high for June 2001.  The cell color for item#1 should be green from Jan 2001 to May 2001 and red for June 2001 (no color for months after June 2001 since that's the last date for item).

I am not familiar with manipulating data in a spreadsheet using MS Access.  I would like to know the functions required  to building the above report.  Thanks.

-jptu

0
Comment
Question by:jptu
2 Comments
 
LVL 3

Accepted Solution

by:
carruina earned 200 total points
ID: 6321307
select the reference "Microsoft Excel 8.0 Object Library" in the "Tools -> References"

and try with this code:



Dim xlApp As Excel.Application

Dim xlBook As Excel.Workbook

Dim xlSheet As Excel.Worksheet




Private Sub Comando0_Click()
Dim lrst As Recordset
Dim auxdate As Date
Dim auxprevdate As Date
Dim i As Integer
Dim txtsql As String
Dim auxitem As Integer

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add 'Create a new Excel book
'If you want use a preformated book change the method Add to Open (path & "\name.xls")
Set xlSheet = xlBook.Worksheets(1)
xlApp.Visible = True

For i = 0 To 119
    auxdate = DateAdd("m", i, Date)
    xlSheet.Cells(1, i + 1) = Format(auxdate, "dd/mm/yy")
Next i

txtsql = "SELECT Tbl_Levels.* "
txtsql = txtsql & "FROM Tbl_Levels "
txtsql = txtsql & "WHERE DATE >#" & Format(Date, "mm/dd/yyyy") & "# "
txtsql = txtsql & "ORDER BY Tbl_Levels.Item, Tbl_Levels.Date;"

Set lrst = CurrentDb().OpenRecordset(txtsql)


While Not lrst.EOF
    If auxitem <> lrst!Item Then
        auxitem = lrst!Item
        auxprevdate = lrst!Date
    End If
    xlSheet.Cells(lrst!Item + 1, DateDiff("m", Date, lrst!Date) + 1) = lrst!Date
    Select Case lrst!level
        Case "low"
            xlSheet.Cells(lrst!Item + 1, DateDiff("m", Date, lrst!Date) + 1).Interior.Color = vbGreen
        Case "med"
            xlSheet.Cells(lrst!Item + 1, DateDiff("m", Date, lrst!Date) + 1).Interior.Color = vbYellow
        Case "high"
            xlSheet.Cells(lrst!Item + 1, DateDiff("m", Date, lrst!Date) + 1).Interior.Color = vbRed
    End Select
    If auxprevdate <> lrst!Date Then
        xlSheet.Range(xlSheet.Cells(lrst!Item + 1, DateDiff("m", Date, auxprevdate) + 2), xlSheet.Cells(lrst!Item + 1, DateDiff("m", Date, lrst!Date))).Interior.Color = xlSheet.Cells(lrst!Item + 1, DateDiff("m", Date, auxprevdate) + 1).Interior.Color
        auxprevdate = lrst!Date
    End If
    lrst.MoveNext
Wend



Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
0
 

Author Comment

by:jptu
ID: 6325658
Very nice answer!  Thanks carruina.

-jptu

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now