0

我正在用 VB 开发一个自动化系统,该项目处于最后阶段。我正在研究报告生成。我面临的问题是,我想创建一个基于学生班级的全年报告。无论何时生成报告,如果我使用循环,我只能看到报告记录中第一个学生的姓名或最后一个学生的姓名。

仅供参考,我将 ADODB 与 Access 数据库一起使用,并使用 ADODB.RecordSet 读取数据。

我的报告生成部分如下所示:

Set RS1 = New ADODB.recordSet
    RS1.Open "SELECT * FROM RECORDS WHERE sClass = '" & ClassBox.Text & "';", connection, 3, adLockOptimistic
    If Not RS1.EOF Then
        Set DataReport2.DataSource = RS1.DataSource
    End If
    RS1.MoveFirst

    Do Until RS1.EOF
    DataReport2.Sections("Section1").Controls("NameLbl").Caption = CStr(RS1!sName)
    DataReport2.Sections("Section1").Controls("SectionLbl").Caption = CStr(RS1!sSection)
    DataReport2.Sections("Section1").Controls("ClassLbl").Caption = CStr(RS1!sClass)

    DataReport2.Sections("Section1").Controls("JanLbl").Caption = CStr(RS1!January)
    DataReport2.Sections("Section1").Controls("FebLbl").Caption = CStr(RS1!February)
    DataReport2.Sections("Section1").Controls("MarLbl").Caption = CStr(RS1!March)
    DataReport2.Sections("Section1").Controls("AprLbl").Caption = CStr(RS1!April)
    DataReport2.Sections("Section1").Controls("MayLbl").Caption = CStr(RS1!May)
    DataReport2.Sections("Section1").Controls("JunLbl").Caption = CStr(RS1!June)
    DataReport2.Sections("Section1").Controls("JulLbl").Caption = CStr(RS1!July)
    DataReport2.Sections("Section1").Controls("AugLbl").Caption = CStr(RS1!August)
    DataReport2.Sections("Section1").Controls("SepLbl").Caption = CStr(RS1!September)
    DataReport2.Sections("Section1").Controls("OctLbl").Caption = CStr(RS1!October)
    DataReport2.Sections("Section1").Controls("NovLbl").Caption = CStr(RS1!November)
    DataReport2.Sections("Section1").Controls("DecLbl").Caption = CStr(RS1!December)
    DataReport2.Sections("Section1").Controls("TotalLbl").Caption = CStr(Val(RS1!January) + Val(RS1!February) + _
    Val(RS1!March) + Val(RS1!April) + Val(RS1!May) + Val(RS1!June) + Val(RS1!July) + Val(RS1!August) + _
    Val(RS1!September) + Val(RS1!October) + Val(RS1!November) + Val(RS1!December))
    RS1.MoveNext
    Loop
    DataReport2.Show

我想要的是创建报告,其中报告包含学生的姓名和详细信息,基于我的搜索条件,在这种情况下是班级。

4

1 回答 1

0

您可以尝试修改现有代码。如果您的控件有一个DataField(这将取决于所使用的控件),您可以尝试设置它而不是Caption属性。我还修改了您的查询以添加 SUM(January, ...) 字段。我相信如果您喜欢这种方法,您也可以在报告中添加一个公式字段来执行此操作。我只添加了续行,因此查询不会永远向右滚动。

Set RS1 = New ADODB.recordSet
RS1.Open "SELECT sName, sSection, sClass, January, February, March, April, " _
    & "May, June, July, August, September, October, November, December, " _
    & "(January + February + March + April + May + " _
    & "June + July + August + September + October + " _
    & "November + December) AS MonthTotal FROM RECORDS " _
    & "WHERE sClass = '" & ClassBox.Text & "'", connection, 3, adLockOptimistic

' RS1.MoveFirst - not needed

DataReport2.Sections("Section1").Controls("NameLbl").DataField = "sName"
DataReport2.Sections("Section1").Controls("SectionLbl").DataField = "sSection"
DataReport2.Sections("Section1").Controls("ClassLbl").DataField = "sClass"

DataReport2.Sections("Section1").Controls("JanLbl").DataField= "January"
DataReport2.Sections("Section1").Controls("FebLbl").DataField = "February"
DataReport2.Sections("Section1").Controls("MarLbl").DataField = "March"
DataReport2.Sections("Section1").Controls("AprLbl").DataField = "April"
DataReport2.Sections("Section1").Controls("MayLbl").DataField = "May"
DataReport2.Sections("Section1").Controls("JunLbl").DataField = "June"
DataReport2.Sections("Section1").Controls("JulLbl").DataField = "July"
DataReport2.Sections("Section1").Controls("AugLbl").DataField = "August"
DataReport2.Sections("Section1").Controls("SepLbl").DataField = "September"
DataReport2.Sections("Section1").Controls("OctLbl").DataField = "October"
DataReport2.Sections("Section1").Controls("NovLbl").DataField = "November"
DataReport2.Sections("Section1").Controls("DecLbl").DataField = "December"
DataReport2.Sections("Section1").Controls("TotalLbl").DataField = "MonthTotal"
If Not RS1.EOF Then
    Set DataReport2.DataSource = RS1
    DataReport2.Show
End If
于 2013-11-15T01:49:52.933 回答