Solved

loading and editing values in spreadsheet

Posted on 2001-07-25
2
236 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

706 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

18 Experts available now in Live!

Get 1:1 Help Now