อยากทราบ function ที่ count string
กระทู้เก่าบอร์ด อ.Yeadram

 1,032   1
URL.หัวข้อ / URL
อยากทราบ function ที่ count string

ผมมีเรื่องอยากทราบ Access function คือ ผมมี field อยู่ ที่ชื่อ [REASON] ซึ่งบรรจุเป็นค่าเป็น text ในแต่ละ records จะมี , (comma) บางหรือไม่มี comma ดังตัวอย่างข้างล่างนี้ครับ

Item    Reason          ผลลัพย์ที่อยากได้
1        12,785,55      3
2        375,55,1        2
3        122               0

ผมอยากทราบถ้าจสร้าง query เพื่อทำการนับจำนวนของ comma จะต้องทำอย่างไร ผมพยายามใช้ function: Count / Dcount ก็ยังไม่ได้

ผมรบกวนด้วยนะครับ

1 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R15194
เขียนฟังก์ชั่นเองครับ
1. เขียนโค๊ดลงใน Module ดังนี้:

Public Function Count_Chr(txt_String As String) As Integer
    Dim i As Integer
    For i = 1 To Len(txt_String)
        If Mid(txt_String, i, 1) = "," Then
            Count_Chr = Count_Chr + 1
        End If
    Next
End Function

หรือแบบนี้ก็ได้ครับ

Public Function Count_Chr2(txt_String As String) As Integer
    Dim TCount As Integer, NCount As Integer
    NCount = 1
    Do
        TCount = InStr(NCount, txt_String, ",")
        If TCount > 0 Then
            NCount = 1 + TCount
            Count_Chr2 = Count_Chr2 + 1
        End If
    Loop Until TCount = 0
End Function

- ไม่รู้ว่าหากข้อมูลเยอะๆ อย่างไหนเร็วกว่ากัน หรือพอๆกัน เลือกใช้ซักอันก็พอนะครับ

2. ในคิวรี่ ใช้ฟังก์ชั่นกับฟิดล์ใหม่ดังนี้:

ผลลัพย์ที่อยากได้: Count_Chr([REASON])
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.2862s