แสดงกระทู้

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Pathompong Tuncharoen

หน้า: [1]
1
DCount("*","Query","[sell]='ขนม'") นับจำนวน ขนม ใน Query ไม่ได้ใช้นับจำนวนที่แสดงในหน้ารายงานครับ

ปัญหานี้ดูเหมือนจะพื้นๆ แต่ก็ไม่ง่ายเสียทีเดียว สิ่งที่จะทำก็คือ รางานจะต้องมี Page Header, Group Header โดยจัดกลุ่มตามชื่ออาหาร (สมมุติว่าชื่อฟิลด์ที่เก็บชื่ออาหารคือ Food) [แก้ไขเพิ่มเติม]และต้องกำหนด Repeat Section property ของ Group Header เป็น Yes เพื่อให้ Group Header ถูกพิมพ์เมื่อเริ่มต้นของทุกหน้าด้วย, มีส่วนของ Detail และ Page Footer เพื่อใส่เท็กซ์บ็อกซ์ SummaryText สำหรับพิมพ์ข้อความสรุปจำนวนอาหารในหน้า  และถึงแม้ว่าคุณจะไม่มีอะไรจะพิมพ์ใน Page Header และ Group Header ก็ตาม แต่ก็ขอให้มีความสูงนิดเดียวก็พอ เพราะต้องเรียกใช้ Print event procedure ของ 2 ส่วนนี้เพื่อเขียนโค้ด



ส่วนโค้ดก็มีตามนี้นะครับ (ถ้าชื่อ section ของคุณต่างออกไป ก็แก้ไขโค้ดด้วย)
โค๊ด: [Select]
Option Compare Database
Option Explicit

' สร้างประเภทข้อมูลเพื่อเก็บชื่ออาหารและจำนวน
Private Type SumText
    Title As String
    Count As Integer
End Type

' สร้างตัวแปรอะเรย์สำหรับประเภท SumText
Dim SumArray() As SumText

Private Sub PageHeaderSection_Print(Cancel As Integer, PrintCount As Integer)
    If PrintCount <> 1 Then Exit Sub
   
    ' เมื่อเริ่มพิมพ์ส่วนหัวกระดาษ ให้ล้างพื้นที่อะเรย์ เพื่อเตรียมรองรับผลรวมในหน้านี้
    ReDim SumArray(0)
End Sub

Private Sub GroupHeader0_Print(Cancel As Integer, PrintCount As Integer)
    If PrintCount <> 1 Then Exit Sub
   
    ' เมื่อเริ่มพิมพ์หัวของกลุ่ม ให้เพิ่มพื้นที่สำหรับอะเรย์อีก 1 element
    ReDim Preserve SumArray(UBound(SumArray) + 1)
End Sub

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
    If PrintCount <> 1 Then Exit Sub
   
    ' ในแต่ละบรรทัดที่พิมพ์ ให้เก็บชื่ออาหารและบวกจำนวนเพิ่มทีละ 1
    With SumArray(UBound(SumArray))
        .Title = Me.Food
        .Count = .Count + 1
    End With
End Sub

Private Sub PageFooterSection_Print(Cancel As Integer, PrintCount As Integer)
    Dim I As Integer
   
    If PrintCount <> 1 Then Exit Sub
   
    ' ล้างข้อความสรุปที่มีในหน้าเก่า
    Me.SumaryText = ""
   
    ' วนตั้งแต่อะเรย์ element แรก ไปจนหมดทุก element
    For I = 1 To UBound(SumArray)
        With SumArray(I)
            ' ใส่ชื่ออาหารและจำนวนนับลงข้อความสรุป
            Me.SumaryText = Me.SumaryText & ", " & .Title & " = " & .Count
        End With
    Next
   
    ' ลบ ", " ตัวแรกในข้อความสรุปทิ้งไป
    Me.SumaryText = Replace(Me.SumaryText, ", ", "", , 1)
End Sub
โพสต์นี้ได้รับคำขอบคุณจาก: Pathompong Tuncharoen

หน้า: [1]