Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

loading and editing values in spreadsheet

Posted on 2001-07-25
2
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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…

722 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