Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

loading and editing values in spreadsheet

Posted on 2001-07-25
2
Medium Priority
?
250 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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

971 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