Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

[5分]谁有功夫帮我解读这段函数代码?

殷小宝  发表于:2012-02-16 11:28:27  
复制

Function ReportFormat(rpt As Report, ctlID As Control, Optional FormatMode As Integer = 0, _
                      Optional FillBlankLine As Boolean = True) As Boolean
    On Error GoTo Err_ReportFormat
    Dim intMaxRec As Integer
    Static intMaxRecOfPage1 As Integer
    Dim intRecCount As Integer
    Dim intCtlCount As Integer
    Dim intMaxctl As Integer
    Dim sngTop As Single
    Dim strFirst As String
    Dim strLast As String
    Dim ctlFirst As Control
    Dim ctlLast As Control

    With rpt.Section(acDetail)
        If rpt.Page = 1 Then
            intMaxRecOfPage1 = ctlID.Value
            intMaxRec = intMaxRecOfPage1 - 1
        Else
            If FillBlankLine = True Then
                intMaxRec = intMaxRecOfPage1 - 1
            Else
                intMaxRec = ctlID.Value - intMaxRecOfPage1 * (rpt.Page - 1) - 1
            End If
        End If

        For intRecCount = 0 To intMaxRec
            sngTop = ctlID.Top + .Height * intRecCount + rpt.Section(acPageHeader).Height
            intMaxctl = .Controls.Count - 1
            For intCtlCount = 0 To intMaxctl
                With rpt.Section(acDetail).Controls(intCtlCount)
                    If intCtlCount = 0 Then strFirst = .Name
                    If .Visible = True Or .Name = ctlID.Name Then
                        If FormatMode <> 1 Then rpt.Line (.Left, sngTop)- _
                                                         (.Left, sngTop + rpt.Section(acDetail).Height)
                    End If

                    If intCtlCount = intMaxctl Then
                        strLast = .Name
                        If FormatMode <> 1 Then rpt.Line (.Left + .Width, sngTop)- _
                                                         (.Left + .Width, sngTop + rpt.Section(acDetail).Height)
                    End If
                End With
            Next
            Set ctlFirst = .Controls(strFirst)
            Set ctlLast = .Controls(strLast)
            If FormatMode <> 2 Then
                rpt.Line (ctlFirst.Left, sngTop)-(ctlLast.Left + ctlLast.Width, sngTop)
                If intRecCount = intMaxRec Then
                    rpt.Line (ctlFirst.Left, sngTop + .Height)- _
                             (ctlLast.Left + ctlLast.Width, sngTop + .Height)
                End If
            End If
        Next
    End With
    If Err.Number = 0 Then ReportFormat = True

Exit_ReportFormat:
    Exit Function

Err_ReportFormat:
    ReportFormat = False
    MsgBox "Error Number" & Chr(13) & Err.Number & Chr(13) & Err.Description, vbCritical
    Resume Exit_ReportFormat
End Function

 

Top
瞿丽忠 发表于:2012-02-16 12:38:22

这么长,可没时间

帮你顶一下



殷小宝 发表于:2012-02-16 13:31:42

先解读下列一段代码吧:



蟹仔 发表于:2012-02-16 22:10:34
你这是补空行的代码,其实很简单,你把前面的定义改成中文,就会说“哦~原来如此!”   intMaxRec     intMaxRecOfPage1      intRecCount      intCtlCount     intMaxctl sngTop   strFirst      strLast    ctlFirst     ctlLast 还好写这些代码的作者专业,通俗易懂。无非就是页码数,行数,控件数,控件高度;长度;左边距这些,接着一大堆if的判断语句,根据不同的页数,行数,控件大小,利用画线画出相应的空格补上而已。

蟹仔 发表于:2012-02-16 22:14:43
Public Function ReportSheet(rpt As Report, _
                            LeftControl As Control, _
                            RightControl As Control, _
                            Optional RowsOfPage As Integer, _
                            Optional Style As Integer = 0, _
                            Optional HasColumnHeader As Boolean = True)
'On Error Resume Next

    Dim intI As Integer
    Dim lngTop As Long          '表格上边距,即报表页眉的高度
    Dim lngBottom As Long       '表格下边距,报表页眉的高度 +主体节高度×每页要显示的记录数
    Dim lngLeft As Long         '表格左边距,第一个控件的左边距
    Dim lngRight As Long        '表格右边距,最后一个控件的左边距+最后一个控件的宽度
    Dim lngRowHeight As Long    '行高,即主体节高度
   
    Dim lngRows As Long
    Dim lngRowTop As Long
    Dim lngBottomMax As Long
   
    Dim ctl As Control
   
    With rpt
        lngRowHeight = .Section(acDetail).Height                          ' 行高:主体节高度
        lngTop = .Section(acPageHeader).Height                            ' 设上边距为页面页眉高度,为防止报表没有页面页眉所以代码单独一行
        If .Page = 1 Then lngTop = .Section(acHeader).Height     '  lngTop  + .Section(acHeader).Height 第一页再加上报表页眉高度,为防止报表没有报表页眉所以代码单独一行
        lngBottomMax = .Section(acPageFooter).Height                      ' 页面页脚高度,为防止报表没有页面页脚所以代码单独一行
        lngBottomMax = .ScaleHeight - lngBottomMax                        ' 报表高度减去页面页脚高度得到最大允许的下边距
    End With

    lngRows = Int((lngBottomMax - lngTop) / lngRowHeight)               ' 当前页面能容纳的行数
    If RowsOfPage > 0 Then
       If RowsOfPage < lngRows Then lngRows = RowsOfPage                  ' 如果指定的行数不超过能容纳的行数,取指定行数
    End If
    lngBottom = lngTop + lngRowHeight * lngRows                           ' 根据行数计算表格下边距
   
    If HasColumnHeader Then
        lngRows = lngRows + 1
        lngTop = lngTop - lngRowHeight
    End If

    lngLeft = rpt.ScaleWidth
    For Each ctl In rpt.Section(acDetail).Controls
        If lngLeft > ctl.Left Then lngLeft = ctl.Left                                ' 表格左边距
        If lngRight < ctl.Left + ctl.Width Then lngRight = ctl.Left + ctl.Width      ' 表格右边距
       ' If Style <> 1 Then rpt.Line (ctl.Left, lngTop)-(ctl.Left, lngBottom)         ' 画竖线
    Next
   ' If Style <> 1 Then rpt.Line (lngRight, lngTop)-(lngRight, lngBottom)             ' 在最右边画竖线
   
    '画横线
    If Style <> 2 Then
        For intI = 1 To lngRows
            rpt.Line (lngLeft, lngTop + lngRowHeight * intI)-(lngRight, lngTop + lngRowHeight * intI)
        Next
    End If
       
End Function

 

 

 

这个就是你下面那个中原一剑的帖子一位网友的回答,他问的也是补空行的问题,代码跟你这个差不多。不过他的有注释,更容易看懂。



殷小宝 发表于:2012-02-17 10:12:20

谢谢你这么热心的指导,你帖出来的代码是红尘如烟写的,注释也是他写的。我将仔细思考思考。



殷小宝 发表于:2012-02-20 13:03:33

蟹仔 老师



总记录:6篇  页次:1/1 9 1 :