หาอายุ และเปลี่ยนปีไทย เป็นฝรั่ง

ปรับปรุง : 2548-05-03 ()
ตอน 2.5 :: หาอายุ และเปลี่ยนปีไทย เป็นฝรั่ง
:: Date calculation ::
:: วิธีการเรียก Module มาทำงานอยู่ใน ตอน 2.1 เริ่มต้น + คำนวณ

> ข้อควรทราบ <
เดิมทีคิดว่าการหาวันเกินไม่น่ามีอะไรยุ่งยาก แต่พอได้ลองทำในหลาย ๆ วิธีพบว่าไม่ได้ผลตามที่ต้องการ ถ้าต้องการผลแบบคร่าว ไม่ serious มากนัก ก็จะไม่มีปัญหา จนกระทั่งศิษย์เก่าคนหนึ่ง มาพบผม แล้วให้ช่วยคำนวนอายุ ปรากฎว่าใช้เวลากว่า 3 ชั่วโมงกว่าจะเขียน module ตัวนี้ ถูกใจเธอในทุกเงื่อนไข เพื่อไม่ให้ใช้เวลาแบบนั้นอีกจึงรวบรวม module มาไว้ที่นี่
แต่ผมจะไม่กล่าวถึงการนำไปใช้มากนัก เพียงแต่แสดงการทำงานแบบเรียก query และเรียกใน list box เท่านั้น เพราะเมื่อเขียน module ได้ก็จะนำไปใช้ได้หลายหลาย ทั้ง form, report หรือ query เป็นต้น ขึ้นอยู่กับว่าจะนำไปใช้ทำรายงานอะไร ตาม criteria ที่ต้องการ

Download : caltime.zip ขนาด 23 KB

> Module source code ใน form1 <
Option Compare Database
Option Explicit

Private Sub คำสั่ง17_Click() DoCmd.OpenQuery ("query1") End Sub
Private Sub คำสั่ง18_Click() Dim sql sql = "SELECT data.name, data.birth, showdate([birth]) AS age FROM Data " sql = sql & "WHERE (((Data.birth) > [Forms]![form1]![Text4])) ORDER BY data.birth;" [รายการ19].RowSource = sql [รายการ19].Requery End Sub

> Module source code ใน module ชื่อ process sample09 <
Option Compare Database
Option Explicit

Function chgmonth(yourdate As Date) Dim thisdate As Date thisdate = date If (Format(thisdate, "mm") < Format(yourdate, "mm")) Then If (Format(thisdate, "dd") > Format(yourdate, "dd")) Then chgmonth = 12 + Format(thisdate, "mm") - Format(yourdate, "mm") Else chgmonth = 12 + Format(thisdate, "mm") - Format(yourdate, "mm") - 1 End If Else If (Format(thisdate, "dd") > Format(yourdate, "dd")) Then chgmonth = Format(thisdate, "mm") - Format(yourdate, "mm") Else If (Format(thisdate, "mm") = Format(yourdate, "mm")) Then chgmonth = 11 Else chgmonth = Format(thisdate, "mm") - Format(yourdate, "mm") - 1 End If End If If chgmonth < 0 Then chgmonth = 0 End If End If End Function
Function chgthaitoengl(getdate As String) ' get วัน/เดือน/ปี เช่น 31/12/2512 chgthaitoengl = Mid(getdate, 1, 2) & "/" & Mid(getdate, 4, 2) chgthaitoengl = chgthaitoengl + "/" & Mid(getdate, 7, 4) - 543 chgthaitoengl = Format(chgthaitoengl, "dd/mm/yyyy") End Function
Function chgyear(getdate As Date) chgyear = Int(Format(date - getdate, "Fixed") / 365) End Function
Function chgdate(getdate As Date) chgdate = Format(date - getdate, "Fixed") Mod 365 chgdate = chgdate Mod 30 End Function
Function showdate(getdate As Date) If chgyear(getdate) > 0 Then showdate = chgyear(getdate) & " ปี " If chgmonth(getdate) > 0 Then showdate = showdate & chgmonth(getdate) & " เดือน " If chgdate(getdate) > 0 Then showdate = showdate & chgdate(getdate) & " วัน " End Function
Download : caltime.mdb ขนาด 102 Kb
ผู้สนับสนุน + ผู้สนับสนุน
+ รับผู้สนับสนุน

แนะนำเว็บใหม่ : ผลการจัดอันดับ
รักลำปาง : thcity.com : korattown.com : topsiam.com : มหาวิทยาลัยโยนก
ศูนย์สอบ : รวมบทความ : ไอทีในชีวิตประจำวัน : ดาวน์โหลด : yourname@thaiall.com
ติดต่อ ทีมงาน ชาวลำปาง มีฝันเพื่อการศึกษา Tel.08-1992-7223