将 MSChart 与 Access 数据库一起使用

Use MSChart with Access Database

我正在用 vb6 创建一个学生数据库项目,我需要显示学生成绩的图表。我找不到任何关于如何做的好教程。我找到了这个 how to create Mschart in VB6?,但链接到它的教程已被删除。如何将 MSChart 连接到我的数据库?

感谢您的帮助:)

我不完全确定你想做什么,但这里有一个黑暗中的疯狂尝试,至少可以提供一些想法。

Table: Grades
Holds a row for each Student and their Grade in percent from 0 to 100.
Field: ID AutoNumber, Primary Key
Field: Student Text(50)
Field: Grade Byte

我想将学生成绩值的计数绘制为成绩范围间隔的条形图,例如0-9%、10-19%、... 100%。

为了确保我得到该范围内有 0 个学生的间隔,我将对另一个 table GradeRanges:

进行 Grades 的左外连接
Table: GradeRanges
Predefined auxiliary table with values from 0 to 100 in steps of 10 for GradeRange values.
Field: ID AutoNumber, Primary Key
Field: GradeRange Byte

我刚好有 30 个学生的成绩。结果:

Option Explicit

Private Sub Form_Load()
    Dim CN As ADODB.Connection
    Dim RS As ADODB.Recordset

    With MSChart1
        .TitleText = "Counts of Student Grades in 10% Intervals"
        With .Title.VtFont
            .Size = 14
            .Style = VtFontStyleBold
            .VtColor.Set 32, 96, 192
        End With
        With .Plot
            With .Wall.Brush
                .Style = VtBrushStyleSolid
                .FillColor.Set 255, 255, 255
            End With
            With .Axis(VtChAxisIdY).ValueScale
                .Auto = False
                .Maximum = 15
                .Minimum = 0
                .MajorDivision = 3
                .MinorDivision = 2
            End With
            With .SeriesCollection.Item(1).DataPoints.Item(-1)
                With .Brush
                    .Style = VtBrushStyleSolid
                    .FillColor.Set 192, 224, 255
                End With
                With .DataPointLabel
                    .LocationType = VtChLabelLocationTypeAbovePoint
                    .VtFont.Style = VtFontStyleBold
                End With
            End With
        End With
    End With

    Set CN = New ADODB.Connection
    CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Mode=Share Exclusive;" _
          & "Data Source='grades.mdb'"

    On Error Resume Next 'In case previous run failed try dropping this here.
    CN.Execute "DROP TABLE [TempGrades]", , adCmdText Or adExecuteNoRecords
    On Error GoTo 0

    'Create TempGrades table containing CalcRange values so we can do a
    'LEFT OUTER JOIN to the GradeRanges table.  This allows us to plot
    'the ranges with a Count of 0 in them:
    CN.Execute "SELECT [Grade], ([Grade] \ 10) * 10 AS [CalcRange] " _
             & "INTO [TempGrades] FROM [Grades]", _
               , _
               adCmdText Or adExecuteNoRecords

    Set RS = New ADODB.Recordset
    With RS
        .CursorLocation = adUseClient
        .Open "SELECT CStr([GradeRange]) & '%', COUNT([Grade]) AS [Count] " _
            & "FROM [GradeRanges] LEFT OUTER JOIN [TempGrades] " _
            & "ON [GradeRanges].[GradeRange] = [TempGrades].[CalcRange] " _
            & "GROUP BY [GradeRange] " _
            & "ORDER BY [GradeRange]", _
              CN, _
              adOpenStatic, _
              adLockReadOnly, _
              adCmdText
        .MoveFirst 'Important, otherwise MSChart will lose the 1st row data!
                   'Also note that the 1st column MUST BE String data or it
                   'will not be used as X-axis Label values.
        Set MSChart1.DataSource = RS
        .Close
    End With

    CN.Execute "DROP TABLE [TempGrades]", , adCmdText Or adExecuteNoRecords
    CN.Close
End Sub

Private Sub Form_Resize()
    If WindowState <> vbMinimized Then
        MSChart1.Move 0, 0, ScaleWidth, ScaleHeight
    End If
End Sub

我很乐意在没有辅助和温度 table 的情况下执行此操作。也许其他人可以提出替代方案。