Question

Hey guys. Just wondering if anyone knows of a method to create sparkline graphs on a form in MS Access. The chart builder does not really work very well to create sparkline charts (graphs that small).

Just curious, thanks!

Was it helpful?

Solution

I don't think there is anything built in for Sparkline graphs in MS Access. You have to use a third party control and deploy it along with your app to all users or use MS Excel embedded control to show the graph.

OTHER TIPS

There is a VBA-powered sparkline solution featured on the Access blog fairly recently: http://blogs.office.com/b/microsoft-access/archive/2011/02/10/power-tip-add-sparkline-like-graphs-to-access-reports.aspx

They have an .mdb as well as an .accdb sample file available, so I'm guessing it works across multiple versions.

I started with the VBA-powered sparkline but didn't like that it looked low-resolution and I couldn't use it on a continuous form (it only works on reports). The solution I came up with was to build the charts in Excel and save the chart images in a subfolder. Then it's easy to link the image on a report or continuous form. My charts update nightly, though the Excel chart building loop is really fast. The slow part is generating the data that the charts need, which may vary depending on what you are charting.

I created a template in Excel that had a chart with the look and resolution that I wanted. I wrote a VBA routine in Access to open an Excel sheet and loop through each record that I wanted to chart. The sheet is passed to this function (below), which loads a records of chart data and passes it into Excel, which automatically refreshes the 'SparkChart' object. It then saves the image to a subfolder. The Excel sheet stays open and is re-used with each loop. I didn't include the function with the loop.

Here's what my chart looks like in Excel:

Excel file with chart

Here is an example of the Sparklines shown in a continuous form:

Data Quality Dashboard example

Public Function fCreateSparklineChart(pDQ_ID As Long, pChartSheet As Object) As Boolean
' Pass in a Dashboard Query ID for data that has already compiled into the top-n
' temp table and the data will be copied to the passed pChartSheet in Excel.  This
' will update a chart object, then the chart is saved as a .png file.

    Dim strSQL As String
    Dim strChartPath As String
    Dim rs As DAO.Recordset

    On Error GoTo ErrorHandler

    ' Get chart data from a table that has already been compiled with 
    ' min and max values as percentages so the lowest value is 0
    ' and the highest value is 100.
    strSQL = "  SELECT DQ_ID, Trend_Value, " & _
            " IIf(Trend_Value=0,0,Null) AS Min_Point, " & _
            " IIf(Trend_Value=100,100,Null) AS Max_Point " & _
            " FROM " & DASHBOARD_TMP_TABLE & _
            " WHERE (DQ_ID=" & pDQ_ID & ") "
    strSQL = strSQL & " ORDER BY RowNo "

    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)

    If rs.RecordCount > 0 Then

        pChartSheet.Range("A1").CurrentRegion.Clear
        pChartSheet.Range("A1").CopyFromRecordset rs
        pChartSheet.ChartObjects("SparkChart").Chart.SetSourceData pChartSheet.Range("rngData")

        ' Use a filename that includes the record ID.
        strChartPath = CurrentProject.Path & "\Images\Sparkline_DQ_ID_" & Format(pDQ_ID, "0000") & ".png"

        ' Delete the file if it already exists.
        DeleteFile strChartPath

        ' Save the Excel chart as a png file.
        pChartSheet.ChartObjects("SparkChart").Chart.Export strChartPath, "png"

        fCreateSparklineChart = True
    End If

Exit_Function:
    Exit Function

ErrorHandler:
    fCreateSparklineChart = False

    MsgBox "Error #" & err.Number & " - " & err.Description & vbCrLf & "in procedure fCreateSparklineChart of basSparkline"
    GoTo Exit_Function

End Function

I've been thinking of creating a YouTube video explaining how I built this Data Quality Dashboard to chart data trends. Let me know if you are interested and I may be encouraged to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top