ACC97: Datasheet of Chart Not Synchronized with Form or ReportID: Q182131
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
During the Current event of a form, there is a one-record difference
between the values displayed in the chart on the form and the values
contained in the datasheet of the chart. The datasheet's values reflect
data from the previous record, not the current record. This behavior also
occurs during the Format or Print event of the detail section of a report.
The chart itself displays the correct values because Microsoft Access populates it directly from its row source, which is a SQL statement or a saved query. During the Current, Format, and Print events, the data sheet of the chart does not yet contain the data from the row source that appears in the chart itself.
If you need to modify a chart characteristic that depends on the data in
the current record or on any state produced by an OnCurrent, OnFormat, or
OnPrint procedure, you must clear the datasheet of the chart and repopulate
it. The following example uses a Visual Basic for Applications procedure to
clear and repopulate the datasheet of a chart during the Current event of
the form.
Private Sub Form_Current()
Dim chtObj As Object, strRowSource As String
Dim rsRowSourceFiltered As Recordset
Dim intMaxShippers As Integer
Dim i As Integer, j As Integer
Set chtObj = Me!chtSync.Object
intMaxShippers = 3
' In the chart's RowSource, insert a WHERE
' clause based on the value of the form
' field contained in the chart control's
' LinkMasterFields property.
strRowSource = Left(Me!chtSync.RowSource, _
InStr(Me!chtSync.RowSource, "GROUP BY") - 1) _
& "WHERE " & Me!chtSync.LinkChildFields & _
" = '" & Me(Me!chtSync.LinkMasterFields) & _
"'" & " " & Right(Me!chtSync.RowSource, _
Len(Me!chtSync.RowSource) _
- InStr(Me!chtSync.RowSource, "GROUP BY") + 1)
Set rsRowSourceFiltered = CurrentDb. _
OpenRecordset(strRowSource, dbOpenSnapshot)
' Check to see if the filtered recordset has any records.
If rsRowSourceFiltered.BOF And _
rsRowSourceFiltered.EOF Then
MsgBox "There are no records to chart."
Exit Sub
End If
' Clear the rows required for the maximum number of
' data rows. The first row contains the column
' headers. Data rows begin at the second row.
With chtObj.Application.DataSheet
For i = 1 To intMaxShippers
.Rows(i + 1).Include = False
Next
End With
' Ensure the RecordCount value is updated.
rsRowSourceFiltered.MoveLast
' Populate the chart's datasheet with the
' filtered recordset, starting with the
' datasheet's second row.
rsRowSourceFiltered.MoveFirst
For i = 1 To rsRowSourceFiltered.RecordCount
For j = 0 To rsRowSourceFiltered.Fields.Count - 1
' Assign data to the datasheet cells starting
' at row 2, column 1.
chtObj.Application.DataSheet. _
Cells(i + 1, j + 1).Value = _
rsRowSourceFiltered.Fields(j).Value
Next
rsRowSourceFiltered.MoveNext
Next
With Me!chtSync.Object.Application
' Each datasheet row contains two columns for the
' shipper's CompanyName and SumProductSales. The first
' row contains the column headers.
Debug.Print "*** OnCurrent Values ***"
For i = 1 To 4
Debug.Print .DataSheet.Cells(i, 1).Value & _
" - " & .DataSheet.Cells(i, 2).Value
Next
End With
End Sub
Private Sub Form_Current()
Dim i As Integer
With Me!chtSync.Object.Application
' Each datasheet row contains two columns for the
' shipper's CompanyName and SumProductSales. The first
' row contains the column headers.
Debug.Print "*** OnCurrent Values ***"
For i = 1 To 4
Debug.Print .DataSheet.Cells(i, 1).Value & _
" - " & .DataSheet.Cells(i, 2).Value
Next
End With
End Sub
Private Sub cmdGraphData_Click()
Dim i As Integer
With Me!chtSync.Object.Application
' Note that this code is the same as
' that in the Current event procedure.
Debug.Print "*** Form View Values ***"
For i = 1 To 4
Debug.Print .DataSheet.Cells(i, 1).Value & _
" - " & .DataSheet.Cells(i, 2).Value
Next
End With
End Sub
For further information about manipulating a chart with Visual Basic for Applications code, see the file Vba8grp.hlp. This file is located in the Morehelp subfolder of the Valupack folder on your Microsoft Office 97 Professional Edition compact disc or your Microsoft Access 97 compact disc.
Additional query words: out of synch incorrect not synchronized behind
Keywords : IntpGrph
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: April 23, 1999