Solved

loading and editing values in spreadsheet

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Familiarize people with the process of utilizing SQL Server functions 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 Microsoft Ac…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

756 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